0

I have developed a simple app: in the main activity you can open the gallery and select multiple pictures. Their URIs will be stored in a list, and then in another activity I get the URIs from that list to make some thumbnails of these pictures.

The code I use for those two actions are the following.

On the main activity I get the Uri and convert it into a String:

Uri mImageUri=data.getData();

// Get the cursor
Cursor cursor = getContentResolver().query(mImageUri, filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
uriStringList.add(cursor.getString(columnIndex));
cursor.close();

On the second activity I get the Uri in this way:

uriList.add(Uri.parse( "file://" + uriStringList.get(id)));
iwt.setImageBitmap(decodeSampledBitmapFromUri(uriList.get(id), 100, 100));

Everything works smoothly on my Android 5.1 (API 22) smartphone, but when I run the app on my newest Xiaomi smartphone with MIUI 9.2 (Android 7.1.2 API 25), I get a java.IO FileNotFoundException.

Does anybody know the reason of this error?

erik.b
  • 107
  • 9
  • See also [this](https://stackoverflow.com/a/48510756/115145), [this](https://stackoverflow.com/a/35871320/115145), and [this](https://commonsware.com/blog/2016/03/15/how-consume-content-uri.html). – CommonsWare Apr 19 '18 at 21:32
  • Thank you for your reply. Anyway, I am already using a method described [here](https://stackoverflow.com/questions/49221312/android-get-real-path-of-a-txt-file-selected-from-the-file-explorer), and it actually works on one of my smartphones, but not on the other one. I edited the original post, including the code when I use decodeSampledBitmapFromUri. – erik.b Apr 19 '18 at 21:43
  • "I am already using a method described here" -- that statement does not line up with the code in your question. The code in your question assumes that everything is a file (hence the `"file://" +`). As [I pointed out](https://stackoverflow.com/questions/49221312/android-get-real-path-of-a-txt-file-selected-from-the-file-explorer), few `Uri` values point to files on the filesystem. – CommonsWare Apr 19 '18 at 21:47
  • Actually, if I do not put the "file://" + I get the same FileNotFoundException on both phones. – erik.b Apr 19 '18 at 21:51
  • Perhaps you should be holding onto the full `Uri`. My guess is that you are calling `getPath()` on `mImageUri` somewhere along the line, before the data gets into `uriStringList`. – CommonsWare Apr 19 '18 at 21:57
  • In the main activity I convert `Uri` values into `String` values, because it is the only way I found to send them to the second activity through an intent (`putStringArrayListExtra`). Then in the second activity I convert them back into `Uri` values. – erik.b Apr 19 '18 at 22:03
  • "In the main activity I convert Uri values into String values" -- and how are you doing that? "because it is the only way I found to send them to the second activity through an intent" -- a `Uri` is `Parcelable`, and so you can put them in an `Intent` extra. However, [your permission to use the content identified by the `Uri` values is limited to the initial activity, by default](https://commonsware.com/blog/2016/08/10/uri-access-lifetime-shorter-than-you-might-think.html), and so you will have some more work to do. – CommonsWare Apr 19 '18 at 22:08
  • I edited the post adding the code I use to convert Uro into String. I know Uri is Parcelable, but I have a List of `Uri`s, and I have not found a way to send a list of Uri to another activity. – erik.b Apr 19 '18 at 22:16
  • To convert a `Uri` to a string, call `toString()`. To convert the string representation back to a `Uri`, call `Uri.parse()`, without any concatenation stuff. To pass an `ArrayList` of `Parcelable` objects in an `Intent` extra, use `putParcelableArrayListExtra()` on `Intent`. – CommonsWare Apr 19 '18 at 22:28
  • Thank you very much! I tried with `putParcelableArrayListExtra()`, avoiding all the conversions, and now it works! Your advices were really helpful :) – erik.b Apr 19 '18 at 22:53
  • Trying again my app I noticed that without the cursor, the multiple images I select in the gallery are added to the list regardless of the "pick order". But I cannot get an Uri from the cursor, I can use just `cursor.getString(columnIndex)` – erik.b Apr 20 '18 at 08:01
  • There is no guarantee of any "pick order". – CommonsWare Apr 20 '18 at 09:26

0 Answers0