首页 > 解决方案 > 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();
    }
}

}

标签: android

解决方案


推荐阅读