Tested in Emulator
At first I make a directory in this form :
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/" + "SubFolderName");
if (!dir.exists()) {
dir.mkdirs();
That make directory in this path : /storage/emulated/0/SubFolderName
Now, I need to use this path for downloaded file, so add this code:
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDestinationInExternalPublicDir(root.getAbsolutePath() + "/" + "SubFolderName","FileName");
That return this path: /storage/emulated/0/storage/emulated/0/SubFolderName/
Too try before this:
request.setDestinationInExternalFilesDir(getActivity(), root.getAbsolutePath() + "/" + "SubFolderName","FileName");
This section are repeated : storage/emulated/0
I check my codes but there-are not mistake.
You are not specifying which directory in external storage.
There are several directories you can use depending on what you are storing e.g "Downloads", "Video"
You can use the downloads directory like this for example:
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdirs();
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS + "/" + "SubFolderName/" + mTedListModel.get(position).getTitle()).mkdirs();
And then set your destination with the request manager as
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, + "SubFolderName/" + mTedListModel.get(position).getTitle());
You are using setDestinationInExternalPublicDir(), as i can read here it is already adding the external root dir so you only need to especify the directory type,subpath and file name.
Example:
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOCUMENTS,"SubFolderName/FileName");
Hope im not wrong.
Related
I have a file in my internal storage, like folderOne/folderTwo/textfile.txt
How can I access this file programmatically?
String yourFilePath = context.getFilesDir() + "folderOne /folderTwo/" + "textfile.txt";
File yourFile = new File( yourFilePath );
You want something like this?
I am downloading a pdf file from server and saving it on sd card without extension (for security purpose so that normal user can't open that file from file manager).For e.g.- I am downloading abc.pdf and saving it on sd card with abc on below path
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File folder = new File(extStorageDirectory, "files");
And later I want to view that file by adding extension.
So when I am accessing that file from code by using below code then its gives error file does't exist..
String s=Environment.getExternalStorageDirectory() + "/files/" + "abc";
File pdfFile = new File(s+".pdf");
So how can I save file in sd card without extension and later open that file with extension.
Workaround for your problem is to rename your saved pdf file without extension to filename with .pdf extension & after completing/accessing view or read operation on your new pdf extension file again you can rename it to file without extension
Example Usage:
String currentFileName = Environment.getExternalStorageDirectory() + File.separator
+ "files" + File.separator + "abc";
String renamedFilename = currentFileName + ".pdf";
boolean isRenamed = renameFile(currentFileName, renamedFilename);
Log.d("isRenamed: ", "" + isRenamed);
if(isRenamed {
//perform your operation
}
Again rename the file if not in use (here exchange renameFile() parameters):
boolean renameAfterOperation = renameFile(renamedFilename, currentFileName);
Log.d("renameAfterOperation : ", "" + renameAfterOperation );
And here is renameFile(currentFilePath, renamedFilePath):
public boolean renameFile(String currentFilePath, String renamedFilePath){
File currentFile = new File(currentFilePath);
File newFile = new File(renamedFilePath);
boolean isrenamed = currentFile.renameTo(newFile);
return isrenamed;
}
In this way whenever you want to perform operation on saved file first rename it to .pdf & whenever it's not in use, again rename it without extension. Let me know if this works for you..
When I call this :
File directory = new File("file:///android_asset/");
then this :
directory.isDirectory()
Always returns false.
Am I doing something wrong here ?
(Sorry if it is a dumb question)
Edit:
I store several images in the assets folder and I wanted to load them in my app (like an image gallery).
Call
directory.mkdirs();
after you created the File object
Edit:
in your case try to change file url as below:
new File("/mnt/external_sd/");
or find the true path by looking it in eclipse File explorer.. do not start it with file://
May be you are searching for this:
String fullPath = "/data/data/" + this.getPackageName() + "/" + path;
File dir = new File(fullPath);
if (!dir.exists())
dir.mkdir();
I want to save the downloaded file into a custom folder previously created as :
String trainingDirectory = "swimmer" + File.separator + "trainings";
String trainingsPath = Environment.getExternalStorageDirectory().toString() + File.separator + trainingDirectory;
File trainingSubdirectory = new File(getFilesDir() + File.separator + trainingsPath );
trainingSubdirectory.mkdirs();
to store the downloaded file into this directory, I tried to follow the solution given : Set custom folder Android Download Manager
writing
request.setDestinationInExternalPublicDir ( "/trainings", "mydownloadedfile.mp4");
In this case , the download manager is creating a new 'training' directory , not using the one I created previously...
I tried also to use
request.setDestinationInExternalPublicDir ( "/swimmer/trainings", "mydownloadedfile.mp4");
but in this case an error is raised ( a path with separators is not accepted..)
where am I wrong ?
Use this:
String directoryPath = Environment.getExternalStorageDirectory() + "/swimmer/trainings/"
// ...
request.setDestinationUri(Uri.fromFile(new File(directoryPath + "fileName.ext")));
i would download with DownloadManager a file in a specific directory. At the beginning of my app, i create (if doesn't exist) a folder:
if (storageState.equals(Environment.MEDIA_MOUNTED)) {
externalStorageWriteable = true;
directory_path = Environment.getExternalStorageDirectory()
+ "/" + context.getResources().getString(R.string.app_name);
directory = new File(directory_path);
if (!directory.exists()) {
directory.mkdirs();
}
}
if i check with a filemanager, folder it's available at /sdcard/folderName but if i print on logcat directory_path variable, i obtain
/storage/emulated/0/folderName
I try to download file by passing destination folder like
Uri destination = Uri.fromFile(new File(directory_path));
request.setDestinationUri(destination);
//request is of type DownloadManager.Request
but when i check where file it's stored, my folder is empty and downloaded file is stored inside /sdcard/Download
Could someone tell me why and how can i solve this issue?
Add .toString()
Environment.getExternalStorageDirectory().toString()