【问题标题】:java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)java.io.FileNotFoundException:打开失败:ENOENT(没有这样的文件或目录)
【发布时间】:2015-08-06 09:54:04
【问题描述】:

不确定我到底做错了什么,但在尝试将位图保存到 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("\\.(?=[^\\.]+$)");

【问题讨论】:

  • 我猜“/storage/emulated/0/storage/emulated/0/”应该是“/storage/emulated/0/”?您的拆分操作似乎没有按预期工作。
  • 也许你可以给我们一个mediapath的示例值?
  • fhissen 我看到这是错误,我如何从媒体路径中取出 /storage/emulated/0/Pictures/HelloCamera/VID_20150806_131011.mp4 然后只是这部分 VID_20150806_131011
  • 如果您确定 mediaPath 将是一个绝对路径: File f = new File(mediaPath);字符串名称 = f.getName(); int i = name.lastIndexOf("."); if(i > 0) name = name.substring(0, i);
  • 我想将其另存为 png 格式的文件名,因为它是视频的缩略图

标签: java android


【解决方案1】:

我最终将文件名传递给 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;
    }

【讨论】:

    【解决方案2】:

    如果你尝试:

    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
    

    【讨论】:

    • 我想将它另存为一个不同的文件名,一个 png,因为它是一个缩略图
    • MrT 尝试添加 png
    • 我将其拆分并添加 png。
    猜你喜欢
    • 1970-01-01
    • 2022-08-23
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多