Pick a file into an Android application (developer)

Uncategorized
Hello,

I can’t find a way to select a file from filesystem (image) in a ImageView.

I do it like this:
private void startCreation(){

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*.*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] {"image/*", "video/*"});
intent.addCategory(Intent.CATEGORY_OPENABLE);

Intent intent2 = Intent.createChooser(intent, "Choose a file");
System.out.println(intent2);
startActivityForResult(intent2, 9999);
}
@override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 9999 && resultCode==Activity.RESULT_OK) {
String choose_directoryData = data.getDataString();
Bitmap photo = null;
try {
System.out.println(choose_directoryData);
photo = BitmapFactory.decodeStream(new FileInputStream(choose_directoryData));
currentFile = new File(choose_directoryData);
fillGallery(photo, data);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else {
Toast.makeText(this, "Error request "+requestCode, Toast.LENGTH_LONG).show();
}
}

W/System.err: java.io.FileNotFoundException: content:/com.android.externalstorage.documents/document/primary%3APictures%2FFeatureApp%2Fdata%2FMyImage_1.jpg: open failed:

File is returned in the Uri form and I can’t open it.

Thank you.

Sharing is caring!

Leave a Reply