0

I have read all image files at cursor in the fragment and transferred first activity with any problems but when jumping another activity same file paths are not reading although files have already exist.

Fragment set all media datas and transfer first activity. First activity is looking all media files existing state and all files return true but if first activity transfer these files the second activity which isn't reading same files and when call file operation(BitmapFactory.decodeFile) throw FileNotFoundException.

isFile() function only provide that check if there is a file.

Media model:

public Media(Uri uri, String path){
  this.uri = uri;
  this.path = path;
}

MyFragment.java:

private final LinkedList<Media> mediasList;

void setMediasListAndStartFirstActivity(){

  while (cursor != null && cursor.moveToNext()) {

    Uri uri = Uri.withAppendedPath(contentUri, cursor.getString(cursor.getColumnIndexOrThrow(Images.Media._ID)));
    String path = cursor.getString(cursor.getColumnIndexOrThrow(Images.Media.DATA));  
    mediasList.add(new Media(uri, path));
  }

Intent i = new Intent(requireActivity(), FirstActivity.class);
i.putExtra("mediasList", mediasList);
startActivity(i);      
}

FirstActivity.java:

Bundle extras = getIntent().getExtras();
mediasList = (ArrayList<Media>) getIntent().getExtras().get("mediasList");

for(Media media : mediasList)
 boolean hasMedia = new File(media.getPath()).isFile(); // --> all medias return true

void transferAllMediasToSecondActivity(){
 Intent i = new Intent(FirstActivity.this, SecondActivity.class);
 i.putExtra("mediasList", mediasList);
 startActivity(i);
}

SecondActivity.java

Bundle extras = getIntent().getExtras();
mediasList = (ArrayList<Media>) getIntent().getExtras().get("mediasList");

for(int i = mediasList.size; i <= 0; i--)
 {
    Media media = mediasList.get(i);
    File mediaFile = new File(media.getPath());
    boolean hasMedia = mediaFile.isFile(); // --> all medias return false. All medias are not reading

    // -> throw FileNotFound exception call this function 
    BitmapFactory.decodeFile(mediaFile.getAbsolutePath(), new BitmapFactory.Options()); 
}

My existed media first path :

  • /storage/emulated/0/DCIM/Screenshots/Screenshot_20190712-123350.jpg

  • at MyFragment new File(media.getPath()).isFile() : true

  • at FirstActivity new File(media.getPath()).isFile() : true

  • at SecondActivity new File(media.getPath()).isFile() : false and decodeFile function throw FileNotFoundException

Exception:

Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/MyApp/MyAppImage/IMAGE20190711_182337871.jpg(No such file or directory)

FirstFragment mediasList logs:

media[0] : com.android.android.conversation.attachment.gallery.model.Media@2ee333c9 media[1] : com.android.android.conversation.attachment.gallery.model.Media@2ee333b1 media[2]: com.android.android.conversation.attachment.gallery.model.Media@2ee333b0

media[0].getPath() : /storage/emulated/0/MyApp/MyAppImage/IMAGE20190711_182337871.jpg media[1].getPath() : /storage/emulated/0/MyApp/MyAppImage/IMAGE20190711_182334910.jpg media[2].getPath() : /storage/emulated/0/MyApp/MyAppImage/IMAGE20190716_170439762.jpg

FirstActivity and SecondActivity mediasList logs:

media[0] : com.android.android.conversation.attachment.gallery.model.Media@2ee333c9 media[1] : com.android.android.conversation.attachment.gallery.model.Media@2ee333b1

media[0].getPath() : /storage/emulated/0/MyApp/MyAppImage/IMAGE20190711_182337871.jpg media[1].getPath() : /storage/emulated/0/MyApp/MyAppImage/IMAGE20190711_182334910.jpg

propoLis
  • 1,229
  • 1
  • 14
  • 48

2 Answers2

1

Try changing the way you get the path from the fragment from:

while (cursor != null && cursor.moveToNext()) {

    Uri uri = Uri.withAppendedPath(contentUri, cursor.getString(cursor.getColumnIndexOrThrow(Images.Media._ID)));
    String path = cursor.getString(cursor.getColumnIndexOrThrow(Images.Media.DATA));  
    mediasList.add(new Media(uri, path));
}

to

while (cursor != null && cursor.moveToNext()) {

    Uri uri = Uri.withAppendedPath(contentUri, cursor.getString(cursor.getColumnIndexOrThrow(Images.Media._ID)));
    String path = uri.getPath();  
    mediasList.add(new Media(uri, path));
}
Frandall
  • 378
  • 1
  • 13
1

I noticed that my parcelable Media is not parcing everywhere.

I found this solution:

Android E/Parcel﹕ Class not found when unmarshalling (only on Samsung Tab3)

  Bundle b = new Bundle();
  b.putParcelableArrayList(KEY_MEDIA, medias);
  intent.putExtra("bundle", b);

  Bundle b = getIntent().getBundleExtra("bundle");
  sendMediasList = b.getParcelableArrayList(KEY_MEDIA);

I used this and it is working fine. Very very thanks for your efforts @John Joe and @Frandall

propoLis
  • 1,229
  • 1
  • 14
  • 48