Writting to root sdcard folder - android

I am trying to copy a file over to the root SD card on an Android device.
When I execute the code, it is going to the sdcard/Android/Data/<packageName>/files folder.
I have tried several different options without any success.
Also, I have checked the app to make sure it has write and read access to external memory.
private void copyfiles() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("ini");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
if (files != null)
for (String filename: files) {
InputStream in = null;
OutputStream out = null;
try { in = assetManager.open("sdcard/" + filename);
//File outFile = new File(Environment.getExternalStorageDirectory(), filename);
File outFile = new File(getExternalFilesDir(null), filename);
if (!(outFile.exists())) { // File does not exist...
out = new FileOutputStream(outFile);
pastefiles( in , out);
}
} catch (IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
} finally {
if ( in != null) {
try { in .close();
} catch (IOException e) {
// NOOP
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// NOOP
}
}
}
}
}

Related

open a file outside of assets folder by assetManager

I have a binary file in root of apk and I can check it by:
AssetManager assets = Context().getResources().getAssets();
assets.list("/")
It exists in returning list. But how can I open it?
Because when I try to open the file by using this code:
assets.open("/test.bin")
I faced by FileNotFoundException.
finally, I soloved it by using getClass().getResourceAsStream() instead of assetManager:
public byte[] loadBinAsset(String name) {
AssetManager assets = context.getResources().getAssets();
InputStream stream = null;
try {
try {
stream = assets.open(name);
} catch (IOException e) {
stream = context.getClass().getResourceAsStream("/" + name);
}
return readFully(stream);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

How to save a text file present in asset folder of android app to the local directory of android phone as text or pdf file?

I have saved some text files in asset folder of android apk , whenever user opens the app which is having list of filenames ,if he clicks on a filename corresponding file opens from asset folder ,
Now I want to let user save that file in text or pdf format to his local directory so that he can transfer it or use it or modify it according to his choice
EDIT : I just want to know the api needed ,not the whole code
Use this function to copy something from assets to storage
private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
if (files != null) for (String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
File outFile = new File(getExternalFilesDir(null), filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, 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);
}
}
Reference : Move file using Java

copy files from assets folder to sd card in android

my code works fine for copying a file from assets folder to sd card. but whenever i try to copy again, it just replaces the old file with the new one instead of renaming it. how do i fix this? thanks
private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
if (files != null) for (String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
File outFile = new File(getExternalFilesDir(null), filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, 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);
}
}
}
Overwriting files in case they exist is a normal behavior, Use fileObject.exists() method to check, If the file exists pick another filename, You can use System.currentTimeMillis() method to gain a unique suffix and add it to the file name.
NaN answer is one of the ways to achieve your goal, just changing one line will do the job:
File outFile = new File(getExternalFilesDir(null), filename + System.currentTimeMillis());
Another way is to increase file size ad eternum, but if you are making a tools suit this won't fit your needs.

Copying file from asset folder to sdcard doesn't seem to work

I am trying to copy an image from the asset folder to the sdcard but doesn't seem to copy it on first launch. It creates the folder okay but doesn't copy the file over.
prefs = getPreferences(Context.MODE_PRIVATE);
if (prefs.getBoolean("firstLaunch", true)) {
prefs.edit().putBoolean("firstLaunch", false).commit();
File nfile=new File(Environment.getExternalStorageDirectory()+"/My Images");
nfile.mkdir();
}
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("middle.jpg");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
File outFile = new File(Environment.getExternalStorageDirectory()+ "/My Images" + filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
}
private void copyFile(InputStream in, OutputStream out) {
// TODO Auto-generated method stub
}
middle.jpg is the file i want to copy over. Can any one tell me what i am doing wrong?
PS i have WRITE_EXTERNAL_STORAGE in my manifest.
Thanks
You forgot to add / in the end of /My images while constructing the path
File outFile = new File(Environment.getExternalStorageDirectory()+ "/My Images/" + filename);
out = new FileOutputStream(outFile);
because the filename would be MyImages+Filename so it wouldn't exists for copying.

How to Copy the Internal Default ringtones to External Memory in Android

Please help me out I am not getting the default ringtone file path.
Can any body tell how to get access to the default ringtone in Android. Here is my code for doing that thing. I have commented the path that I gave directly to asset manager to open the file and read it.
public void copyAssets() {
AssetManager assetManager = this.getAssets();
// String FileName="//media/internal/audio/media/";
File io=getFilesDir();
String[] files = null;
try {
files = assetManager.list("");
// files=assetManager.list(FileName);
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
for (String filename : files) {
InputStream in = null;
OutputStream out = null;
// File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir;
// File fileWithinMyDir = new File(mydir, "myfile"); //Getting a file within the dir.
// FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual
//
try {
in = assetManager.open("Ringtone");
// File myFolder = new File(Environment.getDataDirectory() + "/myFolder");
File myFolder = this.getDir("myFolder", this.MODE_PRIVATE);
File fileWithinMyDir=new File(myFolder,"Ringtoness");
out = new FileOutputStream(fileWithinMyDir);
copyFile(in, out);
} catch (IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
}
}
public 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);
}
}`

Categories

Resources