Android can't get the correct database path - android

I am trying to get the path of my app's sqlite db file as I want to backup it to Google drive in app, but it gives me a different path:
String Path = getApplicationContext().getDatabasePath("mydb.db").getAbsolutePath();
File filePath = new File(Path);
This Path gives me: /data/user/0/mypackage/databases/mydb.db
But when I use:
File data = Environment.getDataDirectory();
String Path ="/data/mypackage/databases/mydb.db";
File filePath = new File(data,Path);
So the final path is /data/data/mypackage/databases/mydb.db and with this path the backup is also working.
I can also see the db file there in the file browser in android studio when the device is attached.
So why the getDatabasePath gives me path like data/user/0/ ?

Related

How to get the file path from the recent files in android 10

I'm creating an app in which I have to take the path from the recent files section when I click on the select files button.
But in the Android SDK version greater than 23 the uri.getPath() is not giving me the path instead it gives me the id /document/document:100664.
And I want a path like this /document/primary:Download/test.txt.
This uri.getPath() is only working in the Android SDK version less than or equal to 23.
Below is the code which I'm using to get the path.
if (Build.VERSION.SDK_INT <= 23) {
file = mediaFiles.get(0).getPath();
}
else {
Uri uri= mediaFiles.get(0).getUri();
String path = uri.getPath();
String path1 = path.substring(path.indexOf(":")+1);
File sdcard = Environment.getExternalStorageDirectory();
File dir = new File(sdcard.getAbsolutePath()+"/"+path1);
file = dir.toString();
}
So how to get the path from the recent file section in the android SDK versions greater than 23?
String path = uri.getPath();
File dir = new File(sdcard.getAbsolutePath()+"/"+path1);
It makes no sense to try to build up a file system path in this way.
Its the equivalent of all those get real path for uri functions.
Instead you should open an InputStream for the obtained uri and than read from that stream as if it was a FileInputStream;
InputStream is = getContentResolver().openInputStream(uri);
Adapt to modern times and use the ur directly.
Have a look at uri.toString() to see the content scheme you got.

How to access a file in Processing Android Mode

I am trying to access a file in Processing Android Mode using the following:
File file = new File(filePath);
And I tried the following options for setting filePath for the file “1.sf2” in the “data” folder that I created in the current sketch directory. However, none of them worked.
filePath = dataPath(“1.sf2”);
filePath = sketchPath(“data/1.sf2”);
filePath = sketchPath(“assets/1.sf2”);
filePath = “data/1.sf2”;
filePath = “assets/1.sf2”;
According to the github page of Processing Android Mode, everying file in the “data” folder of the current sketch directory are automatically copied to the “assets” folder of the generated apk file. However, I keep getting “java.io.FileNotFoundException: /null/restore_pixels: open failed: ENOENT (No such file or directory)” exception every time I run the application on my Android device.
Is there a way to correctly access a file in Processing Android Mode? Thanks a lot!

Firebase Storage Download getFile(Uri) with "content://" uri throws java.io.IOException: No such file or directory

My use case: Get content from remote only when local uri can't resolve the content.
1) File is available remotely in the Firebase storage with the following reference structure user.uid/files/fileId, example:
ysCwjgc0zGfUNuZimHGaun0MzlP2/files/document-d876e8aa-4b6a-45ed-b2f1-dfd2fbf21360
2) Local device determined the content is not available, attempts to get from Firebase Storage, using the same reference as above.
3) Local creates the file and getUri for the file like below:
// TEST
File f = new File(context.getFilesDir() + "/pictures",fileId + ".jpg");
Log.e(LOG_TAG,"test local file created: " + f.getAbsolutePath());
Uri localUri = FileProvider.getUriForFile(context,FILE_PROVIDER_AUTHORITY,f);
Log.e(LOG_TAG, "test local file content uri created: " + localUri.toString());
4) Tried to use localUri as argument for getFile(android.net.Uri) to get file from Firebase:
but my cat screams the following IO exception.
E/FileRemoteDataSource: Local file not found, trying to fetch from remote...
E/FileRemoteDataSource: test local file created: /data/user/0/com.sample.app/files/pictures/document-d876e8aa-4b6a-45ed-b2f1-dfd2fbf21360.jpg
E/FileRemoteDataSource: test local file uri created: content://com.sample.app.fileprovider/pics/document-d876e8aa-4b6a-45ed-b2f1-dfd2fbf21360.jpg
E/FileDownloadTask: Exception occurred during file write. Aborting.
java.io.IOException: No such file or directory
at java.io.UnixFileSystem.createFileExclusively0(Native Method)
at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:281)
at java.io.File.createNewFile(File.java:1000)
at com.google.firebase.storage.FileDownloadTask.zza(Unknown Source:75)
at com.google.firebase.storage.FileDownloadTask.run(Unknown Source:190)
5) Tries to use f as argument for getFile(File) to get file from Firebase:
I can get the file perfectly fine, my cat is perfectly asleep with just...
E/FileRemoteDataSource: Local file not found, trying to fetch from remote...
E/FileRemoteDataSource: test local file created: /data/user/0/com.sample.app/files/pictures/document-d876e8aa-4b6a-45ed-b2f1-dfd2fbf21360.jpg
E/FileRemoteDataSource: test local file uri created: content://com.sample.app.fileprovider/pics/document-d876e8aa-4b6a-45ed-b2f1-dfd2fbf21360.jpg
// No exception, got file
Note: In my project I have file providing logic somewhere else, the above just a example case.I would prefer to work with content uri as much as I can, what am I doing wrong by using the content Uri? Thx.
Additional Info:
The Uri points to a file residing in app-internal directory, so storage read/write permissions are and should not be required.
I use Firebase to upload the file by its Uri and it was able to locate, read and upload the file perfectly. The same local uri that was provided by the android framework FileProvider.getUriForFile(f).The test file was uploaded successfully like this:
// putFile(uri) where uri references to a local file where to
// upload the file from.
UploadTask uploadTask = resourceRef.putFile(localUri);
Firebase's getFile also suuports File as an argument, so you may want to try it instead

image file path in DCIM is returned as false

I see the image file in DDMS file explorer as /mnt/shell/emulated/0/DCIM/myPicsFolder/, if I write
final String uploadFilePath = "/mnt/shell/emulated/0/DCIM/myPicsFolder/";
final String uploadFileName = "mypic.jpg";
String sourceFileUri = uploadFilePath + uploadFileName;
File sourceFile = new File(sourceFileUri);
if (sourceFile.isFile()) {/*Upload Image*/}
but the if statemente returns false :-/ , what's wrong? I'm testing code in Samsung Core 2 with Android 4.4. I've checked and rechecked if the path is correctly written and it's ok.
what's wrong?
Root paths in adb shell are not the same as root paths for your app. Never hardcode root paths in your app. Always use methods on Environment or Context to get at root paths.
In this case, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) will give you a File pointing to the DCIM directory that you are trying to work with. So, use something like:
File dcim=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File sourceFile=new File(new File(dcim, "myPicsFolder"), "mypic.jpg");

Accessing file from android data directory

i want to access the /data/local/tmp for any random access file
(RAF) file how to get the path of that file?
go this way and read the raf file line by line but be assure that the folder have read permission to other user or you can change it from adb shell by using chmod commmand .....
File finalFile = new File("/data/local/tmp", fileName);
RandomAccessFile rafTemp = new RandomAccessFile(finalFile, "r");
For accessing the any file through code , you must know the path of the file.
and then you can use the code:
File finalFile = new File/t("/data/local/tmp", fileName);
RandomAccessFile rafTemp = new RandomAccessFile(finalFile, "r");
"/data/local/tmp" is the path for the file. It can differ file to file. So you must know the path of file before accessing it.

Categories

Resources