I am trying to copy file in android.I have the filepath of the file.I want to copy it to another folder with different file name.I am using below code but it doesn't work.My file is video file.
I get error-
/storage/emulated/0/testcopy.mp4: open failed: EISDIR (Is a directory)
Below is my code
File source=new File(filepath);
File destination=new File(Environment.getExternalStorageDirectory()+ "/testcopy.mp4");
copyFile(source.getAbsolutePath(),destination.getAbsolutePath());
private void copyFile(String inputPath, String outputPath) {
InputStream in = null;
OutputStream out = null;
try {
//create output directory if it doesn't exist
File dir = new File (outputPath);
if (!dir.exists())
{
dir.mkdirs();
}
in = new FileInputStream(inputPath );
out = new FileOutputStream(outputPath);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
// write the output file (You have now copied the file)
out.flush();
out.close();
out = null;
} catch (FileNotFoundException fnfe1) {
Log.e("tag", fnfe1.getMessage());
}
catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
You create a directory with name "/storage/emulated/0/testcopy.mp4" here
//create output directory if it doesn't exist
File dir = new File (outputPath);
if (!dir.exists())
{
dir.mkdirs();
}
try this code
//create output directory if it doesn't exist
File dir = (new File (outputPath)).getParentFile();
if (!dir.exists())
{
dir.mkdirs();
}
Them main problem is that you are trying to write the file that is a directory. To avoid this exception, create the directory first, then write the file:
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
// creating a new folder if doesn't exist
boolean success = folder.exists() || folder.mkdirs();
File file = new File(folder, "filename.mp4");
try {
if (!file.exists() && success) file.createNewFile();
...
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
...
}catch (IOException e){
e.printStackTrace();
}
Related
While moving files from external storage permission denied error on FileOutputStream from external storage path but it works on internal storage path.
I have tried this and my permisson is granted for Read & Write.
File yourFile = new File(outputPath+"/"+file.getName());
if(!yourFile.exists()) { yourFile.createNewFile(); }
FileOutputStream oFile = new FileOutputStream(yourFile, false);
Here is my code: Path is just a string for internal & external too.
String OutputInternal = "/storage/emulated/0/test1/testing"
String OutputExternal = "/storage/1A7D-0850/test1/testing"
private void moveFile(File file, String outputPath) {
if (file != null && !outputPath.equalsIgnoreCase("")) {
InputStream in = null;
OutputStream out = null;
try {
//create output directory if it doesn't exist
File dir = new File(outputPath);
if (!dir.exists()) {
dir.mkdirs();
}
in = new FileInputStream(file);
out = new FileOutputStream(outputPath+"/"+file.getName());
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
// write the output file
out.flush();
out.close();
out = null;
file.delete();
} catch (FileNotFoundException fnfe1) {
} catch (Exception e) {
}
}
}
It works when path is from internal storage but when i select external path it shows error on fileOutputStream (Permission Denied)
how to prevent storing same file selected in galary twice in internal storage in android .I tried with below code it copies same video many times in a folder in the internal storage .
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
new SaveVideoInFolder().execute(uri);
try {
InputStream is = getContentResolver().openInputStream(uri);
File storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
File app_directory = new File(storage, "video_choosing");
if (!app_directory.exists())
app_directory.mkdirs();
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String filename = String.format("VID_%s.mp4", timestamp);
file = new File(app_directory, filename);
Toast.makeText(MainActivity.this,file.toString(),Toast.LENGTH_SHORT).show();
OutputStream output = new FileOutputStream(file);
byte[] buffer = new byte[4096];
int read;
while ((read = is.read(buffer)) != -1)
output.write(buffer, 0, read);
output.flush();
output.close();
} catch (FileNotFoundException e) {
Log.e("TAG", "File Not Found", e);
} catch (IOException e) {
Log.e("TAG", "IOException", e);
}
}
// Create the storage directory if it does not exist
if (!file.exists()) {
if (!file.mkdirs()) {
/* Log.e(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");*/
return null;
}
only you have check that your file is exist bt if condition and make directory if it is not..
File file = new File(app_directory, filename);
if(file.exists()){
...
}
else {
...
}
I am trying to copy two files from assets folder to external storage, one is a text based and another one is a rar file(60 kb). But when i open the rar file in file manager, it says "wrong header". The size of the rar file which i get is 16kb.
private void copyAsset() {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(SKETCH_FILE);
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/Ardumation");
if (!dir.exists()) {
//System.out.println("creating directory: " + directoryName);
dir.mkdir();
}
//File outFile = new File(getExternalFilesDir(null), SKETCH_FILE);
File sketchFile = new File(dir, SKETCH_FILE);
File libraryFile = new File(dir, LIBRARY_FILE);
if (!sketchFile.exists()){
out = new FileOutputStream(sketchFile);
copyFile(in, out);
}
if (!libraryFile.exists()){
out = new FileOutputStream(libraryFile);
copyFile(in, out);
}
} catch(IOException e) {
Log.e(TAG, "Failed to copy asset file: ", e);
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// NOOP
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// NOOP
}
}
}
}
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);
}
}
Now what is wrong..?
I am try to copy from asset to sdcard
Here is part of my code
private void CopyAssets(String folder) {
File folders = new File(Environment.getExternalStorageDirectory().toString()+"/beatscache/"+folder);
folders.mkdirs();
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list(folder);
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(folder+"/"+filename); // if files resides inside the "Files" directory itself
out = new FileOutputStream(Environment.getExternalStorageDirectory().toString()+"/"+"beatscache"+"/"+folder +"/" + filename);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(Exception e) {
Log.e("tag", e.getMessage());
}
}
}
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);
}
}
And
CopyAssets("bin");
Log shows
E/tag(3801): /mnt/sdcard/beatscache/bin/alsa_ctl: open failed: ENOENT
(No such file or directory)
I have given READ and WRITE permissions in AndroidManifest.xml
and checked Environment.MEDIA_MOUNTED.equals(state) returns true.
And folder is not getting created.
Can you tell where I am doing wrong.?
Check if file is exists or not first then move your content there
final File dir = new File(cEnvironment.getExternalStorageDirectory()+"/beatscache/"+folder +"/");
if(dir.exists()==false)
{
dir.mkdirs(); //create folders where write files
final File file = new File(dir, filename);
}
A way to save a non text file to /data/files folder.
If file resource is from assets folder.
this should do it.
private void writeToSDCard() {
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
InputStream from = myContext.getResources().openRawResource(rID);
File dir = new java.io.File (root, "pdf");
dir.mkdir();
File writeTo = new File(root, "pdf/" + attachmentName);
FileOutputStream to = new FileOutputStream(writeTo);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = from.read(buffer)) != -1)
to.write(buffer, 0, bytesRead); // write
to.close();
from.close();
} else {
Log.d(TAG, "Unable to access SD card.");
}
} catch (Exception e) {
Log.d(TAG, "writeToSDCard: " + e.getMessage());
}
}
}