【发布时间】:2016-06-30 05:58:16
【问题描述】:
我的手机中没有 SD 卡,我也希望使用我的应用程序的人可能拥有它,也可能没有它。现在,当我尝试分享我的屏幕截图时,它可以在少数手机中使用,但在少数手机中无法使用。在所有这些手机中,都没有 SD 卡。另外我想确保屏幕截图不应该保存在任何地方,这样它就不会在图库中可见。我尝试了以下方法:
- getExternalStorageState()
- getDatadirectory()
- getFilesDir()
- getDir()
- getCacheDir()
但我仍然无法找到正确的方法。这是我正在处理的当前代码:
view.setDrawingCacheEnabled(true);
view.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(view.getHeight(), View.MeasureSpec.EXACTLY));
view.layout( view.getLeft(), view.getTop(), view.getLeft() + view.getMeasuredWidth(), view.getTop() + view.getMeasuredHeight());
bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
view.destroyDrawingCache();
view.invalidate();
view.refreshDrawableState();
File path = new File(getApplicationContext().getDir("Default", Context.MODE_ENABLE_WRITE_AHEAD_LOGGING),"myapp");
File directory = new File(path.getAbsolutePath());
directory.mkdirs();
String filename = "myapp" + new Date() + ".png";
File yourFile = new File(directory, filename);
try
{
FileOutputStream out = new FileOutputStream(yourFile, true);
bitmap.compress(Bitmap.CompressFormat.PNG, 90,out);
out.flush();
out.close();
send(yourFile);
}catch (IOException e)
{
e.printStackTrace();
}
现在我的重点是在没有 SD 卡的手机上分享屏幕截图,它不应该保存在任何地方,这样屏幕截图就不会在我继续分享时不断转储。怎么办?
编辑: 现在我开始研究 getDir() 方法,以便将文件保存在应用程序数据中,正如我的朋友 Ashish Vora 所说。我发现文件被保存在里面,但没有被找回来共享。我得到的只是一个空白屏幕。
【问题讨论】:
-
如果您使用棉花糖,请参阅此答案.. Marshmallow issues..!!
-
关注那篇文章并编辑了我的代码,一开始它在请求许可,但在共享时仍然出现空白屏幕
标签: android share screenshot