尝试从Photos应用程序复制图像时出现java.io.FileNotFoundException

时间:2015-04-17 19:36:49

标签: java android

我试图使用指向我的PhotoGetFromGallery活动的共享选项从Android照片应用获取图像。这是代码:

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图库中存储任何图像路径?

2 个答案:

答案 0 :(得分:0)

你不应该使用getDataDirectory - 你已经被提交了一个完整的路径,从“/ storage”开始使用单个参数File()构造函数只传递sourcePath,如下所示:

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

答案 1 :(得分:0)

我设法解决了这个问题。遵循有效的代码。感谢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();
   }