Android:
  • 07-May-2023
Lightrun Team
Author Lightrun Team
Share
Android:

Android: “Permission denied” – no matter what I do

Lightrun Team
Lightrun Team
07-May-2023

Explanation of the problem

The user is attempting to save base64 data to file, but is encountering Permission denied errors when accessing external storage (/storage/emulated/0) using either the createFile() or writeFile() functions. The user has requested permissions for read and write external storage and has also added the appropriate settings to AndroidManifest.xml. However, the only location that does not raise a Permission denied exception is RNFetchBlob.fs.dirs.DocumentDir, which maps to /data/user/0/{bundleId}/files. The user suspects this path translates to {Internal storage}/Android/data/{bundleId}/files, but even after running createFile() or writeFile() “successfully”, there is no file at that location. The user is unsure if there is anything they are missing and is seeking assistance to resolve the issue.

The code snippet shows the user requesting multiple permissions, including the permissions for read and write external storage, before attempting to write to a file. If both permissions are granted, the user attempts to create a file using the createFile() function from the RNFetchBlob library. The user has tried both base64 and utf8 encoding but is still unable to create a file. If the file is created successfully, the function returns the path of the created file. If not, it returns false with an appropriate warning message. The user is not sure if there is a problem with their code or with the library, and requires further assistance.

 

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for Android: “Permission denied” – no matter what I do

In order to write files to external storage on Android 10, a new storage paradigm called scoped storage is introduced. As a result, the RNFetchBlob library is currently not fully compatible with this new system. One workaround is to add the following code snippet to your AndroidManifest.xml file:

<application android:requestLegacyExternalStorage=”true” … > … </application>

However, this solution is temporary and not recommended for long-term use. It would be ideal for the RNFetchBlob library to update its functionality to support scoped storage in the future.

Another possible issue that may be causing the “Permission Denied” error is the incorrect mounting of the sdcard. To avoid this problem, it is recommended to connect your phone to the computer and run ADB instead of using the “USB Connected” option on your phone. By doing this, you can ensure that the external storage is properly mounted and accessible for file writing.

Other popular problems with RNFetchBlob

Problem: Permission Denied error while writing to storage

One of the most common problems with RNFetchBlob is the Permission Denied error while writing to storage. This error occurs due to the new storage paradigm called scoped storage, introduced in Android 10.

Solution:

To resolve this issue, you can add the following code to your AndroidManifest.xml file:

<application android:requestLegacyExternalStorage="true" ... >
...
</application>

Problem: Large file uploads fail on iOS devices

Another common problem with RNFetchBlob is that large file uploads fail on iOS devices. This issue occurs due to a default timeout limit on the server.

Solution:

To resolve this issue, you can increase the timeout limit on the server or split the file into smaller chunks for upload.

Problem: Slow download speeds on Android devices

RNFetchBlob may also face slow download speeds on Android devices, particularly when downloading large files.

Solution:

This issue can be resolved by adjusting the connection timeout and read timeout settings for RNFetchBlob. You can try adjusting the timeout values to higher values to improve download speeds. Alternatively, you can use the fetch API to download files as it may offer better performance in some cases.

A brief introduction to RNFetchBlob

The RNFetchBlob library is a popular solution for file handling in React Native apps, providing a variety of file system access functions. It can be used to upload and download files, as well as stream data to and from a file. In this case, the user is attempting to write data to a file, but is encountering Permission denied errors when attempting to write to external storage. This error commonly occurs when the app does not have the appropriate permissions, or when the user has not granted the required permissions. To solve this issue, the user has requested the relevant permissions and has added the appropriate settings to AndroidManifest.xml. However, the user is still unable to create a file. To troubleshoot this issue, the user may need to examine the code in more detail or seek further assistance from the community.

  1. File handling: RNFetchBlob can be used to perform various file handling operations such as reading and writing to files, creating directories, and moving files. Here is an example of how to write a string to a file using RNFetchBlob:
RNFetchBlob
  .config({ fileCache: true })
  .fetch('GET', 'https://example.com/image.jpg')
  .then((response) => response.blob())
  .then((blob) => {
    ImageResizer.createResizedImage(RNFetchBlob.wrap(blob), 800, 600, 'JPEG', 80)
      .then((resizedImage) => {
        // use resizedImage.path to access the resized image file
        console.log('Resized image saved to:', resizedImage.path);
      })
      .catch((error) => console.log('Error resizing image:', error));
  })
  .catch((error) => console.log('Error fetching image:', error));

2. Network requests: RNFetchBlob can be used to make HTTP/HTTPS requests and handle the response in various formats including text, JSON, and binary data. Here is an example of how to make a GET request and handle the response:

RNFetchBlob.config({
  fileCache: true,
})
.fetch('GET', 'https://example.com/api/data.json')
.then((response) => response.json())
.then((data) => console.log('Data received:', data))
.catch((error) => console.log('Error fetching data:', error));
  1. Image processing: RNFetchBlob can also be used for image processing tasks such as resizing, cropping, and converting between formats. Here is an example of how to resize an image:
RNFetchBlob
  .config({ fileCache: true })
  .fetch('GET', 'https://example.com/image.jpg')
  .then((response) => response.blob())
  .then((blob) => {
    ImageResizer.createResizedImage(RNFetchBlob.wrap(blob), 800, 600, 'JPEG', 80)
      .then((resizedImage) => {
        // use resizedImage.path to access the resized image file
        console.log('Resized image saved to:', resizedImage.path);
      })
      .catch((error) => console.log('Error resizing image:', error));
  })
  .catch((error) => console.log('Error fetching image:', error));
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.