How to get Internal and External SDcard path ? - android

I want to open file manager on a button click and want to get the path of a particular item on selection of that particular item.

File f=new File(Environment.getexternalstoragedirectory());
btn.setonclicklistener(new Onclicklistener){
onClick{
if(f.isDirectory){
}else{
//do what u want to do with file
}}}

To get SDCard path....
String sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath();

This will give you the "files" directory for your app external / internal.
context.getExternalFilesDir(null);
context.getFilesDir();
There's no built in android file picker. You need an entire module / activity. You can perhaps get one searching github, like this one here sounds like what you're looking for.

You can ask the user to select a file using the new Intent.ACTION_OPEN_DOCUMENT intent in KitKat.

Related

Where should I choose to save text file in android?

I hope to export my data as a text file and save it to disk in Android, so I need to choose which folder I will save the file to.
I hope that a normal user can find the folder easily and the app does not need special permission to create the folder.
I have read some document, it seems that there are 3 ways: Context.getFilesDir().getAbsolutePath(), Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null).
You know some android users don't install SD card, so it seems that Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null) are be excluded.
Am I only to choose Context.getFilesDir().getAbsolutePath()? or is there a better way? Thanks!
BTW, From the document Android - Where to save text files to?
Save it in internal phone storage, here no users and applications can access these files(unless if phone is rooted). But these files will be deleted one's the user selectes clear data from Settings -> Apps -> .
It seems that normal users can't access the saved text files if I use Context.getFilesDir().getAbsolutePath(), is it right?
Use this if you want a path that the user can modify and can have access
getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();
More documentation here.
EDIT:
This is how use in case error in some devices:
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
String fname = "TEXT.txt";
File file = new File(path, fname);
if (!path.exists()) {
//noinspection ResultOfMethodCallIgnored
path.mkdir();
}
// YOUR CODE FOR COPY OR CREATE THE FILE TXT in PATH WITH THE VARIABLE file ABOVE

To show multimedia content(e.g audio,video,jpeg) from custom folder to a Dialog as file explorer.

I am creating an android app, in which i have created a custom location in external storage ,for example /storage/emulated/0/myfolder/. And it contains my app files which are video,audio and pictures(.mp4,.3gpp,.jpeg).
Is there any way to show the contents of myfolder to a popup or some dialog in list manner.Which will behave like explorer and upon clicking any item in the list ,it will run in appropriate application(e.g video player,audio player or picture viewer).Any help would be appreciated.
File dir = new File(
Environment
.getExternalStoragePublicDirectory(File.separator),
"custom folder name");
String[] files = null;
if (dir.exists()) {
files = dir.list();
}
Now with this files array i extraceted each element and checked its extension to get the mime type and showed accordingly.It worked for me.Most simple code for what i want to achieve.

how to hide images from android gallery

One part of my current project is downloading images from URL then saving to SDCard. But the problem is all of saved images in sdcard is displayed in android gallery page. What I want is "I don't want all of my saved images in android gallery."
Add ".nomedia" file. Did not work? Try the second option.
Save your files to a folder that starts with ".", e.g., /sdcard/.myimages/
You can create .nomedia file using this code:
String NOMEDIA=".nomedia";
File Folder = new File(Environment.getExternalStorageDirectory() + "/mydir");
if(Folder.mkdir()) {
nomediaFile = new File(Environment.getExternalStorageDirectory() + "/mydir/"+ NOMEDIA);
if(!nomediaFile.exists()){
nomediaFile.createNewFile();
}
}
I found a simple way(I think so).
Create a folder in 'Android' folder(where data & obb folders are present) and put your media(pics,videos, etc.,) in the folder you've created.
Gallery app ignores that 'Android' folder. Simple :-)
its so late and i know other answers are complete but i think putting .nomedia file makes all pictures in that particular folder hidden :
just change extension of image file programmatically and this way gallery cant detect it as an image . eg if your image is "pic1.jpg" rename it to "pic1.aaa";
and when you want show it again rename it again

Problems reading file from ddms in android?

The file i want to open is located in the file:
/data/data/MY_PACKAGE/files/samplefile.txt
I have pulled the file from ddms and the desired content is in it
I just want to get the text file in order to parse it..
I tried the following:
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File xmlFile = new File(sdcard,"samplefile.txt");
but no joy...
Thanks in advance!!
Use getFilesDir(), not Environment.getExternalStorageDirectory(), to get at the root of internal storage, where your file resides.
In order to open a file stored within your applications private directory you must use openFileInput(String fileName). This will return a FileInputStream that you can then work with.

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