【发布时间】: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 格式的文件名,因为它是视频的缩略图