我有一个功能,可以通过位图捕获视频的屏幕快照/视频帧,并将图像文件(.png)保存到Android手机上的“图片”目录中。但是,由于某些原因,将图像保存在手机上需要很长时间。例如,我将运行该应用程序/功能,并且该文件将在10分钟后显示在手机上。知道为什么吗?
public static Bitmap captureVideoFrame(Activity activity, VideoView vv, String viewSource, int currPosInMs)
{
MediaMetadataRetriever mmRetriever = new MediaMetadataRetriever();
Map<String, String> headers = new HashMap<String, String>();
mmRetriever.setDataSource(viewSource, headers);
Bitmap bmFrame = mmRetriever.getFrameAtTime(currPosInMs * 1000); // unit in microsecond
if(bmFrame == null){
Toast.makeText(activity.getApplicationContext(), "Bitmap is EMPTY. Curr Position: " + currPosInMs, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(activity.getApplicationContext(), "Bitmap is GOOD. Curr Position: " + currPosInMs, Toast.LENGTH_SHORT).show();
// Save file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + PICTURES_DIRECTORY + Utilities.getDayTimeString() + ".png";
OutputStream fout = null;
File imageFile = new File(mPath);
try
{
fout = new FileOutputStream(imageFile);
bmFrame.compress(Bitmap.CompressFormat.JPEG, 100, fout);
fout.flush();
fout.close();
Toast.makeText(activity.getApplicationContext(), "Saved file successfully!", Toast.LENGTH_SHORT).show();
}
catch(Exception e){
}
}
return bmFrame;
}
首先,您应该放fout.flush();。fout.close(); 在catch部分之后的final块中,以确保始终写入文件。
如果您正在通过Windows资源管理器查看USB连接,则可能是由于缓存造成的延迟(该文件实际上存在于磁盘上,但未显示)。我可以在三星设备上注意到这一点。如果您在电话上使用文件浏览器(例如,Astro文件管理器),则应该实时看到修改(在存储图片的目录上进行刷新,或者在目录上向上或向下导航)。
在finally块完成之后,记录以下内容-imageFile.exists()和imageFile.length(),以查看是否写入了文件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句