【发布时间】:2014-04-17 04:10:07
【问题描述】:
我正在尝试截取活动的屏幕截图,以便在图库中看到它。但是代码不起作用,并且图像没有显示在图库中。我在按钮单击事件中调用方法 takePicSave()。 同样在清单文件中,权限也在那里。
private void takePicSave()
{
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
}
public Bitmap takeScreenshot(){
View rootview = findViewById(android.R.id.content).getRootView();
rootview.setDrawingCacheEnabled(true);
return rootview.getDrawingCache();
}
public void saveBitmap(Bitmap bitmap){
Date date = new Date() ;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
File imagePath = new File(Environment.getExternalStorageDirectory() , dateFormat.format(date) + " .png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://" + Environment.getExternalStorageDirectory())));
} catch (FileNotFoundException e) {
Log.e("Error " , e.getMessage(), e);
} catch (IOException e) {
Log.e("Error ", e.getMessage(), e);
}
}
}
我得到的错误 “java.io.FileNotFoundException/mnt/sdcard/2014-04-16.png(权限被拒绝)
【问题讨论】:
-
您的清单中有读写权限吗?
-
目前我只有在 manifest 中有写权限
-
图像在 android 2.2、4.0 和 4.1 的图库中显示,但在 4.4、2.3 和 2.2 上失败。为什么它对某些人起作用而不对其他人起作用?请帮我理解
标签: android