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