简体   繁体   中英

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

Not sure what I am doing wrong exactly but keep getting this error while trying to save the bitmap into a png file:

java.io.FileNotFoundException: /storage/emulated/0/storage/emulated/0/Pictures/HelloCamera/VID_20150806_124818.png: open failed: ENOENT (No such file or directory)

  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;
 }

The error seems to be pointed out being the repeat in directory /storage/emulated/0/

How do I take out the second one from this... what regex I tried this:

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

If you try:

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

you will get the last token you want.

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

I ended up passing in the fileName to getVideoThumb thanks @fhissen for making me aware of the duplicate path... code looks like this now...

 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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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