繁体   English   中英

java.io.FileNotFoundException:打开失败:ENOENT(没有这样的文件或目录)

[英]java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)

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

如果你试试:

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

我最终将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;
    }

暂无
暂无

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

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