Is it safe to hardcode internal storage path in android? - android

I'm developing an Android App using flutter, which downloads some images from internet and stores them in a directory inside internal storage (the directory and it's content should be visible to user).
I tried path_provider package but it did not work. Here is my code:
var directory = await getExternalStorageDirectory();
print(directory);
Terminal Output (tested on real Android device without SD Card):
W/ContextImpl( 6856): Failed to ensure directory: /storage/extSdCard/Android/data/com.example.test.test/files
I/flutter ( 6856): Directory: '/storage/emulated/0/Android/data/com.example.test.test/files'
I want the root directory of internal storage but it returns app's specific local directory. So I decided to hardcode /storage/emulated/0/ as root directory.
Is this solution safe?
If not, how can I solve the problem?

Related

APK made by buildozer not making CSV file on android mobile

I made an APK with the help of KivyApp. APK generated successfully and my APK also runs on my mobile. The problem is that I didn't give any specific path for the CSV file, which stored the output generated by the app. Initially, I run python code in the pydroid3 app and it automatically generated the CSV file at the same location, where my code was stored. My question is if I want to store the data in the internal storage of my mobile, what path should I enter?
import csv
csvfile = "Discrete_pos.csv"
with open(csvfile, "a") as fp:
wr = csv.writer(fp, dialect='excel')
wr.writerow(csvRow)
Well, I suppose you mean the path of External Storage (which can be accessed by any app, one you see in your mobile file explorer) As it's already storing in your internal storage path. Now to access external storage there are 2 methods. But first of all you need storage permission for that. To get storage permission use the following code:
from android.permissions import request_permissions, Permission
request_permissions([Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE])
Now after you have access to external storage you can use the following code to get external storage path:
from android.storage import primary_external_storage_path
external_storage = primary_external_storage_path()
Sometimes this doesn't work on some android devices so if you are getting that issue then you can also use os.getenv:
external_storage = os.getenv('EXTERNAL_STORAGE')
Also, don't forget to write READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE in the permissions of your buildozer.spec file

Flutter: How to create a folder at the root of the directory, i.e, not under any directory

I am developing a file based application through flutter.
I can create a folder through getApplicationDocumentsDirectory() which gives the path to write files. But it cannot be seen in the files Explorer
I then created the folder through getExternalStorageDirectory() , which can be seen in the files explorer. But I want it to be created in the root.
You may have seen whatsapp folder in the root directory. I also want the same thing. I have tried the following:
Directory('FolderName').create()
But it gives the error saying 'read only os '
Is there a way to do it through flutter ?
You can do it this way:
String folderName = "My New Downloads";
var path = "storage/emulated/0/$folderName";
await new Directory(path).create();

How to fix java.io.FileNotFoundException?

I try to access the file I had downloaded from flutter app and save it to external storage. I call the file and use it in java part but it show this error:
java.io.FileNotFoundException: /storage/emulated/0/Android/data/abc.xyz.qwe/files/mobilenet.tflite
I have tried to grant permission before call the file but it still not work (The file is exist, i have checked).
You start the file name with a slash. What directory is the file located in? If the storage folder is not in the root directory (/), then you won't be able to find the file. If storage is in the same place as the java file you're running, you should remove the first /

How can I get path to my files on device?

How can I get path where my application is installed so I can access to raw folder and set some file as ringtone?
String path=android.os.Environment.getExternalStorageDirectory().getPath() + "sdcard/data/FunnySoundsz/res/raw/fusrodah.mp3";
But nothing happens with this. I set in my Manifest that app install only on Internal Storage

How to copy a file from root of SD Card to one of its parent directory

Im having a file stored in the root of the SD card(i.e not inside any folder)..
but i need my file to be in data/local/ which is above the SD card..
my app is for root users..so i tried doing the following:
"cd "+Environment.getExternalStorageDirectory().getAbsolutePath().toString()
mv file ../
It changes directory to SD Card but doesnt move the file..with error: failed on 'file' - Cross-device link, 255
So i cant move between different mediums .. im guessing so..
Help appreicated.. thanks!
Use the File.renameTo() method in your code.
Edit:
Aha! but the documentation for this API says:
On Android, applications are most likely to hit this restriction when
attempting to copy between internal storage and an SD card.
Hmmm...
I guess you'd have to copy the file and then delete the original.

Categories

Resources