File oldFile = new File(dirPath+"/"+packageName+".apk");
Log.e("Path ",oldFile.getPath());
my log returns this:
E/Path:
/storage/emulated/0/Android/data/com.bazibaaz.app/files/com.YGD.GoldenBasket.apk
i have this file exactly on the path shown in the log, but this code:
oldFile.exists()
returns false!
i have permissions to read and write:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
here is the picture of file (to prove its existence):
( path: internal storage/storage/emulated/0/Android/data/com.bazibaaz.app/files/com.YGD.GoldenBasket.apk)
Related
You need to read data from a file that exists in the path /storage/emulated/0/Documents/. There is no problem in checking the existence of a file. But I access this path with 'jni' i.e. 'C' code and read the data from the file.
Failed to open file due to permission problem.
Is there a way to access files in /storage/emulated/0/Documents/ in jni C?
permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
//in <application...
android:requestLegacyExternalStorage="true"
android:preserveLegacyExternalStorage="true"
code
int nResult = (i ~/ 100) * 100;
File agingFile = File("/storage/emulated/0/Documents/$nResult/$i.C03");
await platform.invokeMethod("readFile", agingFile.path);
///JAVA
if(call.method.equals("readFile")){
String path = call.arguments();
ReadFile(path);
}
I am developing a Flutter app. I am trying to add a functionality to delete a file in device storage. When i initialize a file object with the path to the file, and run file.exist() i get true. But when i run file.delete() i get
(OS Error: No such file or directory, errno = 2)when file.delete(). At the same file.exist() return true!
I have these permissions in my manifest
<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.WRITE_MEDIA_STORAGE" />
<uses-permission android:name="android.permission.STORAGE_INTERNAL" />
and
<application
android:requestLegacyExternalStorage="true"
And i am using permission_handler to ask for permission first.
Any idea?
I'm trying to read a file that exists, but I can not. Thanks!
File f = new File("/mnt/sdcard/WhatsApp/Media/WhatsApp Images/IMG-20180628-WA0000.jpg");
File sdDir = Environment.getExternalStorageDirectory();
Log.w("Whatsapp","#SDDIR CANREAD? "+ sdDir.canRead() +" PATH: "+ sdDir.getAbsolutePath());
f.setReadable(true,false);
Log.w("Whatsapp","#FILE: "+ f.getName() +" l:"+ f.length() +" exists:"+ f.exists() +" canRead:"+ f.canRead() +" PATH: "+ f.getPath() +" ABSOLUTE: "+ f.getAbsolutePath());
LOG
#SDDIR CANREAD? false PATH: /storage/emulated/0
#FILE: IMG-20180628-WA0000.jpg l:0 exists:false canRead:false PATH: /mnt/sdcard/WhatsApp/Media/WhatsApp Images/IMG-20180628-WA0000.jpg ABSOLUTE: /mnt/sdcard/WhatsApp/Media/WhatsApp Images/IMG-20180628-WA0000.jpg
AndroidManifest Permissions
<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_DOCUMENTS" />
First, you do not have arbitrary read-write access to removable storage.
Second, most things in Android are case-sensitive. So, your <uses-permission> elements will not work.
Third, you cannot hold the MANAGE_DOCUMENTS permission, as that is not available for ordinary apps.
So you can fix your manifest by replacing your existing <uses-permission> elements with:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
However, you still may not have access to that particular file.
Works!
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I'm trying to create folder in internal storage of the device, and have some problems with that. I can't create directory using :
String rootPath=Environment.getExternalStorageDirectory().getPath()+"/test";
File file=new File(rootPath);
if(!file.exists()){
file.mkdir();
}
file.mkdir return false
I have such permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
Thank you very much for answers !
So really, as you all guys said in Android Marshmallow (API 23) or higher we need to ask runtime permissions.
To solve this issue use call :
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
in your onCreate() method
Thanks all for your help !
You can use
File sampleDir = new File(Environment.getExternalStorageDirectory(), "/test");
if (!sampleDir.exists()) {
sampleDir.mkdirs();
}
Here is the exact exception that's thrown:
java.io.FileNotFoundException: /mnt/sdcard/example.mp4 (Permission denied)
I literally copied and pasted example code from here. In addition, I also added this little bit of code to format my path correctly:
private String sanitizePath(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
if (!path.contains(".")) {
path += ".3gp";
}
return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
}
Here are the permissions included in my manifest:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.STORAGE" />
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.front"/>
Am I missing something completely obvious or is it something more?
You might want to check if the phone is in "Mass Storage Mode".
When USB cable is connected in this mode, you cannot access files on /sdcard.