0

I have a strange problem which I cannot resolve, I am able to capture screenshot of linear layout and share it through an intent on other apps after accepting the External Storage Write Permissions. This only works once (not even after restarting or re-installing the app through android studio). I have to first unistall it completly or turn off storage permission in phone settings to that it can ask for permissions again then it works fater granting. On any attemtps to rerun the activity after the first time returns this error:

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

Activity onCreate:

checkPermission();

checkPermission Method:

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

Screenshot class:

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

}

user3560827
  • 632
  • 1
  • 5
  • 22
  • The bitmap bm you are passing to savePic is null, check with the debugger – Gastón Saillén Sep 23 '18 at 00:20
  • 1
    Possible duplicate of [Android View.getDrawingCache returns null, only null](https://stackoverflow.com/questions/2339429/android-view-getdrawingcache-returns-null-only-null) – TheWanderer Sep 23 '18 at 00:51
  • @TheWanderer Thank you, your link saved my problem (although why it ran only on clean first time install i will never know) , thanks a million again!!! – user3560827 Sep 23 '18 at 06:55

0 Answers0