Android Read and Write Without Requesting Permission - Theta - android

i need permission read and write for my plugin, i try a lot of things.
In manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
but not working after v23 according to others posts, so i try with requestPermission but my app crash and in real use case i don't have any interface.
I'm using RICOH THETA Plug-in SDK
I'm trying to use BitmapFactory and got this error
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/100RICOH/R0010156.JPG (Permission denied)
If someone has any idea how to do it.
Thanks.

Don't worry. THETA Plug-in store grants all permission automatically on instal time. Users do not need to grant permissions manually in actual use case. You need to grant permissions manually only for development time.
The official document describes that
Declaration of Permissions
When installing from the RICOH THETA store, based on the protection level set in the manifest file, permission is automatically granted. During development, use an application that displays the screen such as Vysor, and grant permission from the application settings or from a plug-in dialog window.

You have to give permission manually in android 6 and above use dexter library
Link https://github.com/Karumi/Dexter

in first you should check android version
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED {
// do work
} else {
String[] permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(permissions, 100);
}
}
} else {
// do work
}

Related

Android API 28, save file in Download folder without user interaction

we're developping an app that will run as a service. One of the feature would be to download file at given URL (ex PDF) and save it into the download folder so user can load it from a specific application (Avenza Maps).
All the download process should be without any user interaction since it's by a service that run in the background.
i've added the following permission:
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.MANAGE_EXTERNAL_STORAGE"/>
<application
android:requestLegacyExternalStorage="true"
whatever i'll try i got the following error
java.io.FileNotFoundException: /storage/emulated/0/Download/name.pdf (Permission denied)
how can i get the permission to write on Download folder (this is system Download folder)
without having to open an activity to save the file?
i'll try multiple solution yet(2 day of google) without success
for now as stated we target API 28 (android 9)
we will later target other API since we provide the device to the client so we develop only for the API our device have.
I've recently had to develop an app that downloads voice files to a device. While you can specify permissions in the Android manifest, you must request permissions from the user. I've done so in Java, but a conversion to Kotlin should be simple.
//method is called to check the storage permissions for this app
//ensures the app can write and read files
private void checkStoragePermissions() {
Log.i("Permissions", "Checking Storage Permissions");
int writePermissionCode = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);//get current write permission
int readPermissionCode = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);//ge current read permission
Log.i("Permissions", "Fetching Read & Write Codes: " + readPermissionCode + "/" + writePermissionCode);
//if permissions to read and write to external storage is not granted
if (writePermissionCode != PackageManager.PERMISSION_GRANTED || readPermissionCode != PackageManager.PERMISSION_GRANTED) {
//request read and write permissions
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
Log.i("Permissions", "Asking For Storage Permissions");
} else {//else: if permissions to read and write is already granted
permissionsGranted = true;//set permissions granted bool to true
}
}
After you've done this the downloading of the files can be done in many ways. It's worth noting that files can't be downloaded to any location on an Android device. Only a specific destinations can be used.
I hope this helps to clear some of your confusion. Happy coding!
how can i get the permission to write on Download folder (this is system Download folder) without having to open an activity to save the file?
The simplest solution is to write somewhere else, where you do not need permissions. The methods on Context that return File objects, like getFilesDir() and getExternalFilesDir(), are your primary candidates.
Beyond that, it appears that your app is pre-installed on some device ("we provide the device to the client"). If you have developed a custom firmware image, you should be able to pre-grant the permission to your app as part of that image. Or, if the device is being distributed already configured (no first-time-power-on onboarding UI), you could manually grant WRITE_EXTERNAL_STORAGE to your app or have a UI automation script do it.
If none of those are options, then you have no choice but to ask the user for permission.

Request write permissions on Android

Sorry, this question has probably been asked multiple times already, but I am struggling with different SDK versions. My app is used as a research instrument and solely installed per .apk directly on tablets, thus no app store. I need to record the answers from users and write them into a text file. And I am having a hard time requesting the permission from users to store the data on the external storage.
I added the following line to AndroidManifest.xml:
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
In the MainActivity, I call askForPermissions in the onCreate function:
public void askForPermissions() {
int result = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result != PackageManager.PERMISSION_GRANTED){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (!Environment.isExternalStorageManager()) {
Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivity(intent);
}else{
//TODO no clue, what to do here
}
}else{
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
Toast.makeText(this, "Permission needed to store the data. Please allow storage functionality.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},2);
}
}
}
createDir();
}
In API version 30, I use the "Intend" and accordingly the settings screen with the permissions shows up and I can grant writing permissions manually - and this works (directory is generated, data stored ...). First questions: In which cases can it happen that there is no external storage manager (hence the TODO)?
What I am struggling with is SDK < 30. This does not seem to work. I do not get permissions and in the app info, I cannot give the app writing permissions manually. The permissions option is disabled. What is the correct way to request permissions in that case (most preferably directly in the app)?
Sorry for the possible double post. Newbie here.
Try this in manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Does Anyone knows Where this permission come from and how to grant this programmatically in Android?

I want to create a file explorer , after a lot searching , I found out that I can't Modify , delete or create files on SD card without SAF(Storage Access Framework) on API 25 and higher.Then I installed Some File Manager for Testing how do They Work. All of Them use SAF except Xiaomi File Manager. Xiaomi's only grants a permission. I captured some screenshots.enter image description here
enter image description here
EDIT 1 : I get all my runtime permissions.
thanks for reading :)
Android defines basically three types of permissions:
Normal Permissions
Signature Permissions
Dangerous Permissions
Both Normal and Dangerous permissions must be defined in the Manifest file. But only Dangerous permissions are checked at runtime, Normal permissions are not.
Normal Permissions
Some permissions are automatically granted to the application. Just we need to declare those permissions in AndroidManifest.xml and it will work fine. Those permissions are called Normal Permissions. An example of a normal permission is INTERNET.
list of Normal Permissions: https://developer.android.com/guide/topics/permissions/overview
Signature Permissions
A permission that the system grants only if the requesting application is signed with the same certificate as the application that declared the permission. If the certificates match, the system automatically grants the permission without notifying the user or asking for the user’s explicit approval.
Dangerous Permissions
Some permissions may affect the users private information, or could potentially affect his data or the operation of other application are called Dangerous Permissions. For example, the ability to read the user’s contacts is a dangerous permission. Some other examples are CONTACTS , CAMERA ,CALENDAR, LOCATION , PHONE , STORAGE , SENSORS , MICROPHONE, etc.
Dangerous permissions must be granted by the user at runtime to the app.
Here is an example for requesting a Dangerous Permissions on runtime :-
First, add permissions to AndroidManifest.xml file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
then ask for permission from user during runtime, like this:-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}else {
dispatchTakePictureIntent();
}
}
Then override the onRequestPermissionsResult method according to your need
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1){
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
do your stuff...
}else {
Toast.makeText(this,"Permission Denied",Toast.LENGTH_SHORT).show();
}
}
}
For more information check out this article : https://medium.com/programming-lite/runtime-permissions-in-android-7496a5f3de55
I found it after too many searching in Stack Overflow , So there is a flag that call that dialog and get the uri of the storage ,but this way only works with android 7 to 10.
here is answer of my question :
question answer

Permission denied reading from sdcard

I test my application on a virtual nexus 5 with Marshmallow
I got a notification about a virtual sd card being ready and I tried choosing both internal and external storage.
I uploaded a text file to sdcard/Download by dragging and dropping to the emulator.
I added <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> and even <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> to my manifest.
When I try to read a file a sd card using code such as this
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"Download/gradle.build");
try {
BufferedReader br = new BufferedReader(new FileReader(file))
...}
I get an open failed... EACCESS - permission denied exception thrown.
Why can't I read a file from the sdcard? Thanks.
Request permission at runtime is a feature added in android version 6.0 The idea is when the app is installed customers just grant permission without knowing the security risk. Dangerous permission must be granted at runtime so hopefully users know the risk, in this case you want to use SD storage because (give reason here). Good luck.
Since you are targetting devices marshmallow or above you have to give permissions at runtime and permissions declared in manifest does not mean anything. So have to give permission like below before accessing any of the file related read or write.
first check if permission already given by checking this
// Assume this Activity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck==PackageManager.PERMISSION_GRANTED){
//this means permission is granted and you can do read and write
}else{
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION);
}
here is permission guide

READ_EXTERNAL_STORAGE cannot be resolved or is not a field

I'm trying to republish my app for API level 26 as per Play Store requirements but I am getting null back from this:
File[] files = directory.listFiles();
I understand this is because I need to check permissions at runtime now. I have inserted this code at the start of the activity's onCreate to do that.
// check permissions
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
{
// Permission is not granted
this.finish();
}
But it does not recognise READ_EXTERNAL_STORAGE. Strangely it does recognise WRITE_EXTERNAL_STORAGE.
I saw many responses saying the wrong import is to blame, so I now have this:
import android.Manifest;
In the manifest itself I have this, inside the Manifest tag but outside the Application tag.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I am working in Eclipse/ADT and have updated the SDK and support package. Any idea what is going on here?
I had not updated the build target in the Project Properties.
If anyone else faces this situation that is the solution.

Categories

Resources