Android untar a file permission denied - android

I have a tar file that I want to download from an API and un-tar it, on most devices the code below works fine but on few devices it gives an error
code:
val untar = arrayOf("tar", "-xvf", file.absolutePath, "-C", tempDirectory.absolutePath)
val process = Runtime.getRuntime().exec(untar)
Error on few devices only:
tar: exec gunzip: Permission denied

Make sure you have the right android permissions:
<manifest>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>

I am not sure, maybe tar which is a link to toybox is fine, but unzip which is a link to ziptool is not. Maybe you should try using apache commons compress, in order to bring your own dependecies and not rely on operation system issues, altough the solution is going to be more complex.

Related

Android 10: Delete denied by permission:android.permission.ACCESS_MEDIA_PROVIDER

Target SDK in my project is 31 and when I'm trying to delete a file from DCIM directory, I'm getting following error such as delete denied by permission:android.permission.ACCESS_MEDIA_PROVIDER.
Please consider file path like this: "/storage/emulated/0/DCIM/Screenshots/pic1.jpg"
I have tried so much to find the result every where including stack overflow, but didn't succeeded yet. Even I changed target SDK to lower 30 but not worked.
Following are the solutions that I had already worked on but nothing works:
1.flags in manifest file
android:requestLegacyExternalStorage="true"
android:preserveLegacyExternalStorage="true"
2.permissions in manifest file
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="40"
tools:replace="android:maxSdkVersion" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
Please find the attached code
val fileToDelete = File("/storage/emulated/0/DCIM/Screenshots/pic1.jpg")
fileToDelete.delete()
NOTE: filepath is hardcoded only to explain better
Please find the attached log
2022-03-21 11:22:35.331 8639-20226/com.filepickerdemo D/ContentResolver: delete denied by permission:android.permission.ACCESS_MEDIA_PROVIDER#content://media/external/images/media#_data = ?#/storage/emulated/0/DCIM/Screenshots/pic1.jpg
What else should I put in my code to solve this issue. Thanks in advance.
To delete media files in android 10 and above, You can try contentresolver API.
You need to pass the media's content uri to the 'contentResolver.delete()' method & you will be able to do it easily.
Here is my post on how to do that using Java - Scoped Storage in Android — Writing & Deleting Media Files

Flutter APK Build

Goo day friends! I am new to Flutter and I have been experimenting on fetching data via REST API. On Android Studio, everything works just fine, so I suppose there's nothing wrong with my code.
Things I did to make sure my code works:
1.) Run the code in android simulator;
2.) Run the code using real device (enabling USB Debugging).
And it works just fine.
But when I build an apk file out of it and install it on my device, it no longer makes the API call (or is unable to?). I made it so, when the app runs initState(), it waits for the data to be loaded. While the data is not yet available, a CircularProgressIndicator() takes the entire screen.
If I run the app via the installed apk file, it's just CircularProgressIndicator(). Meaning, no data is being loaded and displayed. But when I run the code in AndroidStudio, the data is shown..
I am using http package. I'm not sure what I'm doing wrong, or what I'm missing.
Other things to note: I did not change anything in my AndroidManifest file, and just followed all the steps on building apk file in Flutter through the official documentation.
Like alexsanderfr said, this is indeed most of the time being caused by missing
<uses-permission android:name="android.permission.INTERNET" />
You shouldn't need
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
for REST API.
In Flutter, when debugging you implicitly have access to internet, however in release builds, you need to explicitly declare this permission.
update1
Ensure you have the http-package added to your pubspec.yaml file and that it's properly indented with two spaces:
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
http: ^0.12.0+4
Did you add the internet permission to the manifest? If you're accessing the internet from an Android app you need to add the internet permission over the application tag.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

How tho solve the permission denied problem in NDK code?

I want to read a bmp file in ndk code, but it always failed.and the error code showed the permission denied. how to solve this problem?
The IDE is Android Studio 3.1.2,but I think it is nothing to do with IDE. The device is hikey960 with Android 9 API28.
I have referenced some similay issues and added the user-permission in Manifest.xml and specify the absolute path, but it still doesn't work! But if I build it as a executalbe file and push to /data/ and give a full permission, it works normally!
here is the ndk code:
FILE *fpbmp = fopen (imagefile, "r+");
if (fpbmp == NULL) {
LOGD ("open file failed!");
LOGD("code %s \n", strerror(errno));
}
and the AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wangzh.normaltest">
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
....
The logcat shows that the error code means permission denied:
/sdcard/DCIM/1_1.bmp
open file failed!
code Permission denied
Could someone give some advices? Anything will be appreciated!
I think I find the reason!
the permission of the bmp file in /sdcard/DCIM/ is -rw-rw----. It can't be changed even though run the chmod command and set permission in AndroidManifest.xml and run-time.
So I mv file to /data/ and chmod 777 thefile. then it work well and doesn't need permission in AndroidManifest.xml or run-time permission.
on the console, input the below command:
su
setenforce 0
You can use the below command to check if the command is inputed before.
getenforce
if it return "Permissive", it is ok.
I use android 11, cpp code is at hidl interface. With this method, it is solved. fopen/fwrite/fread work well.

How to read a text file from SD card in Android device through command prompt?

There is a folder called "D_Permision" SD Card and there is a text file called "permission.txt" in it.
I want to read this text file through command prompt and display its content in the command prompt(Not in a separate file)
When I use
adb pull permission.txt /sdcard/D_Permission
it gives following error
remote object 'permission.txt' does not exist.
but I used
adb push permission.txt /sdcard/D_Permission
(successfully added permission.txt to D_Permission folder)
and the tried to pull.but I get same error.
sdcard/D_permission has both read / write permission
So how to read this text file through command prompt and display its content in the cmd?
You don't need to pull the file.
adb provides shell acces, so you can do a cat:
adb shell cat /sdcard/D_Permission/foo.txt
If the file does not exist though, you'll still get "does not exists" error of course, which is normal.
I directly use the path "/mnt/external_sd/.. Path of File.".
But it was only work with my Device
Maybe you forget these permissions.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Create folder on /mnt/externel1

I want to create folder on /mnt/externel1 (which is my external micro-sd card path) but when I create folder problematically [ file.mkdirs() ] it is returning false.
And when I am trying to download a file on that path by creating an outputStream it throwing an exception "Permission denied"
Note: android application not allowing to write on external micro-sd card.
your advise will helpful for me.
Please add below permission in manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
you have to mentioned permission in manifest file when you want to write into sdcard.
If you're targeting Honeycomb, you can't write to the external SD card.
Edit
Just noticed the permission stuff - you should make sure that you've got the appropriate permissions. See Chirag Raval's answer.

Categories

Resources