I want to move file from internal storage to sd card. I've tried with Environment.getExternalStorageDirectory() but it's moving internal storage only.
I've used below code:
ContextCompat.getExternalFilesDirs(mActivity, null)[0]
but it's moving in package folder
/storage/emulated/0/Android/data/com.unzipdemo/files/MyFileStorage/SampleFile.txt
I want to move files in a specific folder name. Can you please help me to solve it.
Try this method copyDirectoryOneLocationToAnotherLocation()
Pass the internal file value as source location and External file path
as target location
public static void copyDirectoryOneLocationToAnotherLocation(File sourceLocation, File
targetLocation)
throws IOException {
if (sourceLocation.isDirectory()) {
if (!targetLocation.exists()) {
targetLocation.mkdir();
}
String[] children = sourceLocation.list();
for (int i = 0; i < sourceLocation.listFiles().length; i++) {
copyDirectoryOneLocationToAnotherLocation(new File(sourceLocation, children[i]),
new File(targetLocation, children[i]));
}
} else {
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
NOTE :- After copying delete the source file
Related
I use this code but it fails at the file outputstream.
When i make a static void then getResources will fail.
public void copy (Context context) {
InputStream in = getResources().openRawResource(R.raw.high1);
FileOutputStream out = new FileOutputStream("/sdcard/pic1.jpg");
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}
}
To getResources() do not fail when changing it to static, modify the line:
InputStream in = getResources().openRawResource(R.raw.high1);
To:
InputStream in = context.getResources().openRawResource(R.raw.high1);
Additionally, quoting CommonsWare:
NEVER HARDCODE PATHS. Use getExternalFilesDir(), or Environment.getExternalStoragePublicDirectory(), or something like that to get a directory on external storage to use.
hi i'm trying to open a doc file in quick office from my app but it doesn't seem to be allowed access to get the file from my internal storage. so what i'm wanting to do is move the file from internal storage to a temporary folder on the external storage so that when it can be opened by quick office? does anyone know if this is possible
String source = // Internal file path
String destination = getExternalCacheDir() + "/" + UUID.randomUUID();
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(destination);
byte[] buf = new byte[1024];
int len;
int total = 0;
while ((len = fis.read(buf)) > 0) {
total += len;
fos.write(buf, 0, len);
if (total > 20 * 1024) {
fos.flush();
}
}
fis.close();
fos.close();
I've a functionality in my application in which I save a doc/img file path in my database. This file is lying in a folder (E.g. "/mnt/sdcard/MyApp/MyItem/test.png"). Now what i want to do is to copy this file to other folder (E.g. /mnt/sdcard/MyApp/MyItem/Today/test.png).
Right now I am using the code below but it's not working :
private void copyDirectory(File from, File to) throws IOException {
try {
int bytesum = 0;
int byteread = 0;
InputStream inStream = new FileInputStream(from);
FileOutputStream fs = new FileOutputStream(to);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
fs.write(buffer, 0, byteread);
}
inStream.close();
fs.close();
} catch (Exception e) {
}
}
and on button click am using the following code :
File sourceFile = new File(fileList.get(0).getAbsolutePath); //comes from dbs
File targetFile = new File(Environment.getExternalStorageDirectory(),"MyApp/MyItem/Today/");
copyDirectory(sourceFile,targetFile, currDateStr);
Any idea why it's not working?
This code is working fine for me.
public void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
And one more thing have you added in Manifest file *permission to write to external storage.*
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Yup got it working, I was not giving file name while copying the files, and didnt really look at error log, got it working now thanks. And yea the above code works just fine.
I am trying to move file from /mnt/sdcard to /mnt/extsd
Currently file is stored in /mnt/sdcard/DCIM/camera after shooting a video
but now i want to move this file to /mnt/extsd
I am using following code
File fromFile=new File( "/mnt/sdcard/folderpath" ,"/video.mp4");
File toFile=new File("/mnt/extsd" ,fromFile.getName());
fromFile.renameTo(toFile);
I read that renameTo dosen't work for moving in different file systems
Please help me
try {
java.io.FileInputStream fosfrom = new java.io.FileInputStream(fromFile);
java.io.FileOutputStream fosto = new FileOutputStream(toFile);
byte bt[] = new byte[1024];
int c;
while ((c = fosfrom.read(bt)) > 0) {
fosto.write(bt, 0, c); //将内容写到新文件当中
}
fosfrom.close();
fosto.close();
} catch (Exception ex) {
Log.e("readfile", ex.getMessage());
}
}
give source file where exist ur file and target location where u want to store .
public static void copyDirectoryOneLocationToAnotherLocation(File sourceLocation, File targetLocation)
throws IOException {
if (sourceLocation.isDirectory()) {
if (!targetLocation.exists()) {
targetLocation.mkdir();
}
String[] children = sourceLocation.list();
for (int i = 0; i < sourceLocation.listFiles().length; i++) {
copyDirectoryOneLocationToAnotherLocation(new File(sourceLocation, children[i]),
new File(targetLocation, children[i]));
}
} else {
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
according to Android docs "Both paths must be on the same mount point" like it could be used for file renaming only in case of different paths. So if you want to move it you probably should copy it and then rename and then delete the source file. But in this case you are trying not only to move the file from one FS to another but also you are trying to use /mnt/extsd which might not be able at all. Follow this question about such paths.
I need to find a way how to create files from specific folder in Internal Storage of my device to a specific folder in External Storage.
Example :
I have 50 image files in data/data/app_package/files/documents/server/userId/storage/ in Internal Storage.
I want to copy all of the files in that directory to /sdcard/Documents/Server/UserId/Storage/
And the idea is that in some cases maybe I'll have to move files like 50MB and maybe more. Any suggestions how can I achieve this?
try this code
private void copyToFolder(String path) throws IOException {
File selectedImage = new File(path);
if (selectedImage.exists()) {
String wall = selectedImage.getName();
in = getContentResolver().openInputStream(selectedImageUri);
out = new FileOutputStream("/sdcard/wallpapers/" + wall);
copyFile( in , out); in .close(); in = null;
out.flush();
out.close();
out = null;
} else {
System.out.println("Does not exist");
}
}
private void copyFile(InputStream in , OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in .read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}