gpt4 book ai didi

android - 屏幕截图文件需要很长时间才能保存在 Android 设备上

转载 作者:行者123 更新时间:2023-11-29 00:14:43 25 4
gpt4 key购买 nike

我有一个功能,可以通过位图捕获视频的屏幕截图/视频帧,并将图像文件 (.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 部分之后的 finally block 中,以确保始终写入文件。

如果您正在通过 Windows 资源管理器查看 USB 连接,则可能由于缓存而出现延迟(文件实际上存在于磁盘上但未显示)。我可以在三星设备上注意到这一点。如果您在手机上使用文件资源管理器(例如 Astro 文件管理器),您应该可以实时看到修改(刷新您存储图片的目录,或在目录中上下导航)。

在 finally block 完成后记录以下 - imageFile.exists() 和 imageFile.length() 以查看文件是否已写入。

关于android - 屏幕截图文件需要很长时间才能保存在 Android 设备上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27576468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com