Android 11 - access file in private storage - android

Access to private storage should be possible. So I did a simple test for a test app. I will migrate all my other decent apps later.
I put a file from my desktop to <phone>/Android/data/nl.xyz.myapp/files/example.json.
Just made a quick test to access the data:
File file = new File(MainActivity.mainActivity.getFilesDir(), "example.json");
Reading from the file gave an empty string. Ohw?
When listing the files using the getFilesDir() I saw that only a .Fabric file was there. I couldn't remember putting a file like that over there ;-)
How to make this simple test succeed by reading the data from the file in the private storage?

Your code is trying to read from internal storage. Your location is on external storage.
Start by replacing your code with:
File file = new File(MainActivity.mainActivity.getExternalFilesDir(null), "example.json");
Once you have that working, get rid of the static mainActivity field in MainActivity, so you get rid of your memory leak.

Related

Getting local sqlite database from android

I can successfully upload the sqlite file from iOS to server, however having trouble with android, getting a TiBlob: java.io.FileNotFoundException: Resources/db_name error. To open the database i use var backDatabase = Ti.Database.open('db_name'); i am adding this to the post in the HTTP Client.
file : OS_IOS ? backDatabase.getFile() : Ti.Filesystem.getFile("./db_name"),
So i guess the question is what can i use to get the file in android?
If I understood correctly, you want to download from a server a SQLite database programming with Android, is it that?
I think this is a good answer: https://stackoverflow.com/a/7162385/6133630
Do not use any library, simply take the InputStream that returns the connection and copy byte to byte in s3db file.
On Android, the database is created on the internal storage (you could
move it, or use the install procedure to put it on external storage).
The standard location on internal storage is
/data/data/com.example.yourappid/databases/dbname
Docs here
Or if you are using SDK 5.4.0+, you can actually use the file property to get the db file.
Docs here

Loading MP4 files from Environment.SpecialFolder.ApplicationData Android [duplicate]

From here I know a way to write a file and be accessible to other app and other intent, but now that the Context.MODE_WORLD_READABLE is deprecated how can I safely accomplish this?
FileOutputStream out = myActivity.openFileOutput(fileTo, Context.MODE_WORLD_READABLE);
Okay more info:
I'm using this:
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/*");
And the uri will be the one that I will write to a sdcard location. And the video will come from the application so problem is, if this is now not allowed, how can I write the file and view it.
And the uri will be the one that I will write to a sdcard location.
That is already MODE_WORLD_WRITABLE by default. Also note that the code you have listed (openFileOutput()) does not write to external storage (what you incorrectly call "sdcard"). openFileOutput() is for internal storage.
And the video will come from the application so problem is, if this is now not allowed, how can I write the file and view it.
If you are really writing the file to external storage, just use a Uri pointing to that file.
If you are writing the file to internal storage, create a ContentProvider to serve that file, and use a Uri pointing to that ContentProvider. Here is a sample application with a ContentProvider that extracts a PDF file from assets/ on first run, then serves up that file via openFile() so it can be viewed by a PDF viewer.
Save your video in your internal memory using:
openFileOutput("test.mp4", "MODE_PRIVATE");
Then do this:
String path = context.getFilesDir().getAbsolutePath() + "/test.mp4"; // path to the root of internal memory.
File f = new File(path);
f.setReadable(true, false);
Intent playIntent ....
playIntent.setType("video/*");
playIntent.setData(Uri.fromFile(f));
Good Luck.
It seems to look like the docs are clear about it.
This constant was deprecated in API level 17.
Creating world-readable files is very dangerous, and likely to cause
security holes in applications. It is strongly discouraged; instead,
applications should use more formal mechanism for interactions such as
ContentProvider, BroadcastReceiver, and Service. There are no
guarantees that this access mode will remain on a file, such as when
it goes through a backup and restore. File creation mode: allow all
other applications to have read access to the created file.

Android - working with files in the internal storage

I'm new to Android, but do have some expreience with Java.
For my application I have to use a text file which I have decided to acces from the internal storage using the following code:
String functFileName = "nameOfMyFile";
OutputStream output = openFileOutput(functFileName, Context.MODE_APPEND);
As far as i understood, this means that my application creates a file with the name nameOfMyFile.txt in the internal storage memory, or opens it, if it already exists.
Once the file has been created, it will remain stored until the app is deleted. (Please correct me if I got it wrong)
My question is: Is it possible, that a file with that name has already been created by another application, which in this case would ruin my programm?
In other words: Can I be sure that my app doesn't access another file, which accidentally has the same name, than creating one of itself?
Sorry if this question isn't very professional.
I'd be grateful for any help.
The openFileOutput(String name, int mode) function documentation says :
Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.
It clearly states that the file created with this function is private to the application that created it, so you can be sure that no other application have access to it, provided that MODE_APPEND or PRIVATE were used.
Other two modes MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE are dangerous and deprecated in API 17 and make the file available for other apps.

Application A accessing a textfile stored in Application B's Internal Memory

My app (App A) need to write a textfile which will be stored under another app's(App B) internal memory. Any idea how to implement this?
I have tried the following:
Under both the AndroidManifest.xml file, I have specified the same android:sharedUserId.
From App A, I used:
filePath = getPackageManager().getPackageInfo("packagename of App B", 0).applicationInfo.dataDir;
to get the path and I have confirmed that the path is correct for App B's internal memory.
But I am getting "java.io.IOException: Permission Denied".
Any idea where I have made mistake?
According to official documentation here,
Technically, another app can read your internal files if you set the file mode to be readable. However, the other app would also need to know your app package name and file names.
It's a do-able thing.
So, create a File object with your filepath and call setReadable on it.
Also, check you're not creating the file with MODE_PRIVATE.

Unable to add file from Internal storage as email attachment

I am creating a file into Internal storage (/data/data/package_name/myfile_name), I want to send that file with attachment but I am getting a blank file in attachment(although I checked that file from file explorer, that file is present and not empty at same location).
And same code running well when I used external storage( I am getting my actual file in attachment). Is there any restrictions that we can not send file which are present in internal storage? Or other steps I am missing?
I'm assuming you are trying to send the file as an email attachment using intents.
The reason why the file is empty is that the email app does not have access to the file in /data/data/package_name/myfile_name, due to Androids security model (the /data/data/package_name directory is private to your app).
In order to add the file as an attachment, you need to write it to public storage (such as the SD card) so the email app can access it.
How do you know the file exists at the path you're interested in? Can you view it with DDMS or ADB after your application saved it there? What code are you using to save/read the file? I may be able to provide more specific assistance with that information.
The method used to obtain the internal storage directory on any given device is Context.getFilesDir(). To create a reference to a file named "myfile.dat", for instance:
File myFile = new File(getFilesDir(),"myfile.dat");
Assuming you call the code from inside an Activity or other Context. In order to attach this file to an email, you would be passing a Uri to that location as an extra, so let's add the creation of that to the example:
File myFile = new File(getFilesDir(),"myfile.dat");
Uri fileUri = Uri.fromFile(myFile);
This is all assuming the file was properly saved into Internal Storage in the first place.
Hope that Helps!

Categories

Resources