0

Just like Whatsapp, Telegram does, I want to create a folder "DemoApp" and save .jpg images inside it in Internal storage Environment.getExternalStorageDirectory().getPath() of Android 11 as per scoped storage.

I have already given following permission in AndroidManifest.xml file.

uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />

And also written code to grant Read/Write Permission of folders from user.

if (ContextCompat.checkSelfPermission(SplashActivity.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            Log.i(TAG, "LOG, Permission to record denied");

ActivityCompat.requestPermissions(SplashActivity.this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    REQUEST_WRITE_STORAGE);

But still after user grants folder permissions, the folder is been created, but the Images are not being saved in the 'DemoApp' folder.

Following is the code which is working till Android 10 but not in Android 11:

File localFile;

final String folderLocation= Environment.getExternalStorageDirectory() + "/" + "DemoApp" + "/";

        //  userImage.setImageDrawable(postOwnerImage.getContext().getDrawable(R.drawable.profilepic));
        
        localFile = new File(folderLocation);
      
            localFile.mkdirs();

            final File file = new File(folderLocation+ "/" + "Picture1" + ".jpg");
                if (file.exists()) {
                    Picasso.get().load(file).resize(1024, 800).noPlaceholder().onlyScaleDown().into(postOwnerImage);
                } else {
                    StorageReference storageReference;
                    storageReference = FirebaseStorage.getInstance().getReference();

                    String STORAGE_PATH_DOWNLOADS = "ImageFolder" + "/";   // backslash to refer folder in firebase Storage
                    final File downloadFile = new File(folderLocation + "/" + "Picture1" + ".jpg");
                    storageReference.child(STORAGE_PATH_DOWNLOADS +
                             "Picture1.jpg").getFile(downloadFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                            if (downloadFile.exists()) {
                                Picasso.get().load(downloadFile).resize(1024, 800).noPlaceholder().onlyScaleDown().centerCrop().into(postImage);
                            } else {
                            }
                        }
                    });

                }
Sandeep
  • 455
  • 4
  • 26
  • For the next few months, use `android:allowLegacyExternalStorage="true"` in your `` in the manifest. After that, what you are seeking is no longer supported. Or, you could use `ACTION_OPEN_DOCUMENT_TREE` and let the *user* decide where on the *user's* device your app should be putting the *user's* content. – CommonsWare Apr 14 '21 at 19:56
  • What you are suggesting will not work for Android 11 users who newly installs the app. – Sandeep Apr 16 '21 at 12:16
  • Both of my solutions will work for Android 11 users who newly install the app. `android:allowLegacyExternalStorage="true"` will only work until your `targetSdkVersion` reaches 30, and you will be forced into doing that within months, perhaps weeks. So, of the two, I recommend `ACTION_OPEN_DOCUMENT_TREE`, which has been around for ~7 years. – CommonsWare Apr 16 '21 at 12:18

0 Answers0