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");
Related
Here is my code:
File dir = new File(Environment.getExternalStorageDirectory(), "newDir");
if (!dir.exists()) {
System.out.println(dir.mkdirs());
}
It always prints false.
In my AndroidManifest.xml I have the following line:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I have also tried the following variations of the first line of the code:
File dir = new File(Environment.getExternalStorageDirectory() + File.separator + "newDir");
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/newDir");
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/newDir/");
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "newDir");
File dir = new File(Environment.getExternalStorageDirectory() + "newDir");
File dir = new File(Environment.getExternalStorageDirectory().toString() + "/newDir");
Nothing works.
EDIT: Device OS version is 6.0.1 (a Nexus 9 tablet).
The issue is due to changed permission model since Android M.
You need to ask for the permission programmatically even though you've declared it in Manifest file.
Checkout my answers here and here for more details.
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.
I'm making an app that let's you either capture photo/video, or choose an existing photo/video before sending. I set up the directory to save the files in here:
String appName = Main.this.getString(R.string.app_name);
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES), appName);
and I name the files here
File mediaFile;
Date now = new Date();
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(now);
String path = mediaStorageDir.getPath() + File.separator;
if(mediaType == MEDIA_TYPE_PHOTO){
mediaFile = new File(path + "IMG_" + timestamp + ".jpg");
} else if(mediaType==MEDIA_TYPE_VIDEO){
mediaFile = new File(path + "VID_" + timestamp + ".mp4");
}
So it's saving my pictures under /storage/emulated/0/pictures/(app name) with correct timestamped format. However, my videos are being saved to /storage/emulated/0/DCIM/100MEDIA and are just being named VIDEO0073, VIDEO00074, etc. I tried changing the directory name to MOVIES instead of PICTURES or DCIM, but there is no effect. I'm on an HTC One running Android 4.3
This is a bug that occurs on certain devices including the HTC One. If you include your code where you declare your video Intent I could more precisely answer the question, but basically, after you declare your Intent for the video (not image), get your Uri like so:
videoUri = getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, new ContentValues());
In this case the video will be saved in the /storage/emulated/0/video folder instead of the pictures or dcim folders you mentioned above.
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 try to rename video file with this code:
File from = new File(outputFileName);
File to = new File(mediaStorageDir,mediaFile);
from.renameTo(to);
when
outputFileName = //mnt/sdcard/Movies/Your_voice/Your_voice.mp4
and
mediaFile = mediaStorageDir.getPath() + File.separator
+ "Your_voice" +
timeStamp +
".mp4";
and
mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES),
"Your_voice");
no error in locat .... but not execute rename.
I thought this problem cause because this file play in videoview so before the code I add videoView.setVideoPath(""); but it dont help, what I need to do ???
thanks ahead...
You already specify the mediaStorageDir when constructing the File object:
File to = new File(mediaStorageDir,mediaFile);
so you should remove the mediaStorageDir.getPath() from mediaFile, as follows:
mediaFile = "Your_voice" + timeStamp + ".mp4";
You should probably also remove the Your_voice part from the mediaStorageDir, just use:
mediaStorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);