android - Android 仅在新安装后截屏并分享作品一次
问题描述
我有一个无法解决的奇怪问题,在接受外部存储写入权限后,我能够捕获线性布局的屏幕截图并通过其他应用程序的意图共享它。这只能工作一次(即使在通过 android studio 重新启动或重新安装应用程序后也不行)。我必须首先完全卸载它或关闭手机设置中的存储权限,以便它可以再次请求权限,然后才能更有效地授予权限。在第一次后重新运行活动的任何尝试都会返回此错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
创建活动:
checkPermission();
检查权限方法:
private void checkPermission() {
mRequestPermissionHandler.requestPermission(this, new String[]
{android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE
}, 123, new RequestPermissionHandler.RequestPermissionListener() {
@Override
public void onSuccess() {
//Toast.makeText(ActivityCompare.this, "request permission success", Toast.LENGTH_SHORT).show();
takeScreenShot();
}
@Override
public void onFailed() {
Toast.makeText(ActivityCompare_ScreenshotLayout.this, "request permission failed", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
mRequestPermissionHandler.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
private void takeScreenShot() {
try {
cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), "store");
if (!cacheDir.exists()) {
cacheDir.mkdirs();
}
//Get the bitmap from the linearlayout view
ll_screenshot.setDrawingCacheEnabled(true);
ll_screenshot.buildDrawingCache();
bm = ll_screenshot.getDrawingCache();
//End
path = new File(android.os.Environment.getExternalStorageDirectory(),"store") + "/screenshot.jpg";
Screenshot.savePic(bm, path);
Toast.makeText(getApplicationContext(), "Preparing to share..", Toast.LENGTH_SHORT).show();
shareFile();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
private void shareFile() {
String file_path = path.toString();
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
File fileWithinMyDir = new File(file_path);
if(fileWithinMyDir.exists()) {
intentShareFile.setType("image/*");
intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+file_path));
intentShareFile.putExtra(Intent.EXTRA_SUBJECT, "Share File..");
intentShareFile.putExtra(Intent.EXTRA_TEXT, "Share File..");
startActivity(Intent.createChooser(intentShareFile, "Share File"));
}
}
截图类:
public class Screenshot {
static Bitmap b;
public static Bitmap takeScreenShot(Activity activity) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
//Find the screen dimensions to create bitmap in the same size.
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay().getHeight();
b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
view.destroyDrawingCache();
return b;
}
public static void savePic(Bitmap b, String strFileName) {
FileOutputStream fos;
try {
fos = new FileOutputStream(strFileName);
b.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
解决方案
推荐阅读
- typescript - 为接口的 getter/setter 禁用 `@typescript-eslint/explicit-member-accessibility`
- reactjs - 钩子 onClick 调用的开玩笑测试
- r - 如何一个接一个地复制一些循环和功能
- typescript - 如何在 AssemblyScript 和 TypeScript 之间“共享”缓冲区
- javascript - 可以在网络应用程序中更改相机曝光吗?
- node.js - 为什么 await 在异步系列中不起作用?我该如何实施?
- r - 如果 ggplot 中有 2 个图例,图例框颜色会发生变化
- python - 尝试按名称将多组列堆叠成多个目标列
- javascript - 在单独的 git 上发布 Angular 库 dist 文件夹
- reactjs - 如何使用 Gsap 在 gartsby 中的页面之间添加动画