load image picasso open failed: EACCES (Permission denied) - java

I want to load an image but I got this error:
/storage/emulated/0/productss/Montearci_products/Bracelets/airplane/Brac - 020.jpg: open failed: EACCES (Permission denied)
The file is exist.
My codes:
Picasso.get().setLoggingEnabled(true);
Picasso.get().load(muri).into(mimg);
my android version is 10.
my permissions:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

I just added this line to application:
android:requestLegacyExternalStorage="true"
I don't know but for android 10 it should be added !

First Option that you should take care of is Permision -
For API 23+ you need to request the read/write permissions even if they are already in your manifest file cause these are considered dangerous permissions
// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
/**
* Checks if the app has permission to write to device storage
*
* If the app does not has permission then the user will be prompted to grant permissions
*
*
*/
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
Second Option that you can try is :-
According to Picasso docs you have to do something like this: file:///android_asset/DvpvklR.png
So I used to have:
/storage/sdcard/Pictures/findyoursport/yoursport_1482358052384.jpeg
Prepending: file:// did the trick
Third - If you're running this on Android 10 or higher then use this->
<manifest ... >
<application android:requestLegacyExternalStorage="true" ... >
...
</application>
</manifest>
I hope it will work

Maybe you should add request for FileSystem permission

You might be missing the following permissions in your Manifest to be able to read from SD Card:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Related

BluetoothAdapter.startDiscovery() works on one phone and not on the other, both are android 11 (api 30)

I am working on a bluetooth application. When I run the app on my phone and press the button that is supposed to run the startDiscovery() method, it works fine for me. But when I try the same thing on another phone, it doesn't work. It does not start searching for devices. Both phones run Android 11.
This is the code I have:
btnSearch.setOnClickListener(new View.OnClickListener() {
#SuppressLint("MissingPermission")
#Override
public void onClick(View v) {
enableBt();
txtStatus.setText("Searching...");
btnSearch.setEnabled(false);
btnCancel.setVisibility(View.VISIBLE);
pbLoading.setVisibility(View.VISIBLE);
deviceList.clear();
addresses.clear();
setAdapter();
bluetoothAdapter.startDiscovery();
}
});
I have all the necessary permissions written in the AndroidManifest file:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
I request from user for ACCESS_FINE_LOCATION permission in MainActivity.java. Which should also automatically allow ACCESS_COARSE_LOCATION.(right?). Both phones get location permission request, and both phones get bluetooth enable request, but only one scans for other bluetooth devices when the btnSearch is pressed.
I tried to ask for BLUETOOTH_SCAN which isn't required for android 11 if I'm right. And I am pretty much out of ideas.

Android Scoped Storage Permission for Download Folder to Download PDF

I have an app that download specific pdf files using streamig Retrofit. I am upgrading my app targeting android 11. I have gone through many online documents and stackoverflow solutions and got it working. The files gets downloaded to Downloads folder.
With Scope Storage, do i need to ask for specific permission to read/write for 29 and above, as this folder is shared folder? I already have these in manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
I am using this to check if permission is granted
public static boolean isPermissionGranted(Context context){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
return Environment.isExternalStorageManager();
} else {
int readExtStorage = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE);
int writeExtStorage = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
return readExtStorage == PackageManager.PERMISSION_GRANTED && writeExtStorage == PackageManager.PERMISSION_GRANTED;
}
}
Do I need to use this also in manifest, and if so how do i handle if permission not granted?
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
I was thinking of using below, but i guess Playstore will reject my app with this absurd permission request.
Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
intent.addCategory("android.intent.category.DEFAULT");
Uri uri = Uri.fromParts("package",getPackageName(),null);
intent.setData(uri);
permissionActivityResultLauncher.launch(intent);

Android READ_EXTERNAL_STORAGE permission not working

I'm trying to create a simple app that can get an image from the Gallery app and display it on an imageButton. I'm testing with API 21 on a phone running Android 5.0.1. Unfortunately, no matter what I try I keep getting a security error - even when I specify the permissions.
My code for getting retrieving the image is:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent){
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode){
case PICK_IMAGE_REQUEST:
if(resultCode == RESULT_OK && imageReturnedIntent != null && imageReturnedIntent.getData() != null) {
Uri uri = imageReturnedIntent.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
// Log.d(TAG, String.valueOf(bitmap));
ImageButton ib = (ImageButton) findViewById(R.id.inputImg);
ib.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
break;
}
}
The code works when I try to select an image from Dropbox, but when I select an image from gallery, I get
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/35634 from pid=25240, uid=10070 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
I made the use-permission tag for READ_EXTERNAL_STORAGE a child of manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="[REDACTED]">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="23" />
<uses-permission tools:node="replace" android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
<application>
[REDACTED]
</application>
</manifest>
I've searched high and low on other posts, but still can't seem to figure out why I'm still getting this pesky error.
PROBLEM
Your have the error
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/35634 from pid=25240, uid=10070 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission().
CAUSE
You are not giving correctly permissions because of android:maxSdkVersion="18", which according to documentation.
The highest API level at which this permission should be granted to your app. Setting this attribute is useful if the permission your app requires is no longer needed beginning at a certain API level.
As long as you didn't give permissions to API 21 your app is behaving ok.
Check this topic for further info.
SOLUTION
If you wanna give read permissions under API 21, you must declare them as:
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="21" />
IMPORTANT:
This only make sense when you need to give extra permissions because some old api's require permissions that new versions doesn't, so user experience get's improved because you only ask for certain permissions when needed.
So (in this case) the correct way will be:
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
ADD ON
If you want to save data you must add WRITE_EXTERNAL_STORAGE permissions.
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Simply replace with the below one and try,
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
While debugging on some devices you need to grant permission explicitly by going to your settings --> apps --> MY_APPLICATION --> Permissions and then grant required permissions.
In addition to adding the below in AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
we also need to request permission in the activity for the app to actually have access to external storage. https://developer.android.com/training/permissions/requesting.html

android bluetoothadapter.startLeScan, filter by UUID

I want to scan BLE device with the startLeScan(UUID[] serviceUuids, LeScanCallback callback) method, now I have a UUID, it's a 16-bits value, for example, 00000000-0000-1000-8000-00805F9B34FB.
How can I use the UUID in startLeScan method, I write like this,
UUID[] uuid = new UUID[1]; uuid[0] = UUID.fromString("00000000-0000-1000-8000-00805F9B34FB");
mBluetoothAdapter.startLeScan(uuid, mLeScanCallback);
But finally I can scan nothing. How can I resolve this problem.
I used the same method as you,and i added permissions in AndroidManifest.xml file like below:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
hope that will help you.

android: how to set the "user-permission" within the "MainActivity.java" file

I am developing an android app that is implementing the Wifi P2P framework ...the user-permissions are set on the Manifest.xml as follow:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
.. and, I believe, the P2P service is enabled once I start my app (and on resume) ...
and I do not want that anymore ...i.e.: enabling the P2P service automatically ..
I want to be able to set a ToggleButton that enables or disables these permissions from within my MainActivity.java file. here is my ToggleButton code:
public void onToggleClicked(View view) {
Button btnDiscover = (Button) findViewById(R.id.btnDiscover);
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// Enable the P2P service
} else {
}
}
How can I do this ?
P.S: I am new to android and Java programming
No. The user needs to be informed about the permissions while installing the application. Askling them at runtime would be a security risk. And google doesn't like possible security-leaks.
Instead of changing the permissions dynamically...Use a boolean variable. If it true proceed other wise don't proceed further...the variable default value is false

Categories

Resources