I have a big problem. I want to save some files that I download from internet with my application, into my SD card fallowing this path:
/storage/9016-4EF8/Android/data/com.my.application/files
But I don't want to hardcode this string inside my code. So how can I do it programmatically?
I have already ask permission to write on external storage.
Currently inside my code there is this piece of code below:
File sdcard = new File(context.getExternalFilesDir(null).getAbsolutePath() + "/Video scaricati/");
But with it, my application saves the files inside:
/storage/emulated/0/Android/data/com.my.application/files/Video scaricati
that it isn't in my SD card.
How can I do it?
Take the second item returned by
File dirs[] =getExternalFilesDirs();
Check if the array contains more then one element before use. Not everybody puts a micro SD card in.
Try changing to Environment.getExternalStorageDirectory() like this
new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Video scaricati/");
Related
I have write a code to find if SD card path if available which is like this
File[] paths = ContextCompat.getExternalFilesDirs(context, null);
if (paths.length > 1) {
if (paths[1] != null) {
root = paths[1].getAbsolutePath();
// for sd card
} else {
root = Environment.getExternalStorageDirectory().toString();
}
} else {
root = paths[0].getAbsolutePath();
}
I am saving my data in path "/storage/4130-1912/Android/data/com.enable/files" but I wanted to save data outside Android folder.
I have also tried to make an directory outside the Android folder.But unable to make it.I am testing in Lave phone with version Marhmallow
As you are using Marshmallow, you should be requesting runtime permissions (more details here). Despite that, you may add the WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE for external storage access. You can find more information on how to here.
You can access the external storage path like this: Environment.getExternalStorageDirectory().getAbsoluteFile(). If you are getting FileNotFoundException, it's probably because you didn't add an additional "/" before your file. Example:
File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() +
"/your_file.txt");
I am saving my data in path "/storage/4130-1912/Android/data/com.enable/files"
Ok. Go ahead. You will succeed.
but I wanted to save data outside Android folder.
Well that is not possible anymore on modern Android systems.
Even inside the Android folder you can only write to mentioned private directory for your app.
If you want to write to the whole micro SD card then use the storage access framework.
This will help you
As miguelarc said:
As you are using Marshmallow, you should be requesting runtime
permissions (more details here). Despite that, you may add the
WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE for external
storage access.
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + getPackageName() + "/yourdir");
if (f.mkdirs() || f.isDirectory())
Log.e("path",f.getPath());
//do what you want
}
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
I have a file in the internal storage of my tablet. /myfolder/subfolder/index.html. How can i load this into a webview of an app.
i have tried
webview.loadURL("file:///myfolder/subfolder/index.html");
but it is not giving the expected result. it says web page not available.
I know how to load from the asset folder or from the web/internet. but i need to load local file into webview. is it possible.?
File file = new File("/data/data/packagename/foldername/");
webView.loadUrl("file:///" + file);
I was able to solve my problem by using the following as path:
webview.loadURL("file:///mnt/sdcard/myfolder/subfolder/index.html");
An app cannot access data from the Internal storage stored by another app. Permissions are applied to internal storage that make data written by an application not accessible outside of that application (your app cannot read anything written by another app).
So, if you are accessing a file that is not created by your app, AFAIK, you cannot have access to it.
BTW, you could access the file from the internal storage as below,
webview.loadURL("file:///data/data/com.yourproject.example/files/index.html");
Put your html files in asset folder access the page like given below.
webview.loadURL("file:///"+mContext.getFilesDir()+"/myfolder/subfolder/index.html");
you have to mention the android asset while accessing html pages in android assets.
File gameDir = new File("/data/data/" + getActivity().getPackageName() + "/games");
gameDir.mkdirs();
// Create and execute the background task.
mTask = new DownloadTask();
mTask.execute("https://github.com/gabrielecirulli/2048/archive/master.zip", "/data/data/" + getActivity().getPackageName() + "/games/2048.zip");
mWebView.getSettings().setJavaScriptEnabled(true);
Toast.makeText(MainActivity.this, path+"/index.html", Toast.LENGTH_SHORT).show();
mWebView.loadUrl("file:///data/data/com.example.zipfiledownload/games/2048-master/index.html");
I have an application which has loads of pictures. The total picture is saved in an SD card due to large number of picture. The main problem is that the pictures are available in gallery. I want a way to hide the picture from the gallery but must be available for my application.
I did put " . " in front of the file name to make it hidden
but the images were not displayed in may application also!
Anyone.. help.. please
thanks..
Gallery handles all media files it knows about, so if you want it to stay away of your assets you got two solutions. First, add .nomedia file to the folder with your images, and Gallery should skip it (most image viewers would honor .nomedia, yet there's no guarantee). Other solution is to use some own, proprietary file format, so Gallery or other tools won't be able to process it. I'd recomment .nomedia if that's sufficient solution.
Add .nomedia file into the image folder. The important thing is that, the folder should be empty, when you add .nomedia file. If the folder contains some images before adding '.nomedia' file, the gallery will not hide those images, and it will hide the images added after '.nomedia' file. So The first file added to the image folder should be '.nomedia'.
Hope dis will help you
Thankx,
This line of code can add the .nomedia file in your directory and makes your images hidden from the gallery
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 think the place where you save the images matter, try to save them into application cache directory, or files directory.
you can get access to cache directory using : getCacheDir() in your activity.
and your files directory: getFilesDir()
these two directories must not be visible or accessible to any other applications expect your application,
I used assets to accomplish this in my app.
I've developed an app that records audio and saves sample on sd card. i'd like to add a bit more functionality for example at the moment it save the sample automatically, whereas i'd like the user to be able to specify a filename and then that sample is saved under that name on the sd card. also when playing a sample i'd like the user to be able to choose a sample from a list generated from the available samples on the sd card. this is my problem, i'm not sure which android components to use to generate a list from the samples on the sd card. can anyone point me in the right direction?
thanks
Use a ListView.
With the ListView you will create a list of your SDcard´s media.
ListView Example
UPDATE ::
yes Turtleboy you will save your media files inside a directory
"/sdcard/mymediafiles/"
then extract the file´s name to be displayed in your ListView!
File sdCardRoot = Environment.getExternalStorageDirectory();
File myDirectory = new File(sdCardRoot, "/mymediafiles/");
for (File f : myDirectory.listFiles()) {
if (f.isFile())
Log.i("Jorgesys","*** Media File :: " + f.getName());
}
#Jorgesys suggestion of ListView is fine (+1), but you really need to think about what functionality you want to offer.
A listview would be fine if you just want to list file names.
If you have your files stroed in directories, then maybe a treeview, where you can expand and collapse the nodes (list windows explorer).
If you want to show multiple data about each sound sample (e.g file name, duration, artist, genre, etc) then use a stringgrid