I have been searching for it but theres no any answer to my problem. I am programming an app and I want to delete an external folder. For example /storage/emulated/0/MyFolder, there is a lot of ways to create and read files from a internal app folder and an external app folder, but I dont know how to acces to files in "/storage/emulated/0/...".
Thanks.
public static void deleteDir(Context ctx) {
try {
File myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator
+ "MyFolder");
}
if (myFile.exists()) {
deleteRecursive(myFile);
}
}catch (Exception ignored){
Log.e("Delete error File: %s",ignored.getMessage());
}
}
private static void deleteRecursive(File myFile) {
if (myFile.isDirectory())
for (File child : myFile.listFiles())
deleteRecursive(child);
Log.e("MyFolder Files Deleted!!! : %s", myFile.delete());
}
Add this lines to app manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Update
- As mentioned by CommonsWare runtime permission request needed for Android 6+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
&& ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 101);
} else {
deleteDir(context);
}
Your Activity
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case 101:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
deleteDir(context);
}
}
}
new File("/storage/emulated/0/MyFolder").delete();
# your manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Update: As mentioned by CommonsWare hardcoded filesystem paths might be invalid for some versions of Android.
new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/MyFolder").delete();
Related
i am trying to create a video player ,So I am trying to add the videos to the list
Storage permission is required to fetch the videos, so I took the permission with the below code.
But playstore was reject My app for this MANAGE EXTERNAL STORAGE permission.
But without this permission, I can't get storage permission on Android 10+ device.
To change the name of the video, delete the video and download the video permission is required , so please help me , please tell me how to get storage permission (/storage/Media/Videos , /storage/Download/)
My storage permission code :-
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
android:requestLegacyExternalStorage="true"
Main activity code :-
private boolean checkPermission() {
if (SDK_INT >= Build.VERSION_CODES.R) {
return Environment.isExternalStorageManager();
} else {
int result = ContextCompat.checkSelfPermission(PermissionActivity.this, READ_EXTERNAL_STORAGE);
int result1 = ContextCompat.checkSelfPermission(PermissionActivity.this, WRITE_EXTERNAL_STORAGE);
return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED;
}
}
private void requestPermission() {
if (SDK_INT >= Build.VERSION_CODES.R) {
try {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse(String.format("package:%s",getApplicationContext().getPackageName())));
startActivityForResult(intent, 2296);
} catch (Exception e) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivityForResult(intent, 2296);
}
} else {
//below android 11
ActivityCompat.requestPermissions(PermissionActivity.this, new String[]{WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2296) {
if (SDK_INT >= Build.VERSION_CODES.R) {
if (Environment.isExternalStorageManager()) {
// perform action when allow permission success
} else {
Toast.makeText(this, "Allow permission for storage access!", Toast.LENGTH_SHORT).show();
}
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0) {
boolean READ_EXTERNAL_STORAGE = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean WRITE_EXTERNAL_STORAGE = grantResults[1] == PackageManager.PERMISSION_GRANTED;
if (READ_EXTERNAL_STORAGE && WRITE_EXTERNAL_STORAGE) {
// perform action when allow permission success
} else {
Toast.makeText(this, "Allow permission for storage access!", Toast.LENGTH_SHORT).show();
}
}
break;
}
}
So please tell me how to take storage permission in Android10+ Devices and also below Android 10 devices with out using MANAGE EXTERNAL STORAGE permission , Please Help Me
Permissions have changed in Android 10+:
External storage access scoped to app files and media
By default, apps targeting Android 10 and higher are given scoped access into external storage, or scoped storage. Such apps can see the following types of files within an external storage device without needing to request any storage-related user permissions [..]
Source: Privacy changes in Android 10
On devices that run Android 10 or higher, you don't need any storage-related permissions to access and modify media files that your app owns, including files in the MediaStore.Downloads collection
Source: Storage Permissions
If you have
android:requestLegacyExternalStorage="true"
in application tag in manifest file then you are done for an Android Q/10 device.
It will behave as it behaved on before 10.
I do not understand why you would have any problem on Android 10.
On Android 11+ you should be able to see media files in the usual public directories.
I want to save my captured images to a specific directory (/sdcard/DCIM/FokusStacker) with the following method. I have tried different Locations, but none of them worked.
private void capturePicture(){
File dir = new File("/sdcard/DCIM/FokusStacker");
String fileName = "IMG_"+ System.currentTimeMillis();
File file = new File(dir,fileName);
Log.d(TAG, "capturePicture: DIRECTORY: "+dir.getAbsolutePath());
ImageCapture.OutputFileOptions outputFileOptions =
new ImageCapture.OutputFileOptions.Builder(file).build();
imageCapture.takePicture(outputFileOptions, ContextCompat.getMainExecutor(this), new ImageCapture.OnImageSavedCallback() {
#Override
public void onImageSaved(#NonNull ImageCapture.OutputFileResults outputFileResults) {
Log.d(TAG, "onImageSaved: SAVED");
return;
}
#Override
public void onError(#NonNull ImageCaptureException exception) {
Log.d(TAG, "onError: FAILED");
return;
}
});
}
These are the Permission i've included:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
And i also tried saving it to the internal storage.
Code Snippet for to get the local directoryof the App for the file:
File dir = getApplicationContext().getFilesDir();
Can anyone please guide me how to do this?
I am a little new to android so please, I would appreciate if I can have a detailed procedure.
I should generate a .pdf file inside the android data folder to use the Java code below, with the permissions enabled in the XML manifest file. But when I run the code I have the following exception. The application has different permissions within the manifest, It should all be configured correctly, I state that the application I'm testing on an old Android 4. How can I solve this? and what is it due to?
Exception: error: java. I. FileNotFoundException: /data/my.pdf:
open failed: EACCES (Permission denied)
Code:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE " />
public static Boolean GeneratePDF(String base64) {
Boolean ret = true;
try {
String direttorio=""+Environment.getDataDirectory().getAbsolutePath();
final File dwldsPath = new File(direttorio + "/" + "my.pdf");
byte[] pdfAsBytes = Base64.decode(base64, 0);
FileOutputStream os;
os = new FileOutputStream(dwldsPath, false);
os.write(pdfAsBytes);
os.flush();
os.close();
} catch (Exception ex) {
System.out.println("\n Errore Generazione File: "+ex);
ret = false;
}
return ret;
}
you have to give Write permission at run time.
It can be achieved something as following...
public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback{
private static final int REQUEST_WRITE_PERMISSION = 111;
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_WRITE_PERMISSION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
GeneratePDF("your String name");
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermission();
}
private void requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION);
} else {
GeneratePDF("your String name");
}
}
}
Replace this line
String direttorio=""+Environment.getDataDirectory().getAbsolutePath();
To:
String direttorio= Environment.getExternalStorageDirectory().getAbsolutePath();
final File dwldsPath = new File(direttorio + "/" + my.pdf");
if you're using Android 6.0 and above,
there could be 2 ways:
1 if you're making only for demo purpose you can manually give permission to app
by going in settings->apps->permissions. Then allow all permission which are
required.
2 you've to implement runtime permissions so that user can allow it runtime.
I am new to android and I am working in an android existing project.The app is crashing on android version >6.0, with below exception.Basically app is selecting photo from gallery which is working fine for the first time and on second time onwards the app is crashing giving permission denial exception.
java.lang.SecurityException: Permission Denial: reading
com.google.android.apps.photos.contentprovider.MediaContentProvider
uri
content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F1022/ORIGINAL/NONE/256350537
from pid=7789, uid=10145 requires the provider be exported, or
grantUriPermission()
I have gone through few links and checked that android has introduce run time permissions and I have used below code to check the runtime permission.
The things I have tried so far...
Added permission in manifest.
2.Checking the runtime permission from code.
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
Log.d("Enter", "onRequestPermissionsResult: ");
switch (requestCode){
case REQUEST_CODE_PERMISSION:{
Map<String,Integer> perms = new HashMap<>();
//Initialize the map with the permissions
perms.put(Manifest.permission.ACCESS_COARSE_LOCATION,PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.CAMERA,PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE,PackageManager.PERMISSION_GRANTED);
// perms.put(Manifest.permission.READ_USER_DICTIONARY,PackageManager.PERMISSION_GRANTED);
//Fill with actual results from user
if (grantResults.length > 0){
for (int i = 0 ; i < permissions.length ; i++){
perms.put(permissions[i],grantResults[i]);
//check for all permissions
if (perms.get(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
Log.d("Permission Granted", "onRequestPermissionsResult: ");
}else{
Log.d("Some", "onRequestPermissionsResult: ");
//if (perms.get(Manifest.permission.ACCESS_COARSE_LOCATION))
if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CAMERA)
|| ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.ACCESS_COARSE_LOCATION)
|| ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_EXTERNAL_STORAGE)){
new DialogInterface.OnClickListener(){
#Override
public void onClick (DialogInterface dialog, int which){
switch (which){
case DialogInterface.BUTTON_POSITIVE:
checkAndRequestPermission();
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
}else{
Toast.makeText(this,"Go to Settings and enable Permissions",Toast.LENGTH_LONG).show();
}
}
}
}
}
}
}
private void showDialogOK(String message, DialogInterface.OnClickListener okListener){
new AlertDialog.Builder(this)
.setMessage(message)
.setPositiveButton("OK",okListener)
.setNegativeButton("Cancel",okListener)
.create()
.show();
}
}
And the line where it is crashing is :-
if (checkAndRequestPermission()){
InputStream fis = getContentResolver().openInputStream(Uri.parse(url)); //Crashing Line
BitmapFactory.decodeStream(fis, null, o);
fis.close();
}
Below are the permissions used in My Manifest:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_USER_DICTIONARY"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
<uses-permission android:name="com.google.android.apps.photos.permission.GOOGLE_PHOTOS"/>
<!-- <uses-permission android:name="com.google.android.apps.photos.permission.GOOGLE_PHOTOS"/>-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.google.android.apps.photos.permission.GOOGLE_PHOTOS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-feature android:name="android.hardware.location" android:required="true" />
<uses-feature android:name="android.hardware.location.gps" android:required="false" />
The problem is not with your manifest permissions, but rather the URI being used. How did you get the URI? The logcat output tells you that the URI is for a content provider which was not exported or that the URI was not provided in an Intent which granted temporary access to the ContentProvider.
After a lot of research I was able to fix the issue.The error was causing since the google photos needs a persistable URI of images,even if you have all codes for runtime permission and flags in Manifest file.
With the help of below answer I have fixed my issue.
https://stackoverflow.com/a/29588566/1842304
Note down all your permission that are working in manifest below android M. We dont need any additional permission for android M. We have to just request at runtime.
And request your permission like this. Change your permission(only dangerous permissions) as they are in manifest. Put this in onCreate
/// granting permission ////
if(!checkPermission())
{
requestPermission();
}
/////////////////////////////
And in class add these
/////////////////////// permission for marshmallow ///////////////////
private boolean checkPermission(){
int result1 = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
int result2 = ContextCompat.checkSelfPermission(this, Manifest.permission.MODIFY_PHONE_STATE);
int result3 = ContextCompat.checkSelfPermission(this, Manifest.permission.PROCESS_OUTGOING_CALLS);
int result4 = ContextCompat.checkSelfPermission(this, Manifest.permission.PROCESS_INCOMING_CALLS);
int result5 = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
if (result1 == PackageManager.PERMISSION_GRANTED && result2 == PackageManager.PERMISSION_GRANTED &&
result3 == PackageManager.PERMISSION_GRANTED && result4 == PackageManager.PERMISSION_GRANTED
&& result5 == PackageManager.PERMISSION_GRANTED){
return true;
} else {
//Toast.makeText(this,"You don't have permission to use further features",Toast.LENGTH_LONG).show();
return false;
}
}
private void requestPermission(){
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_PHONE_STATE) &&
ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.MODIFY_PHONE_STATE) &&
ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.PROCESS_OUTGOING_CALLS) &&
ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.PROCESS_INCOMING_CALLS) &&
ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)){
Toast.makeText(this,"Application needs permission to use your camera, calls, storage and location.",Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_PHONE_STATE,
Manifest.permission.MODIFY_PHONE_STATE, Manifest.permission.PROCESS_OUTGOING_CALLS,
Manifest.permission.PROCESS_INCOMING_CALLS, Manifest.permission.CALL_PHONE},1);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED &&
grantResults[1] == PackageManager.PERMISSION_GRANTED &&
grantResults[2] == PackageManager.PERMISSION_GRANTED &&
grantResults[3] == PackageManager.PERMISSION_GRANTED &&
grantResults[4] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this,"Permission Granted.",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this,"Permission Denied.",Toast.LENGTH_LONG).show();
}
break;
}
}
////////////////////////////////////////////////////////////////////////
Try this Solution
in your OnCreate() method
#Override
protected void onCreate(Bundle savedInstanceState)
{
// your code
.................
// call dynamic permission
check_SD_CARD_Permissions();
}
public void check_SD_CARD_Permissions()
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int permissionCheck = this.checkSelfPermission("Manifest.permission.WRITE_EXTERNAL_STORAGE");
permissionCheck += this.checkSelfPermission("Manifest.permission.READ_EXTERNAL_STORAGE");
if (permissionCheck != 0) {
this.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1001); //Any number
}
}else{
Log.d(TAG, "checkBTPermissions: No need to check permissions. SDK version < LOLLIPOP.");
}
}
in AndroidManifest.xml
add these permissions
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
and targetSdkVersion must be 23
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="23" />
Build your project with
Version : Android 6.0
and finally in project.properties target must be 23
target=android-23
it's working for me
I want to use the file explorer in Android system to show the file I saved before.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 1);
So, I think saving the data into SD card is the only way to achieve this. I've found many materials, but I always get the error that I don't have the permission to save the data into SD card. The error message is as follow.
01-17 23:35:44.184 6337-6337/? W/System.err: java.io.FileNotFoundException: /storage/emulated/0/rtd: open failed: EACCES (Permission denied)
01-17 23:35:44.184 6337-6337/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
01-17 23:35:44.185 6337-6337/? W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
01-17 23:35:44.185 6337-6337/? W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
In fact I do add the permission lines in the AndroidManifest
</activity>
</application>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
So, can anyone help me figure out what I should do to fix this error?
Here is the code of my saving button
class ButtonClickListener2 implements View.OnClickListener{
#Override
public void onClick(View v){
// Intent intent = getIntent();
Name = getIntent().getStringExtra("Name");
Age = getIntent().getStringExtra("Age");
Movie = getIntent().getStringExtra("Movie");
File2 = getIntent().getStringExtra("File");
TextView txt = (TextView)findViewById(R.id.textView3);
txt.setText(Name+" "+ Age+ " "+Movie+" "+ File2);
String filename = File2.toString();
String Age_Content = Age.toString();
String Movie_Content = Movie.toString();
String Name_Content = Name.toString();
FileService service = new FileService(getApplicationContext());
try {
if(Environment.getExternalStorageState().equals(Environment.getExternalStorageState())){
service.save(filename, Age_Content, Movie_Content,Name_Content);
Toast.makeText(getApplicationContext(),R.string.SDCard_available,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),R.string.SDCard_protected,Toast.LENGTH_SHORT).show();
}
Toast.makeText(getApplicationContext(),R.string.success,Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),R.string.fail,Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
class FileService {
private Context context;
public FileService (Context context) {
this.context = context;
}
public void save(String filename, String Age_content, String Movie_content, String Name_content) throws Exception{
//save the data into SD card
/* FileOutputStream outputStream = context.openFileOutput(filename,Context.MODE_APPEND);
outputStream.write(Age_content.getBytes());
outputStream.write(Movie_content.getBytes());
outputStream.write(Name_content.getBytes());
outputStream.close();*/
File file = new File(Environment.getExternalStorageDirectory(),filename);
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(Age_content.getBytes());
outputStream.write(Movie_content.getBytes());
outputStream.write(Name_content.getBytes());
outputStream.close();
}
}
If you're using Android M, try this:
private final String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
requestPermissions(permissions, 2909);
Also, override the onRequestPermissionsResult method as follows:
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
Your activity must extend AppCompatActivity in order for this to work.
Android has introduced runtime permissions with version 6.0. You might want to look into it.
In your manifests file put the below code
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Check whether you used PERMISSIONS in AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />