Android仅在全新安装后才截图并共享作品

时间:2018-09-22 23:29:11

标签: android

我有一个无法解决的奇怪问题,在接受外部存储写入权限后,我可以捕获线性布局的屏幕截图并通过其他应用程序的意图来共享它。这仅工作一次(即使在通过android studio重新启动或重新安装该应用程序之后也是如此)。我必须首先完全取消安装它,或者关闭电话设置中的存储权限,以使其可以再次请求权限,然后才能进行授权授予。在任何第一次尝试重新运行活动的attemtps上都会返回此错误:

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

活动onCreate:

checkPermission();

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();
    }
}

}

0 个答案:

没有答案