I'm trying to read a json file from the SDCard in the Phone.(SAMSUNG SM-G532M).
But I can't.
I want to put the file in "Downloads" folder, and make the app to look in that folder in particular, for a particular filename.
But i get a FileNotFoundException.
When I debug the application, the path is different from what i spected.
I get "/storage/emulated/0", but I want to read the Download Folder in the SDCARD.
When i use this sentece:
ruta_sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
and I debug the value, it's ponting to :
/storage/emulated/0/Download
When I try to navigate with Device File Explorer, i get "Opendir Failed: Permission Denied"
What i'm doing wrong ?
I added this line to manifest.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE">
</uses-permission>
notes:
Developing un Android Studio 3.2
Phone : Samsung : SM-G532M ( not emulated )
Thanks in advance!
Best Regards
You need to request the runtime.
public static final int READ_EXTERNAL_STORAGE = 112;
protected void readSDcardDownloadedFiles() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_EXTERNAL_STORAGE);
} else {
//Permission is granted
//Call the method to read file.
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Read the files
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
}
Related
I added the permissions to write access to the external storage device in the Manifest, and granted them in my Android phone. I even ask for them at execution time if they don't exist.
However, I always get this exception:
java.io.FileNotFoundException: /storage/emulated/03b3d97bd-5186-4506-97dc-9994b7ce0761 (Permission denied)
Do you have an idea of why?
Code
try {
if (Build.VERSION.SDK_INT >= 23) {
int permissionCheck = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory() + image_uuid);
byte[] buffer = new byte[1024];
int bytesRead;
BitmapDrawable bitmapDrawable = (BitmapDrawable) temp.getDrawable();
etc. etc. etc.
And in the manifest file:
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You write file in sd card before you get permission.You must wait for the result of granting permission by using below code:
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE/*1 in here*/: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted
// write file here
} else {
// permission denied
}
return;
}
// other 'case' lines to check for other
// permissions this app might request.
}
}
see here
I have a problem creating directory on SD card.
AndroidManifest.xml contains neccessary permissions:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application...
Also for Android 6.0 I ask these permissions at runtime:
int permissionCheck1 = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
int permissionCheck2 = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck1 != PackageManager.PERMISSION_GRANTED || permissionCheck2 != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_READWRITE_STORAGE);
} else {
init();
}
and waiting for results:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_READWRITE_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
init();
} else {
Toast toast = Toast.makeText(this, "Read / Write storage permissions required", Toast.LENGTH_SHORT);
toast.show();
}
break;
default:
break;
}
}
My application determines two available storages:
internal - /storage/emulated/0
and external (removable SD card) - /storage/6052-CD5B
I create folders using
new File(parentDirectory, newDirectoryName).mkdirs();
Results:
I can create folder inside /storage/emulated/0.
I can create folder inside /storage/6052-CD5B/Android/data/myapp (this is my application folder).
But I can't create folder in /storage/6052-CD5B outside my application folder, e.g. images folder /storage/6052-CD5B/DCIM.
It seems that granted permissions give me access only to application-specific folder. Is it so? If not, how can I finally get write access to other SD card folders?
I'm working on an application built for Android SKD version 22.
In this application folders are created using this way:
String FileDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES) + "/" + app_name + "/" + prefix;
File mediaStorageDir = new File(FileDir);
if (!mediaStorageDir.exists()){
mediaStorageDir.mkdirs();
Log.d("FolderCreation", "created successfully");
}else{
Log.d("FolderCreation" , "Already exists");
}
It was working well until I had to compile and run it on Android 6 (SDK v.23).
Now the folders is not created.
Is there something that has changed with Android 6 regarding the folder "storage/emulated/0"?
Do I have to change the directory when I work on Android 6?
EDIT -> SOLVED
With Android 6 (SDK v23) I must ask the permissions runtime, showing a confirm dialog.
Here the code I use that works.
//create a method that check the permissions
public static boolean hasPermissions(Context context, String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
In the onCreate():
//list of permissions that you need to ask
String[] permissions = new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION};
//use the method to check if the user needs to confirm the permissions
if(!hasPermissions(this,permissions)){
ActivityCompat.requestPermissions(this, permissions, PERMISSION_IDENTIFIER);
}else{
//permission already granted, hooray!
}
Add onRequestPermissionsResult to manage the choose of the user when the dialog is shown:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode){
case PERMISSION_IDENTIFIER:
//your actions
break;
}
}
Android 6 introduced a new version of permission management. Users can now give and take permissions for apps.
See Android - Marshmallow.
Check youre app permissions in the emulator and look out as
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
might not be given by the user.
I try to create a folder in sdcard
File folder = new File(Environment.getExternalStorageDirectory().getPath() + File.separator + "folder");
Log.d(TAG, "FOLDER :" +folder);
folder.mkdir();
mkdir always return false. I added permission to manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I can create folder with adb tool.
Phone is Nexus 5 Android 6.0.1
what is wrong with code ?
In android 6.0+ you have to request permission at runtime, so in onCreate() request WRITE_EXTERNAL_STORAGE
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
1);
And add this method (optional):
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted!
} else {
// permission denied!
Toast.makeText(MainActivity.this, "Permission denied to write External storage", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
You can use also Nammu to check the permissions
when I try to run this code in marshmallow the folder was not created..
the code is,I tried to run the same code its working fine except marshmallow
File folder = new File(Environment.getExternalStorageDirectory() + "/abcdefg");
boolean success = false;
if (!folder.exists()) {
success = folder.mkdir();
}
if (!success) {
Log.d("", "Folder not created.");
} else {
Log.d("", "Folder created!");
}
Try to add below code in your activity for requesting runtime permission.
Your need to require READ_EXTERNAL_STORAGE permission to create folder(directory) in external storage.
if (ActivityCompat.checkSelfPermission(YourActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_FOR_STORAGE);//REQUEST_FOR_STORAGE=1111
} else {
//Do your stuff here
}
...
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if(requestCode == REQUEST_FOR_STORAGE){
//Do your stuff here
}
}
Hope its help you.
As marshmallow introduce Run time Permission you have to check for the permission
in run time. You can refer here
1.https://developer.android.com/training/permissions/requesting.html
2.https://www.youtube.com/watch?v=iZqDdvhTZj0
3.https://www.youtube.com/watch?v=C8lUdPVSzDk
You have to accept the STORAGE permission group from the user dynamically.
Go with the below link
http://developer.android.com/guide/topics/security/permissions.html