Android: How to access a file in the SD Card - android

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"

Related

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.

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

Android mkdirs() sdcard do not work

I want to make dir in Sdcard, and i do follow:
I added: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in manifest.
I get root_path by: public static final String ROOT_PATH = Environment.getExternalStorageDirectory().toString() + "/Hello_World/"; and it returns
/storage/emulated/0/Hello_World (getting when debug).
Next, I run this code:
File file = new File(Constants.ROOT_PATH);
int i = 0;
while (!file.isDirectory() && !file.mkdirs()) {
file.mkdirs();
Log.e("mkdirs", "" + i++);
}
I also tried both mkdirs() and mkdir() but it's showing endless loop in logcat (Log.e("mkdirs", "" + i++);). Sometimes it work, but sometimes not.
Thanks for you helping!
Update: I tried my code for some devices: Nexus4, nexus7, Vega Iron, Genymotion, LG G Pro, then just Vega Iron work as expected. ??!!?!?
Try like this it will create a folder in the sd card
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/hello_world");
myDir.mkdirs();
If you want to check that file exists or not use this code
File file = new File (myDir, file_name);
if (file.exists ())
// file exist
else
// file not exist
For reference look at this answer Android saving file to external storage
The error is cause by && in while (!file.isDirectory() && !file.mkdirs()) it should be while (!file.isDirectory() || !file.mkdirs()). You should also check if the media is mounted.
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
if (DEBUG) {Log.d(TAG, "createSoundDir: media mounted");} //$NON-NLS-1$
File externalStorage = Environment.getExternalStorageDirectory();
if (externalStorage != null)
{
String externalStoragePath = externalStorage.getAbsolutePath();
File soundPathDir = new File(externalStoragePath + File.separator + "Hello_World"); //$NON-NLS-1$
if (soundPathDir.isDirectory() || soundPathDir.mkdirs())
{
String soundPath = soundPathDir.getAbsolutePath() + File.separator;
if (DEBUG) {Log.d(TAG, "soundPath = " + soundPath);} //$NON-NLS-1$
}
}
}
Cut and paste from one of my project.
Thank all you guys, finally i found out the problem. The problem is in the while() loop, I replace by
if
(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
&& !file.isDirectory()) {
file.mkdirs(); }
Use Environment.getExternalStorageDirectory().getAbsolutePath() as below...
public static final String ROOT_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Hello_World/";
And Check that SDCard is mounted before creating directory as below....
File file = new File(Constants.ROOT_PATH);
int i = 0;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
if(!file.exists()) {
file.mkdir();
}
}

How to rename video file in android

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

File.mkdir() and mkdirs() are creating file instead of directory

I use the following code:
final File newFile = new File("/mnt/sdcard/test/");
newFile.mkdir(); // if I use mkdirs() result is the same
And it creates an empty file! Why?
You wouldn't use mkdirs() unless you wanted each of those folders in the structure to be created. Try not adding the extra slash on the end of your string and see if that works.
For example
final File newFile = new File("/mnt/sdcard/test");
newFile.mkdir();
When I need to ensure that all dirs for a file exist, but I have only filepath - i do
new File(FileName.substring(0,FileName.lastIndexOf("/"))).mkdirs();
First of all you shouldn't use a file path with "/mnt/sdcard/test", this may cause some problems with some android phones. Use instead:
public final static String APP_PATH_SD_CARD = "/Test";
String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + APP_PATH_SD_CARD;
It creates an empty file since you added the dash.
Now that you have your path use the following code:
try {
File dir = new File(fullPath);
if (!dir.exists()) {
dir.mkdirs();
}
}
catch(Exception e){
Log.w("creating file error", e.toString());
}
Try to use
String rootPath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/test/";
File file=new File(rootPath);
if(!file.exists()){
file.mkdirs();
}

Categories

Resources