java.io.FileNotFoundException:没有这样的文件或目录

时间:2019-07-24 06:53:14

标签: android file 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()); 
}

我现有的媒体优先路径:

  •   

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

  •   

    在MyFragment new File(media.getPath()).isFile()上:true

  •   

    在FirstActivity new File(media.getPath()).isFile()上:true

  •   

    在SecondActivity new File(media.getPath()).isFile()上:错误,decodeFile函数抛出FileNotFoundException

例外:

  

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

FirstFragment mediasList日志:

  

media [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

     

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和SecondActivity媒体列表日志:

  

media [0]:   com.android.android.conversation.attachment.gallery.model.Media@2ee333c9   媒体[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

3 个答案:

答案 0 :(得分:1)

为您的SecondActivity尝试此操作

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

for(int i = mediasList.size; i <= 0; i--)
 {
    boolean hasMedia = new File(mediasList.get(i).getPath()).isFile(); 
 }

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

我注意到我的可包裹介质没有在所有地方都进行修剪。

我找到了这个解决方案:

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

我使用了它,并且工作正常。非常非常感谢您的@John Joe和@Frandall