getting data from internal storage - android

I'd like to read all files located in internal storage and filter them to find those *.mp3 ones. With external storage I could just use:
final String MEDIA_PATH = Environment.getExternalStorageDirectory().getPath();
to get the path, but I cannot find the way to do it for internal memory (for instance to get to Music folder of the phone). Is there a way to do it?

Are you aware of getFilesDir() method?
http://developer.android.com/training/basics/data-storage/files.html

You can use Context.getFilesDir() method to get the path to the internal storage.
Like : File file = new File(getFilesDir() + "/" + name);

Related

Get path for data storage

I need to create file in storage card from my Android program. I know I can create this file only in particular location like \Card\Android\data\my.application.package\. How to get this directory exact location? Code below brings path /data/my.application.package/Log and mkdirs() does not creates directories.
String path = "Log";
File root = new File(Environment.getDataDirectory().getAbsolutePath(),
"my.application.package" + File.separator + path);
boolean dirExists = true;
if (!root.exists()) {
dirExists = root.mkdirs();
}
How to get the same application path on internal storage?
You should use getFilesDir() for getting app specific internal File path and getExternalFilesDir() for getting app specific external File path.
Note that on Android 4.3 (API level 18) or lower, your app need to request storage-related permissions to access app-specific directories within external storage.
For more information see https://developer.android.com/training/data-storage/app-specific

Media Controller how to save to internal storage?

I am using Android MediaController. How can I save a recorded voice file to internal storage?
private String FILE;
FILE = Environment.getExternalStorageDirectory() + "/tempRecord.3gpp";
Well here it describes how to save to internal storage
In short use:
File file = new File(context.getFilesDir(), filename);
Where context.getFilesDir() returns a File representing an internal directory for your app.
To write to cache change getFilesDir() to getCacheDir().

How can I get the external application data path in android?

I would like to store some cache data in the path /sdcard/Android/data/, but how can I get it?
simply use Context.getExternalCacheDir(), this is the standard way android provides for external cache, when your app uninstalled, the dir removed automatically and don' t leave any trash for the user.
the dir pattern ends like Android/data/your-package-name/cache/
but you should check null for the return value (a File object), if null, it indicates that the cache dir is not avaialbe(like sd card removed or connected to your pc, etc.)
I am doing this before saving my files to my external folder.
String pathToExternalStorage = Environment.getExternalStorageDirectory().toString();
File appDirectory = new File(pathToExternalStorage + "/" + getText(R.string.app_name));
if (!appDirectory.isDirectory() || !appDirectory.exists()) //Checks if the directory exists
appDirectory.mkdir();

Should I save external data in directory Android/data or data/?

Which is correct,
String filePath = Environment.getExternalStorageDirectory()
+ "/data/com.packagename";
or
String filePath = Environment.getExternalStorageDirectory()
+ "/Android/data/com.packagename";
if I want to store data in external storage? I see many apps are using the second option, but some use the first path.
You should rely on the API to figure out the directory for you:
File externalDir = Context.getExternalFilesDir(null);
Context.getExternalFilesDir will return your 2nd path. Programs that return the 1st path probably hardcoded the path and got it wrong as a result.

Find path of storage in Android

The application that am working has to access the files stored
on a tablet in the internal storage folder. Can any one tell me
how to get access to it?
What about this?
return Environment.getExternalStorageDirectory().toString() + "/Music";
See internal storage section of android data storage document..
EDIT
I think you should use ContextWrapper..Personally I never tried this..
ContextWrapper cw = new ContextWrapper(context);
File directory = cw.getDir("your music folder name here", Context.MODE_PRIVATE);
please check directory is null or not before you use it..
EDIT
See this thread..this might help you
You can use :
File f = new File(Environment.getExternalStorageState());

Categories

Resources