I'm trying to mount an obb file but
std::fstream test("/storage/emulated/0/Android/obb/com.bixense.jngl_test/main.1.com.bixense.jngl_test.obb");
assert(test);
says it doesn't exist (or I can't access it). When looking via adb shell the file is located here:
/storage/sdcard1/Android/obb/com.bixense.jngl_test/main.1.com.bixense.jngl_test.obb
Do I need to give my app some special permission?
I would try to access it in good old C style and print strerror(errno) as in
https://stackoverflow.com/a/504039/755804
Okay I've fixed it using
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in my AndroidManifest.xml. Strange, I thought I've read that this isn't needed for accessing my own files ...
Related
Our application has a large amount of C++ code that creates its own log file as a simple .txt file. It's a circular buffer so it's limited to the size we specify. It's also placed in whatever directory we specify.
The problem is where to place the file so it can be accessed with ADB or a similar tool (without rooting). If we didn't care about the publicly-accessible part, it seems this would be the logical place to locate the file:
packageManager.getApplicationInfo(applicationContext.getPackageName().dataDir
But since we want to be able to pull the file from a customer's phone for post-mortem debugging, I've tried placing it here:
"/mnt/sdcard/Android/data"
This is problematic for several reasons, but I'm not sure if they're all true. (1) It's hard-coded; (2) Not all Android devices have external storage, although I thought they still mapped it to internal storage? (3) The location isn't app-specific so it won't get uninstalled along with the app. And (4) Runtime permission for EXTERNAL_STORAGE is required.
I believe 1-3 can be solved with something like:
android.content.Context.getExternalFilesDir()
Or is there a better choice?
But I don't believe this will get around #4, which is unfortunate as I'd prefer not to "scare" users with more permission requests.
What's the best way to handle this?
Make sure that you have the permissions to read and write the External SD using this code in the Manifest File:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
And then this string will give you the wanted path:
String directory = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + getContext().getPackageName();
/storage/emulated/0/Android/data/com.exemple.yourapp/
I am hoping to develop an application that needs to read a log file from another popular application. The log file is in /android/data/com.xxx.xxx/files.
Initially I thought my application would need root, but using two different file managers on an unrooted phone, I can access the /Android/data/com.xxx.xxx/Files/ directory and read/write the files there.
Everything I read online tells me it shouldn't work that way though. Can someone help clarify things?
The only way to do this is with the FileProvider. Here a good example on how to implement this:
https://developer.android.com/reference/android/support/v4/content/FileProvider.html
Remember to add these two lines in the manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
to have the permission to read the files from other dirs.
To read this path:
"/your_sd_path/android/data/com.xxx.xxx/"
As you tested, root acess is not needed.
But for reading app dir in below path:
"/data/app/com.xxx.xxx/"
You should have root acess.
Hope it helps
I am new to android. I am trying to download a file from server and save the file in a particular path. The application is working fine. However I am not able to find the path of the downloaded file. In the code I have given as
File output = new File("/data/data/com.test.firstApp/", fileName);
Where can i find the file on my system?
Don't use hard coded file paths. The framework will give you the base path of the area you want to save files to.
For the SD card, use Environment.getExternalStorageDirectory()
For local files, use Context.getFilesDir() (or Context.openFileOutput(String name, int mode), etc)
For local cache, use Context.getCacheDir()
Adding to Rich's answer, in the likely event you will end up writing to external storage make sure to include this permission in the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Check this page http://developer.android.com/guide/topics/data/data-storage.html
You can find there many methods how to save file. What is more you can also read something about good practices.
Cheers
in my application i create a temporary file this way
File tmp = File.createTempFile("TEST_", null, getFilesDir());
this resolves in a file that toURI()zed corrisponds to something like
/data/data/it.lorenzoff.test/files/TEST_XXX.tmp
In certain circumstances, i'd like to move this file permanently on sdcard but this code
dest = new File("/sdcard/permanentFile");
tmp.renameTo(dest);
never works.
I'm already using these permissions
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
but renameTo continue returning false.
What i'm doing wrong?
Thanks in advance
L.
The explanation can be found in documentation for File:
Many failures are possible. Some of the more likely failures include:
Write permission is required on the directories containing both the source and destination paths.
Search permission is required for all parents of both paths.
Both paths [must] be on the same mount point. On Android, applications are most likely to hit this restriction when attempting to copy between internal storage and an SD card.
In this case source and target file paths point to different mount points (these two mount points even have different file system). You only option is to manually copy the file to sdcard and then delete the file from internal storage.
This seems like a trivial question, so whoever can answer first and provide me with a resource, I'd be happy to provide you with a green tick :)
How can I write files from within native code? I want to perform some processing in C++ which will output a .txt file, so I'd like to save that to the SD card. (I tried and it told me permission denied).
Thanks!
try to add this to your manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in default, android denies you of writing to external storage unless you specify your desire to..