Get internal as well as external storage file path - android

In my app, i need to read external as well as internal filepath. In other words, the user can select a file from internal storage if he desires or from sdcard if he so desires.
I found this while searching in SO:
Use getExternalStorageDirectory() to get access to external storage.
Use getFilesDir() to get at your app's portion of internal storage.
But how would i know whether the user has chosen a file from external or internal directory?

Here is a sample method that reads an image from internal storage;
public Bitmap readFromInternalStorage(String filename){
try {
File filePath = this.getFileStreamPath(filename);
FileInputStream fi = new FileInputStream(filePath);
return BitmapFactory.decodeStream(fi);
} catch (Exception ex) { /* handle exception like a king here */}
return null;
}
Now, what you could do from here is to see if the value is null and if so, then try to use a corresponding method to read from External Storage!
Of course, you can also have class variables to keep track of which source the image/file came from (internal or external) and update them accordingly depending on which method was called!
I hope this helps you solve this issue! Good luck.

But how would i know whether the user has chosen a file from external or internal directory?
That is up to you in your user interface. You are the one who is building the file-browsing UI, and so you know what root directory you used in your file-browsing UI.
Of course, there will not be any files in any of those locations, most likely.
getFilesDir() is part of internal storage, and the only files that will be in there will be ones that you place there yourself.
getExternalFilesDir() is part of external storage. In theory, the user could put files in this directory using other tools, like a desktop OS. In practice, the user is unlikely to do this, and nothing else is going to put files in that directory other than your code.
You are certainly welcome to write a file-browsing UI that uses other external storage locations, such as Environment.getExternalStoragePublicDirectory(), that are more likely to actually contain files.

Related

How to get path of directory which is in SD Card?

I want to create directory in SD card same as in the internal storage.
My internal storage path is "sdcard/<my_directory_name>/"
I want to create the same directory in root of SD card.
I have try the following ways to find the path of directory.
sdcard1/<my_directory_name>/
Environment.getExternalStorageDirectory() +"/<my_directory_name>/"
Please suggest the way to find SD card path.
You do not have arbitrary access to removable storage on Android 4.4+. Hence, there is no useful path, from a filesystem standpoint. You are welcome to use getExternalFilesDirs() and kin -- if they return 2+ locations, the second and subsequent ones are on removable storage, and you can read and write to those locations.
Although as CommonsWare answered, you cannot read or write in Secondary External Storage, here's a way to generate it's path.
System.getenv("SECONDARY_STORAGE"); //returns /storage/extSdCard
You can also use String path = "/storage/extSdCard"; but again you cannot write files there.
Edit: As #CommonsWare commented, there is no guarantee of this method. Though when I tested it, it worked, but again, if he says there's no guarantee, you can take his word.

How to access to internal storage in Android

I want to store .docx files generated and modified through an Android app into the internal storage of android phones (all the same model of phone if that helps). I've got the loading of the template (located into the app) and the modifying of the fields working, but I'm blocked on how to save the files.
I am using docx4j which has a method for saving :
private void writeDocxToStream(WordprocessingMLPackage template,
String target) throws IOException, Docx4JException {
File f = new File(target);
template.save(f);
}
I have tried other more "manual" ways, but I always have the same problem :
what should be the input for the target directory?
It seems easy to do it for external storage, but concerning internal storage it's something else.
When I simply try to output a String in a test.txt then get the String in it back, with the classical BufferedReader, InputStreamReader, FileInputStream way of doing, I can't get this information whatsoever.
Information on this topic is super scattered, I've passed the afternoon reading about thousands of ways to achieve storage, but none corresponds to my problem.
I have set <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in my Manifest just to be sure but it doesn't change anything.
Moreover, and maybe it has to do with the issue, I can't access files (from device or from computer) on my testing device (a Samsung Galaxy 5S which can run the app), I seem to have quite all folders but not any files in them, apart from a few xml and txt files... (However, I still get nothing when doing the : write in EditText - store in File - save File in .txt - retrieve String - view String in textView).
This is super important as I have to retrieve the .docx files for further use, after them being created.
Thank you for your answers and let me know if I can provide any more information, I know my problem is quite unclear.
It seems easy to do it for external storage, but concerning internal storage it's something else.
To write to external storage, you use the two-parameter File constructor, where you provide a File object pointing to some root location on external storage, such as:
new File(getExternalFilesDir(null), target);
To write to internal storage, you use the two-parameter File constructor, where you provide a File object pointing to some root location on internal storage, such as:
new File(getFilesDir(), target);
You don't have to hold the permission android.permission.WRITE_EXTERNAL_STORAGE if you write your file to the app's private internal storage.
In your code, with the call
context.getDir("testfile", Context.MODE_WORLD_READABLE)).getPath().toString()+"/test.docx";
the file will write to the path
/data/data/com.yourpackage.name/app_testfile/test.docx
If the write operate successfully, you can read and retrieve the content of the file by using the path above.

Record and playback a sound on Android without storing a temporary file

In my app I want to record a 2-second long sound and play it back to the user. All examples I've found so far require that the audio data for playback either comes from a file or from a URL.
Both MediaPlayer and SoundPool classes accept only files, file descriptors, resource id's or URLs as input and not just, say, a byte array.
Since one can only write files to the SD card and NOT internal storage (is this so?), the app would require that an SD card is mounted and writable. But my app should also work if no SD card is present.
Is there a way to achieve this?
Thanks
Yes, an App can write to internal storage (where it is installed). Every Installed app is provided a Data folder to write its internal files. Also there is a cache storage provided.
These methods of Context can get you these directories:
getCacheDir() : Returns the absolute path to the application specific cache directory on the filesystem (Note: System may delete cache if its low on storage)
getDir(String name, int mode) : Retrieve, creating if needed, a new directory in which the application can place its own custom data files.
Also there is a method for External storage: getExternalCacheDir() but is unreliable since external storage might not be always there.
Also, if you just need to write files in App's internal data directory, there is a very simple method:
openFileOutput (String name, int mode)
You can use
'getApplicationContext().getFilesDir().getAbsolutePath()'
to get the path of the directory where the app is installed.
One limitation is, 'getFilesDir()' gives you the path to the directory on the file system where files created with openFileOutput(String, int) are stored.

Saving files in flash memory

how can I get the path for the folder where my app can save large JPG files?. getExternalStorageDirectory() works fine only when SD Card is present, but what happens when SD is removed or the harware don't have SD Card slot.
thanks
You can use the getFilesDir() method of a Context. From a context you can also use methods to get the cache directory, external cache directory, and the external files directory. An Activity is also a context, so you can use these methods from inside one.
The getFilesDir() method gives you the folder where your files will be accessible only from your application and will be always available. However, you should use the cache directory instead, when possible. This way you will avoid making the system run out of space.
EDIT:
My answer: Almost always a device will either have an SD card or built-in external storage. When it's built-in, it's still called external storage. To check whether the external storage is removable (SD card) or built-in you can use isExternalStorageRemovable() in Environment.
Basically, you shouldn't place large files on the internal memory. There is no public folder in the internal memory. If a device doesn't have external storage, it's simply not capable of doing certain things. Simple as that. So one option you have when there is no external storage is to inform the user about it and ask them to insert a card. You don't have to handle this case, let the user handle it.
The answer you asked for: Try using getDir(String name, int mode) and/or openFileOutput(String name, int mode) of a Context object, and for mode use MODE_WORLD_READABLE or MODE_WORLD_WRITEABLE. Also check Using the Internal Storage.
You are facing intended limitations of the platform that are there for the good of everyone.
You could either require external directory to exist, or you can store to the internal directory. If you choose to permit both, I suggest you store a flag in internal space to indicate that you've stored something externally, so that if external storage is not present you can take appropriate action.
As you are Saving Large JPG files, its is better to save it in external storage because Phone has very small internal memory and its all effect the performance of phone.

Saving public files in the internal storage

I have an application that saves files downloaded from a server. These files are not private to my application and should be accessible to other applications as well. I want to know what would be the correct path to save these files when the SD card is ABSENT. For the SD card, there is the well known API -
getExternalStorageDirectory()
For the application's private data in the internal memory there is -
Context.getFilesDir()
On some devices, the internal memory is represented by /emmc/.
It will be really helpful if someone could elaborate on /emmc/. I know it stands for embedded Memory card and is not present in all the devices. But is it really representative of the internal memory? Or is it the third memory?
Should I save the files using openFileOutput() with MODE_WORLD_READABLE?
I realize this is old and has an accepted answer, but I think the question is still relevant and that there is a way to achieve what the asker wants.
To paraphrase, I believe the asker wants to save a file, and make it accessible (e.g. for viewing) by other apps, without having to think about external storage (e.g. an additional permission and the possibility that the external storage is not present).
It turns out that you can make your own files readable by other apps.
File file = new File( ctx.getCacheDir(), "picture" );
...
file.setReadable( true, false );
// makes file readable to other apps
// Get another app to view my picture
Uri uri = Uri.fromFile(file);
Intent intent = new Intent( Intent.ACTION_VIEW );
intent.setDataAndType( uri, "image/*" );
ctx.startActivity( intent );
The setReadable made the above code work for me - without that line the viewer app couldn't read the file. Note that setReadable operates on the whole directory, so the best solution is probably to create a sub-directory name 'public' and put your files in there.
On some devices, the internal memory is represented by /emmc/.
/emmc/ may exist on some devices, and it may be internal, but it may not be accessible to applications, and it certainly is not part of the Android SDK unless that happens to be what getExternalStorageDirectory() returns.
But is it really representative of the internal memory?
No.
Or is it the third memory?
Ask your device manufacturer.
Should I save the files using openFileOutput() with MODE_WORLD_READABLE ?
That is impossible to answer in the abstract. You say that your files "should be accessible to other applications as well", but you have not indicated why you expect any other application to care one bit about your files. Other applications will not be scanning your directories for files -- at best, they will allow users to browse external storage. The only reason to have a file that is MODE_WORLD_READABLE is if your application will be triggering another application to do something with the file (e.g., ACTION_VIEW Intent).

Categories

Resources