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.
Related
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.
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");
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);
i have a very small problem. I am writing a file to sdcard. I'l show you two code, one code works and other doesn't. It looks like this:
First one,
new FileOutputStream("/sdcard/HelloWorld.txt")
This works fine and creates a HelloWorld.txt file in sdcard.
Now second one,
new FileOutputStream(android.os.Environment.getExternalStorageDirectory()+java.io.File.separator + "filetest" + java.io.File.separator + "HelloWorld.txt")
This throws error "/mnt/sdcard/filetest/HelloWorld.txt (No such file or directory)".
I want to know why because i have mnt/sdcard path on my device, is it that it cannot find filetest folder if yes then isn't it supposed to create filetest folder if its not created before.
Thanks.
First Make a directory of filetest if its not available,
File file = new File(android.os.Environment.getExternalStorageDirectory()+java.io.File.separator + "filetest");
file.mkdir();
Then execute your code...
OR
File f = new File(android.os.Environment.getExternalStorageDirectory()+java.io.File.separator + "filetest" + java.io.File.separator + "HelloWorld.txt");
if (!f.getParentFile().exists());
{
f.getParentFile().mkdir();
}
Yes........ filetest folder are not there so you need to create it manually or programatically.and try that code...so you get success.
you can also create dir like this ::
File wallpaperDirectory = new File("/sdcard/filetest/");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
// create a File object for the output file
File outputFile = new File(wallpaperDirectory, filename);
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);
Use Permission :::
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I'm trying to access the image on the SD card using below code but I"m getting File not found exception. Can someone tell me a correct way to acess the file???
new File("/mnt/sdcard/splah2.jpg")
Try like this:
String SD_CARD_PATH = Environment.getExternalStorageDirectory().toString();
new File(SD_CARD_PATH + "/" + "splah2.jpg");
Try running: new File(Environment.getExternalStorageDirectory() + "/splah2.jpg")
try this,
File f=new File("/sdcard/splah2.jpg");
The code below has worked for me.
String mRelativeFolderPath = "/DCIM/Camera/"; // i.e. SDCard/DCIM/Camera
String mBaseFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + mRelativeFolderPath;
String filePath = mBaseFolderPath + "test.jpg";
File handle = new File(filePath);
Shash
try
new File("/sdcard/splah2.jpg")
or
new File(Environment.getExternalStorageDirectory() + "/" + "splah2.jpg")
both are same if SD card is install because,
Environment.getExternalStorageDirectory() returns "/sdcard"