read file from external storage - android

I simply cannot find how to get one specified file from the external storage.
I know that with getExternalStoragePublicDirectory(), you get the external storage directory but I can't get further. I need some kind of method where you have to give the name of the file and it returns the file.
Thanx

Better than using File.separator is this, which is standard Java:
final File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath(), filename);

You can just do this:
File f = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + File.separator + fileName);

Better still:
final File file = new File(Environment.getExternalStorageDirectory(), filename);

I also use getExternalStoragePublicDirectory() and everything goes well. My filename is "DE Disimpan"
File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File filepath = new File(file,"DE Disimpan");

Related

Creating a directory on external sd card that will have a text file written to it

I am trying to create a folder on my external SDcard that will contain a text file that I can pull off my phone from my PC and read it.
I have pieced together a snippet that I believe should work, but I cannot find the folder that is supposedly being created.
File path = Environment.getExternalStorageDirectory();
File dir = new File(path.getAbsolutePath() + "/iptestdata/");
if (!dir.exists()) {
dir.mkdirs();
}
String fileName = new SimpleDateFormat("yyyyMMddHHmmss'.txt'").format(new Date());
final File file = new File(dir, fileName);
As I am stepping though the debugger in Android Studio, I see the path being created and stored in dir is /storage/emulated/0/iptestdata, and I see the file name appearing in the format that I requested, but I cannot find the file via File Explorer in Windows. How can I fix this?
If you create File and store into storage Directory you want to call mkdirs()
File sdcard = Environment.getExternalStorageDirectory();
File dir = new File(sdcard.getAbsolutePath() + " /dir - name /");
dir.mkdir();
File file = new File(dir, "File-Name.txt");
FileOutputStream os = outStream = new FileOutputStream(file);
String data = "This is the content of my file";
os.write(data.getBytes());
os.close();
Please check the Directory is created or not the following method
public static boolean canWriteOnExternalStorage() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
Log.v("Activity", "Yes, can write to external storage.");
return true;
}
return false;
}
Add Permission in your AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

How to rename File From filepath in Android?

I have Path Which is stored into one variable That is
String Path:
String filepath="/mnt/sdcard/DCIM/Camera/1396854069062.jpg";
Now i want to rename only file that is 1396854069062.jpg I Reilly don't have any idea for how to do that.
My Code Is:
File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard, filePath);
File to = new File(sdcard, "RChat_Rename.jpg";
from.renameTo(to);
Any help will b appreciated.
Thank You
Try this way,
File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard,"/1396854069062.jpg");
File to = new File(sdcard,"test.jpg");
from.renameTo(to);
Do not forget to add below permission in android manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Edit
String filepath= Environment.getExternalStorageDirectory() + "/DCIM/Camera/";
File from = new File(filepath,"1396854069062.jpg");
File to = new File(filepath,"test.jpg");
from.renameTo(to);
Try as below...
File fileDir = Environment.getExternalStorageDirectory() +"/DCIM/Camera/";
File from = new File(fileDir, "1396854069062.jpg");
File to = new File(fileDir, "RChat_Rename.jpg";
from.renameTo(to);

Android Subfolder sdcard

I create a folder called "res" inside sdcard
File f = new File(Environment.getExternalStorageDirectory() + "/res/");
f.mkdirs();
The folder is successfully created.
But when I try to create a file within the folder does not work.
String string = "hello!";
File file = new File(Environment.getExternalStorageDirectory() + "/res/","test.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write(string.getBytes());
fos.close();
LOGCAT
java.io.FileNotFoundException: /mnt/sdcard/res/test.txt (Not a directory)
The call to new File() doesn't actually create the file. You need to check if the file exists by using file.exists(). If it doesn't, you must create the file by calling file.createNewFile(). See this answer for sample code: https://stackoverflow.com/a/7012122/379245

Android: how to set the path for saving files etc like in windows we write c:/someting/...what will I do in android

I am trying to record voice and save it in a file. For this I have to give a path to save the file, but I don't know how will I set the path...I have recently started working on android phone. In windows we set path like a drive e.g. C:/folder/folder....in android phone what will be my root? Where can I save an audio file? Write now I am working on emulator...Will the path be same for both emulator and phone?
we can create file like this in SD card
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/folder");
myDir.mkdirs();
String fname = "file";
File file = new File (myDir, fname);
file is the path of file where you stored.
and add this Permission in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
_path=Environment.getExternalStorageDirectory().getPath() + "/RECORDS/";
// || ||
// VV VV
// storage path folder name
fileName = String.format("filename.mp3");
File file = new File(_path, fileName);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Save your file in Sdcard.
File path = Environment.getExternalStorageDirectory();
String cardName = path.getName();
path=cardName+"/mypathname/file_name.mp3";
It is also possible in emulator. create sdcard , when you create emulator.
Thanks
Uri outputFileUri;
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "myDir" + File.separator);
root.mkdirs();
sdImageMainDirectory = new Filenter code here(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);

Android - downloading file, java.io.FileNotFoundException

I may be way off here, but I am trying to download a file and store it in the downloads folder on my phone. I am getting a "java.io.FileNotFoundException" error, because the file doesn't exist, because I'm trying to download it...what am I doing wrong?
String PATH = Environment.getExternalStorageDirectory() + "/download/";
File dir = new File(PATH);
dir.mkdirs();
File outputFile = new File(dir, "downloadFile");
FileOutputStream fos = new FileOutputStream(outputFile);
This fails, with the following:
java.io.FileNotFoundException: /mnt/sdcard/download/downloadFile (Permission denied)
I am using the WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions....
Please try following updated code,
String PATH = Environment.getExternalStorageDirectory() + "/download";
File dir = new File(PATH);
dir.mkdirs();
File outputFile = new File(dir, "downloadFile");
if ( !outputFile.exists() )
{
outputFile.create();
}
FileOutputStream fos = new FileOutputStream(outputFile);
There is a "/" after the download. This way Android is thinking that you are creating Recursive Directory. While using mkdirs(), you can not create recursive directories.
You can also check my answer for same in Java ME here.
Add Permission to write external memory in your Manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Here you want to make directoy?
it there is already folder of download than use this code directly.
// create a File object for the parent directory
File Directory = new File("/sdcard/download/");
Directory.mkdirs();
// create a File object for the output file
File outputFile = new File(Directory, downloadFile);
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);
It might be wise to use Environment.getExternalStorageDirectory() for getting the "SD Card" directory as this might change

Categories

Resources