首页 > 解决方案 > java.io.FileNotFoundException:没有这样的文件或目录

问题描述

我已经读取了片段中光标处的所有图像文件,并传输了第一个有任何问题的活动,但是当跳转另一个活动时,虽然文件已经存在,但没有读取相同的文件路径。

片段设置所有媒体数据并传输第一个活动。第一个活动正在查看所有媒体文件的现有状态并且所有文件都返回 true,但是如果第一个活动传输这些文件,则第二个活动不会读取相同的文件,并且当调用文件操作 (BitmapFactory.decodeFile) 时会抛出 FileNotFoundException。

isFile()函数只提供检查是否有文件。

媒体型号:

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()); 
}

我现有的媒体第一路径:

例外:

无法解码流:java.io.FileNotFoundException:/storage/emulated/0/MyApp/MyAppImage/IMAGE20190711_182337871.jpg(没有这样的文件或目录)

FirstFragment mediasList 日志:

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

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

FirstActivity 和 SecondActivity mediasList 日志:

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

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

标签: androidfilefilenotfoundexception

解决方案


尝试更改从片段中获取路径的方式:

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));
}

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));
}

推荐阅读