How to rename File From filepath in Android? - 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);

Related

Android file exists problems

When i make File file = new File(etc..) i actuly created that file on SD card? Iam bit confused because everytime my condition is true and program jumps in if tree but in that time there is no file on SD card...
String filename = "pictures.data";
String root = Environment.getExternalStorageDirectory().toString();
File dir = new File(root + "/courier/saved/");
File file = new File(dir,filename);
if (file.exists())
// program jumps here
else{
}
OK so you can see the file in a dir /courier/saved/. Anyway here's my answer
String filename = "pictures.data";
String root = Environment.getExternalStorageDirectory().toString();
File dir = new File(root + "/courier/saved/");
File file = new File(dir,filename);
file.mkdirs();
if (file.exists())
// program now stops here
else{
}
Best would be to create the dir beforehand maybe, But this creates it on the fly

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

How to check if a file exists in a directory in sd card

I want to check whether a given file exists in android sd card. I am trying it out with creating a file using the absolute path and checking with file.exists() but its not working. The URL for the file is "file:///mnt/sdcard/book1/page2.html" and the file does exist. But somehow file.exists() isn't showing the same.
File extStore = Environment.getExternalStorageDirectory();
File myFile = new File(extStore.getAbsolutePath() + "/book1/page2.html");
if(myFile.exists()){
...
}
This should work.
Try like this:
File file = new File(Environment.getExternalStorageDirectory() + "/book1/page2.html");
if (file.exists()) {
/*...*/
}
Also make sure you have:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
in your manifest file.
You can check as follows:
File file = new File(getExternalCacheDirectory(), "mytextfile.txt" );
if (file.exists()) {
//Do action
}
File logFile = new File(
android.os.Environment.getExternalStorageDirectory()
+ "/book1/", "page2.tml");
if (logFile.exists())
System.out.println("file exists");
else
System.out.println("file does not exist
Do something like this :
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "your/file/path");
if(yourFile.exists())
{
}
String filepath = getFilesDir().getAbsolutePath();
String FileName = "Yourfilename" ;
File FileMain = new File(filepath, FileName);
if (FileMain.exists()){
do somthing here
}else{}
File file = new File(path+filename);
if (file.exists())
{
//Do something
}
checked, this will work

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

read file from external storage

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

Categories

Resources