繁体   English   中英

尝试从“照片”应用中复制图像时出现java.io.FileNotFoundException

[英]java.io.FileNotFoundException when trying to copy a image from Photos app

我正在尝试使用指向我的PhotoGetFromGallery活动的共享选项从android Photos应用获取图像。 这是代码:

public void copy(File src, File dst) throws IOException {
    FileInputStream inStream = new FileInputStream(src);
    FileOutputStream outStream = new FileOutputStream(dst);
    FileChannel inChannel = inStream.getChannel();
    FileChannel outChannel = outStream.getChannel();
    inChannel.transferTo(0, inChannel.size(), outChannel);
    inStream.close();
    outStream.close();
}

public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

void handleSendImage(Intent intent) {
    Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
    String sourcePath = getRealPathFromURI(imageUri);
    if(isExternalStorageWritable()) {
        if (imageUri != null) {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();
            String destinationImagePath = sd + "/Pictures/MyAppImgFolder/";
            File source = new File(data, sourcePath);
            String fileName = source.getName();
            File destination = new File(sd, destinationImagePath + fileName);
            try {
                copy(source, destination);
            } catch (Exception e) {
                Log.e("COPY IMAGE ERROR", e.toString() + ". Destination Path is " + destinationImagePath.toString() + " and Source path is "+ sourcePath);
            }
        }
    }
}

sourcePath字符串返回正确的图像路径(例如/storage/emulated/0/Pictures/Instagram/IMG_20150413_114608.jpg )。 但是,我收到FileNotFoundException因为Environment.getDataDirectory()返回/data/storage/emulated/0/Pictures/Instagram/IMG_20150413_114608.jpg

这是我的日志:

E/COPY IMAGE ERROR﹕ java.io.FileNotFoundException: /data/storage/emulated/0/Pictures/Instagram/IMG_20150413_114608.jpg: open failed: ENOENT (No such file or directory). Destination Path is /storage/emulated/0/Pictures/MyAppImgFolder/ and Source path is /storage/emulated/0/Pictures/Instagram/IMG_20150413_114608.jpg

这是我的AndroidManifest.xml:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

我的问题是,如果可能的话,如何获取存储在“照片”应用或Android Gallery中的图像的任何路径?

您不应该使用getDataDirectory-您已经获得了一个完整的路径,以“ / storage”开头。使用仅传递sourcePath的单个参数File()构造函数,如下所示:

File sd = Environment.getExternalStorageDirectory();
String destinationImagePath = sd + "/Pictures/MyAppImgFolder/";
//sourcePath is already a full path name
File source = new File(sourcePath);
//redundant String fileName = source.getName();

File destination = new File(destinationImagePath + sourcePath);
//You are proposing many new subdirectories, so you must create them
destination.getParentFile()makeDirs();

//now you can continue with your copy attempt

我设法解决了这个问题。 遵循有效的代码。 感谢克里斯·斯特拉顿(Chris Stratton)在这件事上为我提供的指导。

File sd = Environment.getExternalStorageDirectory();
String destinationImagePath = sd + AppConstant.PHOTO_ALBUM;
File imagePath = new File(destinationImagePath);
File source = new File(sourcePath);
String fileName = source.getName();
File destination = new File(destinationImagePath + fileName);
   if (!imagePath.exists()) {
       imagePath.mkdirs();
       try {
            copy(source, destination);
            Toast.makeText(getApplicationContext(), "Success! File was copy from " + sourcePath + " to " + destinationImagePath, Toast.LENGTH_LONG).show();
       } catch (Exception e) {
            Log.e("COPY IMAGE ERROR", e.toString());
       }
   } else if(destination.exists() && !destination.isDirectory()){
       Toast.makeText(getApplicationContext(), "Image is already on your image folder", Toast.LENGTH_LONG).show();
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM