0

I integrate file picker in my application. so i set below code for that and i use this library for that. library

public class FileChooserExampleActivity extends Activity {

private static final String TAG = "FileChooserExampleActivity";

private static final int REQUEST_CODE = 6384;

private static final int REQUEST_WRITE_STORAGE = 112;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Create a simple button to start the file chooser process
    Button button = new Button(this);
    button.setText(R.string.choose_file);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Display the file chooser dialog
            showChooser();
        }
    });


    setContentView(button);
}




private void showChooser() {
    // Use the GET_CONTENT intent from the utility class
    Intent target = FileUtils.createGetContentIntent();
    // Create the chooser Intent
    Intent intent = Intent.createChooser(
            target, getString(R.string.chooser_title));
    try {
        startActivityForResult(intent, REQUEST_CODE);
    } catch (ActivityNotFoundException e) {
        // The reason for the existence of aFileChooser
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CODE:
            // If the file selection was successful
            if (resultCode == RESULT_OK) {
                if (data != null) {
                    // Get the URI of the selected file
                    final Uri uri = data.getData();
                    Log.i(TAG, "Uri = " + uri.toString());
                    try {
                        // Get the file path from the URI
                        final String path = FileUtils.getPath(this, uri);
                        Toast.makeText(FileChooserExampleActivity.this,
                                "File Selected: " + path, Toast.LENGTH_LONG).show();
                    } catch (Exception e) {
                        Log.e("FileSelectorTestActivity", "File select error", e);
                    }
                }
            }
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

}

permissin:

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

When i run above code in Android Marshmallow 6.0 device and i fetch image from sdcard than i get selected file path value as null so any idea how can i solve this ? your all suggestions are appreciable

ankuranurag2
  • 2,300
  • 15
  • 30
Harshal Kalavadiya
  • 2,412
  • 4
  • 38
  • 71
  • have you give runtime permission ? – Vishal Thakkar Aug 12 '16 at 10:27
  • Android Developer : yes i also check by giving runtime permission but ther is no luck. [http://stackoverflow.com/questions/33139754/android-6-0-marshmallow-cannot-write-to-sd-card](http://stackoverflow.com/questions/33139754/android-6-0-marshmallow-cannot-write-to-sd-card) i refer this link – Harshal Kalavadiya Aug 12 '16 at 10:30
  • "so any idea how can i solve this ?" -- stop trying to get a file path using that library. At best, it will handle a small percentage of scenarios on a small percentage of devices. It is also not a "file picker". If you want to allow the user to pick files that your app has direct filesystem access to, use [an actual file picker](http://android-arsenal.com/tag/35). If you want access to removable media and other sources that you cannot access directly on the filesystem, use [the Storage Access Framework](https://commonsware.com/blog/2016/05/03/storage-access-framework-faq.html). – CommonsWare Aug 12 '16 at 11:23

0 Answers0