Home » Android » Android Studio how to create folder in internal mobile storage and store screenshot inside

Android Studio how to create folder in internal mobile storage and store screenshot inside

Posted by: admin November 1, 2017 Leave a comment

Questions:

I am trying to create a folder called “Scans” inside the app directory inside internal storage. In this folder I will be storing screenshots as photos when clicking on button.
There is no error but the issue is the that no folder is created and no screenshot is stored.
I don’t know hat is the issue. Help please.

        floatButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bitmap b = getScreenShot(rootContent);
            //break;
            Date currentTime = Calendar.getInstance().getTime();
            File saveFile = getMainDirectoryName(cwebview.this); //get the path to save screenshot
            File file = store(b, currentTime.toString() + ".jpg", saveFile);//save the screenshot to selected path
            Toast.makeText(cwebview.this,"Scan is Saved",Toast.LENGTH_SHORT).show();


        }
    });
}

public static Bitmap getScreenShot(View view) {
    View screenView = view.getRootView();
    screenView.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
    screenView.setDrawingCacheEnabled(false);
    return bitmap;
}


/*  Create Directory where screenshot will save for sharing screenshot  */
public static File getMainDirectoryName(Context context) {
    String folder = "Scans";
    //Here we will use getExternalFilesDir and inside that we will make our Demo folder
    //benefit of getExternalFilesDir is that whenever the app uninstalls the images will get deleted automatically.
    File mainDir = new File(
            context.getFilesDir(), folder);

    //If File is not present create directory
    if (!mainDir.exists()) {
        if (mainDir.mkdir())
            Log.e("Create Directory", "Main Directory Created : " + mainDir);
    }
    return mainDir;
}

/*  Store taken screenshot into above created path  */
public static File store(Bitmap bm, String fileName, File saveFilePath) {
    File dir = new File(saveFilePath.getAbsolutePath());
    if (!dir.exists())
        dir.mkdirs();
    File file = new File(saveFilePath.getAbsolutePath(), fileName);
    try {
        FileOutputStream fOut = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
        fOut.flush();
        fOut.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return file;
}
Answers: