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..?
Related
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();
}
Am trying to copy a file from a named subfolder in asset folder but am getting a "not found error" when trying to use the file. Apparently it seems am not copying the file right.
Here is what I have done maybe someone can spot my error
Method call:
copyfile("/lollipop/proxy.sh");
Method:
public void copyfile(String file) {
String of = file;
File f = new File(of);
String basedir = getBaseContext().getFilesDir().getAbsolutePath();
if (!f.exists()) {
try {
InputStream in =getAssets().open(file);
FileOutputStream out =getBaseContext().openFileOutput(of, MODE_PRIVATE);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
Runtime.getRuntime().exec("chmod 700 " + basedir + "/" + of);
} catch (IOException e) {
Log.e(TAG, "Error reading I/0 stream", e);
}
}
}
Trying to use the proxy.sh fails as the file seems it's never copied but when I remove the " lollipop " directory it works fine. What seems wrong? Tnx
openFileOutput() does not accept subdirectories. Since of points to /lollipop/proxy.sh, you are trying to create a subdirectory.
Those having issues accessing sub directories in asset folder since explanation to this isn't explicitly answered this is how I achieved it.
AssetManager assetManager = getAssets();
String[] files = null;
try {
if (Build.VERSION.SDK_INT >= 21)
files = assetManager.list("api-16");
else
files = assetManager.list("");
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
if (files != null) {
for (String file : files) {
InputStream in = null;
OutputStream out = null;
try {
if (Build.VERSION.SDK_INT >= 21)
in = assetManager.open("api-16/" + file);
else
in = assetManager.open(file);
out = new FileOutputStream("/data/data/yourpackagename/" + file);
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);
}
}
method call
Files are now accessible from
/data/data/yourpackagename/
so call the files from there. Using
getFilesDir()
won't work as it gets from
/data/data/yourpackagename/files/
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);
}
Can anyone kind enough show me how to copy files from my app assets folder to /system folder? I know how to get root access and all. For example: I want to copy file from "/assets/lib/libs.so" and check if this file already exist, if it does replace it to new "/system/lib/libs.so".
try this:
try {
File from = new File( "/assets/lib/libs.so" );
File to = new File( "/system/lib/libs.so" );
if( from.exists() && to.exists() ) {
FileInputStream is = new FileInputStream( from );
FileOutputStream os = new FileOutputStream( to );
FileChannel src = is.getChannel();
FileChannel dst = os.getChannel();
dst.transferFrom( src, 0, src.size() );
src.close();
dst.close();
is.close();
os.close();
}
} catch( Exception e ) {
}
This will check if your file exists, delete it and then copy everthing that is in assets.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File exists = new File("/system/lib/libs.so");
if(exists.exists()){
exists.delete();
CopyAssets();
}else{
CopyAssets();
}
}
private void CopyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
out = new FileOutputStream("/system/lib/" + 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);
}
}
Edit:
Declare this permission in your manifest file for filesystems.
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
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());
}
}
}