android download manager - download file into previously created directory - android

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")));

Related

Storing data inside Android/data/pkgname

I am trying to add data into a folder which is inside Android/Data/packagename. So I am trying this out:
String tempsubdirtest = Environment.getExternalStorageDirectory()
+ File.separator + "/Android/data/" + getPackageName() + "/files/Images";
File subdirecttest = new File(tempsubdirtest);
if (!subdirecttest.exists())
{
subdirecttest.mkdirs();
}
The question is that is there an easy way to reduce the code for:
Environment.getExternalStorageDirectory()
+ File.separator + "/Android/data/" + getPackageName()
instead of me typing /Android/data/ +getPackName etc.
Thanks!
You can use this existing method getExternalCacheDir
This will return the directory path plus a cache folder.
So in your case, you can just exclude that cache path.
/storage/emulated/0/Android/data/com.example.test/cache
You might gonna check this, which will return something similar to what you want.
String pathImage = getExternalFilesDir(Environment.DIRECTORY_PICTURES).getPath();
/storage/emulated/0/Android/data/com.example.test/files/Pictures

Different value for 'get/setDestination' Directory

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.

Saving file in sd card without extension and access that with extension

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..

right path to a file in google glass memory

As a Google Glass developer, how can I reach the internal storage of the Glass?
I tried:
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "/DCIM/test.jpg");
but it doesn't work.
I want to be able to reach the photos and videos inside the device.
Thanks!
You should use (note the missing "/" at the begging):
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "DCIM/Camera/test.jpg");
An example path of a picture in the Glass storage is like:
/storage/emulated/0/DCIM/Camera/20140530_181813_833.jpg
you can get it with:
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "DCIM/Camera/20140530_181813_833.jpg");

Creating new file from assets folder, isDirectory() returns false

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();

Categories

Resources