首先,我会说我读过许多其他与此问题相符的帖子,而且没有一个帖子适合我。
我正在我的设备上测试我的应用程序,运行Android L的Nexus 5.它还没有根植。 这个相同的代码适用于运行API 19的旧版Android。
我正在尝试截取屏幕截图并使用以下代码进行分享:
View screen = getWindow().getDecorView().getRootView();
screen.setDrawingCacheEnabled(true);
Bitmap bitmap = screen.getDrawingCache();
String filename = getScreenshotName();
String filePath = Environment.getExternalStorageDirectory().getPath()
+ File.separator + filename;
File imageFile = new File(filePath);
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] bitmapData = bos.toByteArray();
FileOutputStream fos = new FileOutputStream(imageFile);
fos.write(bitmapData);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
// share
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(filePath));
startActivity(Intent.createChooser(share, "Share Image"));
我在AndroidManifest.xml
中拥有这些权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
我收到了这个错误:
java.io.FileNotFoundException: /storage/emulated/0/2014-09-14.png: open failed: EACCES (Permission denied)
在这一行:
FileOutputStream fos = new FileOutputStream(imageFile);
我还尝试了其他10种方法来获取filePath,现在我怀疑这是一个设备/ Android L问题。
知道发生了什么事吗?