Read harcoded Uri from external storage - java

Im trying to read a json file from an external storage without sucess. Im getting the error:
Permission Denial: opening provider com.android.externalstorage.ExternalStorageProvider from ProcessRecord{bfaa1d6 26301:com.plm.valdecillasurdemo/u0a284} (pid=26301, uid=10284) requires that you obtain access using ACTION_OPEN_DOCUMENT or related API
My manifest.xml file is OK
I am checking in runtime for READ_EXTERNAL_STORAGE permission and only read the file when the permission is granted
When I try read the file I getting the related error.
In a second approach I started an ACTION_OPEN_DOCUMENT Intent.
A system dialog ask me for picking a file and all is fine. I read the file correctly however I dont want be asked for a Uri. My Uri is hardcoded.
I know must be easy the solution but this problem is drives me crazy...How can I resolve this?
Thanks in advance.

My Uri is hardcoded
That is not possible. You do not have rights to such a document, even if the Uri is valid (which it won't be, since the DocumentsProvider did not do any work on its side to grant it). Beyond that, the user may not have a document at the location that you hard-coded.
How can I resolve this?
Use ACTION_OPEN_DOCUMENT and allow the user to choose the document to remove.
Or, use filesystem locations that you can read and write across OS versions, mostly those defined on Context: getFilesDir(), getCacheDir(), getExternalFilesDir(), getExternalCacheDir(), etc. In this case, you will not need to work with Uri values.

Related

Storage on android 11

I want to store images, videos, audio, and all types of documents like (jpg, mp4, gif, txt, pdf, CSV and etc.) in one folder like 'ABC' and store all that in ("ABC") folder.
I have lots of R&D and done from ALL files access(MANAGE_EXTERNAL_STORAGE) permission and working perfectly as I said as above but play store was rejected due to all file access permission.
Play Store Reject Message
Policy: "Access all files" permission
File and directory attributes on your device are considered your personal and sensitive information and are subject to your personal and sensitive information policies and the following requirements:
The access that your app requests should be limited to device storage, which is essential for your app to function. You cannot request access to device storage for third parties for purposes unrelated to essential app features for your users.
Android devices running Android R (Android 11) or later require MANAGE_EXTERNAL_STORAGE permission to manage access in shared storage. Any app that targets R or later and requests a wide range of access to shared storage (“access to all files”) must pass a proper access review before it can be published. Apps that are allowed to use this permission must clearly display a message to the user asking them to enable Access to all files in the Special App Access settings. For more information on this requirement for R, see this help article.
Find out more about the Access All Files permission
Find out Android storage use cases and recommended methods
Please resolve this issue in Google Play Console.
.
Problem: I need to use the Media Store API
The "Access All Files" permission has been requested, but only media files need access for important features of the app. The MediaStore API allows apps to access and interact with media on external storage volumes without the need for "access to all files."
Update your app to use the Media Store API and remove the "Access All Files" (MANAGE_EXTERNAL_STORAGE) permission.
So please help me have anyone idea using Scoped Storage how it works like create a folder on storage like /data/ABC and store all documents as I mentioned like (jpg, mp4, gif, txt, pdf, CSV and etc.)
I want to like please check below steps
Create a folder (not in a folder like pictures, video, audio, document and download as we have on File manager)
Store file and images in "Created Folder"
Store Download File in "Created Folder"
Easily open after download
Update and Delete Image
Please help me through the coding level if you used it because I have lot's of R&D but not get any document so please don't share any document so if you have used please help me because before 29 API levels it's working and on 29 API level I set android:requestLegacyExternalStorage="true" on manifest so I was also worked for me but android 11(30) what we do please give me a proper solution.
Used code like below
final String directoryFolder =
Environment.getExternalStorageDirectory() + "/" + "ABC";
File file = new File(directoryFolder);
if (!file.exists()) {
file.mkdir();
}
getting error like below
com.androidnetworking.error.ANError:
com.androidnetworking.error.ANError: java.io.FileNotFoundException:
/storage/emulated/0/ABC/19429202106171621.pdf: open failed: EPERM
(Operation not permitted)

Security rules for Firebase Storage using File Metadata

I am building an android app and I need to add security rules to Firebase Storage. My App has groups and the file path on storage is /{groupId}/Images/{allPaths=**} and I want to give read and write permission to the members of that group. How can I pass a list of uids to customMetadata() and add corresponding rule for that. Another question is about the rules for write permission: In Firebase, an image is uploaded first and then its metadata can be updated. So if a write rule contains some check on metadata, won't it fail at the time of upload?
PS: I have tried various solutions but all of them failed. One solution was to add uids as keys in customMetadata() and keep value as empty string. But even this didn't work. Any help is appreciated. Thanks.
Update: I added uids as keys in customMetadata() and it worked in read mode security rule but I am still facing error in write mode because while uploading file request.resource.metadata is null.
I solved this issue. I was updating metadata after file upload whereas I had to add metadata during file upload. Refer to Add File Metadata section in the docs: https://firebase.google.com/docs/storage/android/upload-files

Unable to read file using FileProvider

I have multiple apps that take photos and store them on external storage.
I want to upload the photos along with some metadata to a web application running on my companies server.
I am able to write data and file URI to a database using a ContentProvider.
I have written a SyncAdapter to upload the files via HTTP-POST, but I am stuck because in this SyncAdapter I cannot get permission to read the files.
My big question is: Can I use a FileProvider to overcome this issue?
I am NOT storing the files in the ContentProvider using an intent.
I have tried to set permissions on URL, it failed.
I Desperation I tried to export the FileProvider, which was not allowed.
I want to be able to open an input-stream of the file in another app than the one storing the file without having to start the transfer using an Intent.

How can I read a file in Internal Storage without declaring the permission?

I'm writing an app that allows users to import their own data (via .zip's, .png's etc.) from an external location such as their downloads folder.
My app is correctly getting the file path from the URI, however I do not wish to require the WRITE_EXTERNAL_STORAGE permission in my app, which I need in order to read the file the user is providing me. I feel like Android should allow my app read permission to this file, since the user selected it.
Is there a way to have Android copy this file to a temporary location where I can read it, so that I do not need to ask the user for this permission?
If you don't want the permission, then there is a way for Android4.4 and above. Let the user import the data into the programdirectory, see also:
Starting in Android 4.4, the owner, group and modes of files on
external storage devices are now synthesized based on directory
structure. This enables apps to manage their package-specific
directories on external storage without requiring they hold the broad
WRITE_EXTERNAL_STORAGE permission. For example, the app with package
name com.example.foo can now freely access
Android/data/com.example.foo/ on external storage devices with no
permissions. These synthesized permissions are accomplished by
wrapping raw storage devices in a FUSE daemon.
Taken from this page -> https://source.android.com/devices/storage/index.html

Content Syncing on Android

I am writing an application for android and i wish to be able to sync content from a web server, IE: The layouts XML to the device and cache it so that if the device is not connected to the internet it uses an old version of the XML so as soon as the device is on the internet is able to update and gather the latest XML.
Couldn't find much on the internet, so if someone could point me in the right direction or give me some ideas this would be quite helpful.
Cheers,
Alex
This is how it works with a normal xml file:
Create an on your device HTTP Client
Prepare a GET request to your server
Send the request and retrieve the HTTP Response
Read the content using an InputStreamReader
Write the Stream to a file on the external storage name it e.g. newes_version.xml
catch all exception, and just use the file newest_version.xml file.
But it can't work for Layout files
Why? Every time you load your app on an device, everything (except /raw) in the /res folder is being compiled. Therefore your 'stylsheets' doesn't exist anymore the way they did in your IDE. It's not as simple as in html/css where you can just exchange on file. (Well, you can, put not an runtime)
Solution
If you really want to change your layout on Run time, you would have to load the xml (see above), parse it, and apply the information given by file using the R.id reference of the View elements.

Categories