Pick a file into an Android application (developer)

Manuel Dahmen

New member
Nov 22, 2021
1
0
0
Visit site
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.
 

Thomas_George

Well-known member
Mar 8, 2015
137
2
18
Visit site
I think you should use an ACTION_OPEN_DOCUMENT intent other than a ACTION_GET_CONTENT one.
With the latter approach, an app just imports a copy of the data. But in your code, you are intending to create a new file with the Url returned.