Writing files within native code in Android - android

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..

Related

Where to create a publicly-accessible log file on Android

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/

Android saving file to external storage without adding write permission?

Can we write data to external storage without adding write permission in the manifest ? Does Storage access Framework or Download manager gives any support for this?
It depends where you want to write the files and what SDK(s) you are targeting. If you want to write the files in your app's external directories (getExternalFilesDir(String) and getExternalCacheDir()) and you are targeting SDK19+, then the permission is not required. If you want to write in other areas or targeting SDK<19, then you need the permission. HOWEVER, based on my experience, there is a bug in some Lollipop versions out there that is causing the permission to still be required. So I usually put <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="22"/> in my manifest.
Short answer: no. If there was an easy way to bypass required permissions they'd be pretty pointless. See the documentation below:
https://developer.android.com/training/basics/data-storage/files.html
To write to external storage, you need permission to write to external storage.
Why would you want to take an action you haven't received permission for anyway?
Maybe FileProvider is what you are looking for.
FileProvider is part of Support Library, available for all Android versions starting 2.3. The main goal of this API is to temporary open a private file to some targeted apps: you keep the file in your private folder, and let some other apps read or even write it via a secured ContentProvider. Permissions are revoked when your activity is destroyed.
More info

accessing files in another apps directory

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

android: convert mp3 to wav using Jlayer

I need to convert mp3 to wav in android and i found that it can be possible using Jlayer.
To do this, i coded below, and it looks working for a long time about 30seconds with no error, but the wav file haven't created anywhere. could you advise for me?
Converter converter = new Converter();
converter.convert(sourceName, destinationName);
ps. The souceName path is /mnt/sdcard/mp3/xxx.mp3
and the destinationName path is /mnt/sdcard/mp3/xxx.wav
please, help
Do you add permission at AndroidManifest?
< uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
It could be a variety of things.
You might have a permissions issue: make sure you have
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
in your Android Manifest.
Or it could be the device you're testing on. "/mnt/sdcard" is NOT guaranteed to exist within the Android file system. It is present on most devices, but you shouldn't rely on it. What you should be using is
Environment.getExternalStorageDirectory()
which will return the correct path to external storage.
Or you might be eating any errors JLayer throws. It crashes if the input path isn't correct, or if the file isn't actually an MP3 file.
I'm going to guess it's a permissions error.
Edit: Also you should remove the JLayer tag, it refers to a Swing window decorator and not the MP3 library from JavaZoom :/

Android- Storing Large files at compile time

How exactly does one go about such a simple thing in this beautifully over-complicated framework?
Yes I've read the documentation on Data Storage for Android, about 54.5 times. But I can not find anyplace where the documentation describes how you should go about placing files on the external storage at compile time.
Here's what I want to do: I want to include a couple of (big) (10-20mb) audio files in my application. Naturally, I do not want these to be stored on the internal storage, because they're just too big. So placing them in res/raw is not an option (because, if I understand correctly, things in res/raw will be placed in the internal storage of the phone, correct?)
The documentation only states that "if you want to store static files at compile time, use res/raw". Now if Android is smart enough to place those files on the external storage all by itself then I'm forever greatful.. But somehow I doubt that. All help is appreciated :)
(Sorry if it seems like I have an attitude, I've just spent way too much time on something so simple)
Thanks again :)
UPDATE: I ended up downloading the files from the application instead of including them at install-time. Thanks for the help guys!
I hit a problem while trying to download through a url, spent a lot of time trying to get it to work, and in the end the problem was because I hadn't declared the correct permission in the android manifest file! So to anyone who's looking to download stuff in their apps, do not forget to set the permissions in the android manifest, here are the permissions I needed:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
Place these above the start of the tag
If your large files are in the APK, they'll be stored wherever the APK gets stored - this can be internal or external, and it seems that this is not what you want. The most likely option for you seems to be to place the files on a web site and during the first run of your application, it must notice that the files do not exist and retrieve them post-installation.
how you should go about placing files on the external storage at compile time.
I'm assuming the above is a typo and you mean at install time.
As for your requirement - it's not possible to instruct the Android Application Manager to unpack different parts of an APK to different places during the installation.
Further to this, there's no guarantee that an APK download will go to the internal or external memory storage (where it will stay unless otherwise deleted).
And even further to this, there's no guarantee that even if a device has external storage, it will be available at installation time or have enough free space.
At this point I wonder about audio files which are 10-20MB in size - either they're very long (in duration) or they're encoded at a high bit-rate. If it's the latter then this doesn't make too much sense as most mobile devices have fairly poor audio reproduction (in relative terms)....just some thoughts to mull over.
I personally think mah's suggestion of downloading post-installation may be a better approach but my comments about availability of external storage still hold true.
Proper approach to solving your problem on Android is "don't put them with your application". Just download them on first start from your web server (using HTTP client API) or, if these files will be upgraded independently of the application itself, prepare them as a separate "application" for the user to download via Market.

Categories

Resources