How do I save images in storage particular folder (gallery) & Get image from storage in above android 11 or below with java?

Viewed 53

As in my Drawing art project I want Save Painted images to storage or retrieval same image in my app Recycler view with Android Java & I got & save images from below android 11 but not from above 11 what should I do ??

Thankful if got help from someone & if I got solution than sure put in my answer....Thank-You

1 Answers

Hi First add below line to manifest in application tab

for make directory in android 10 & solve error in android 8

<application
     android:requestLegacyExternalStorage="true"
     android:hardwareAccelerated="false"
     android:largeHeap="true" />

& than try below code / method

in blank space set folder name of your wish and put this in res - values - string

<resources>
     <string name="app_folder_name">Your_Folder_name</string>
</resources>

Then add this method to your Activity.

private void saveToGallery() {
        String parentPath="";
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            // for above android 11
            parentPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + Environment.DIRECTORY_PICTURES + File.separator + getString(R.string.app_folder_name);
        }else{
            // for below android 11
            parentPath = Environment.getExternalStorageDirectory() + File.separator + getString(R.string.app_folder_name);
        }
        File parentFile = new File(parentPath);
        Log.e("TAG", "saveToGallery1: "+parentFile);
        if (!parentFile.exists()){
            parentFile.mkdirs();
        }
        File imageFile = new File(parentFile, "drawing"+System.currentTimeMillis() + ".png"); // Imagename.png

        FileOutputStream out = null;
        try {
            out = new FileOutputStream(imageFile);
            Bitmap bmp = binding.paintView.save();
            Common.DRAWING_BITMAP = bmp;
            bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // Compress Image
            out.flush();
            out.close();
            // Tell the media scanner about the new file so that it is  // immediately available to the user.
            MediaScannerConnection.scanFile(PaintDrawActivity.this, new String[]{imageFile.getAbsolutePath()}, null, new MediaScannerConnection.OnScanCompletedListener() {
                public void onScanCompleted(String path, Uri uri) {
                    Log.e("ExternalStorage1", "Scanned " + path + ":");
                    Log.e("ExternalStorage1", "-> uri=" + uri);
                }
            });
        } catch (Exception e) {
        e.printStackTrace();
        }
    }


private void getMyWorkImagesFromStorage() {
    File file;
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        // for above android 11
        file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), Environment.DIRECTORY_PICTURES + File.separator + getString(R.string.app_folder_name));
    } else {
        // for below android 11
        file = new File(Environment.getExternalStorageDirectory() + File.separator + getString(R.string.app_folder_name));
    }
    File[] files = file.listFiles();
    if (files != null) {
        for (File file1 : files) {
            if (file1.getPath().endsWith(".png") || file1.getPath().endsWith(".jpg")) {
                BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                Bitmap bitmap = BitmapFactory.decodeFile(file1.getAbsolutePath(), bmOptions);

                myWorkItems.add(new MyGalleryItem(bitmap, file1.getName()));
                Common.MY_GALLERY_IMAGES = myWorkItems;
            }
        }
        if (files.length == 0) {
            binding.tvEmpty.setVisibility(View.VISIBLE);
        } else {
            binding.tvEmpty.setVisibility(View.GONE);
        }
    }
}
Related