java.io.FileNotFoundException:open failed:ENOENT(没有这样的文件或目录)

时间:2015-08-06 09:54:04

标签: java android

不确定我做错了什么,但在尝试将位图保存到png文件时不断出现此错误:

  

java.io.FileNotFoundException:   /storage/emulated/0/storage/emulated/0/Pictures/HelloCamera/VID_20150806_124818.png:   打开失败:ENOENT(没有这样的文件或目录)

  private File getVideoThumb(String mediaPath, Uri videoUri) {
    Bitmap bmThumbnail;
    bmThumbnail = ThumbnailUtils.createVideoThumbnail(mediaPath,   MediaStore.Video.Thumbnails.MINI_KIND);
    File fPath = Environment.getExternalStorageDirectory();
    String[] tokens = mediaPath.split("\\.(?=[^\\.]+$)");
    File f = null;
    f = new File(fPath, tokens[0] + ".png");
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(f);
        bmThumbnail.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
        // PNG is a lossless format, the compression factor (100) is ignored
    } catch (Exception e) {
        Log.d(Constants.DEBUG, "ERROR saving the compressed bitmap " + e);
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.flush();
                out.close();
            }
        } catch (IOException e) {
            Log.d(Constants.DEBUG, "ERROR closing out stream for file for bitmap");
            e.printStackTrace();
        }
    }
     return f;
 }

错误似乎被指出是目录/storage/emulated/0/

中的重复

我如何从这个中取出第二个...我尝试了什么正则表达式:

int index = mediaPath.lastIndexOf("\\");
String fileName = mediaPath.substring(index + 1);
String[] tokens = fileName.split("\\.(?=[^\\.]+$)");

2 个答案:

答案 0 :(得分:0)

如果您尝试:

File f = null;
f = new File(mediaPath);
System.out.println(f.getName().split("\\.")[0] + ".png");

您将获得所需的最后一个标记。

Input: /storage/emulated/0/Pictures/HelloCamera/VID_20150806_131011.mp4
Output: VID_20150806_131011.png

答案 1 :(得分:0)

我最终将fileName传递给getVideoThumb,感谢@fhissen让我意识到重复的路径......代码现在看起来像这样......

 private File getVideoThumb(String mediaPath, String mediaName, Uri videoUri) {
        Bitmap bmThumbnail;

//MINI 512x334
        bmThumbnail = ThumbnailUtils.createVideoThumbnail(mediaPath, MediaStore.Video.Thumbnails.MINI_KIND);

        File fPath = Environment.getExternalStorageDirectory();


        String[] tokens = mediaName.split("\\.(?=[^\\.]+$)");
        File f = null;

        f = new File(fPath, tokens[0] +".png");
        FileOutputStream out = null;
        try {


            out = new FileOutputStream(f);
            bmThumbnail.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
            // PNG is a lossless format, the compression factor (100) is ignored
        } catch (Exception e) {
            Log.d(Constants.DEBUG, "ERROR saving the compressed bitmap " + e);
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.flush();
                    out.close();
                }
            } catch (IOException e) {

                Log.d(Constants.DEBUG, "ERROR closing out stream for file for bitmap");
                e.printStackTrace();
            }
        }

        return f;
    }