Successfully i wrote program to read single file in asset folder and assign it to text view.
Now i want to read all files and assign it to the text view, will any one of you help me how to do? all the files are text files, thankful to you in advance.
****In Android we can't read file from assets folder u have to copy file from asseset to sdcard than perform reading**
EDIT: this statement is wrong. See comments.
use following code for perform copy from assets folder
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);
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
out = new FileOutputStream("/sdcard/" + filename);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
}
}
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);
}
}
Related
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..?
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/
consider i have 10 images in assets folder and i retrieve it to Image View.And i have added another 10 more images to assets folder.Will i be able to view all 20 images on image view?If not why?If so how?
This is the code that i used to read and copy assets file to SD card
String[] getImagesFromAssets() {
AssetManager assetManager = getAssets();
String[] img_files = null;
try {
// img_files = getAssets().list("pictures");
img_files = assetManager.list("pictures");
} catch (IOException ex) {
Logger.getLogger(GameActivity.class
.getName()).log(Level.SEVERE, null, ex);
}
return img_files;
}
void loadImage(String name) {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("pictures/" + name);
File outFile = new File(getExternalFilesDir(null)+"/"+"pictures/"+ name);
out = new FileOutputStream(outFile);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
Bitmap myBitmap = BitmapFactory.decodeFile(outFile.getAbsolutePath());
image.setImageBitmap(myBitmap);
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + "pictures/" + name, e);
return;
}
}
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);
}
}
I recommend use 3rd party libs, Use Acceptable URIs examples in android Android-Universal-Image-Loader
Ex: String imageUri = "assets://image.png";
Credits: Sergey Tarasevich
I have made an application, in which I am using more images & videos , I have putted all of resources inside the sub folders of assets & also my database is in a sub folder of assets folder, I want to move whole sub folders of assets folder including there files in sdcard.
My assets folder size is more than 30mb.
The only way you can access assest is throw assetManager to get input steream from them.
So your code will have to look something like this.
for (int i = 1; i < files.length ; i++) {
try{
InputStream is = aManager.open(files[i]);
OutputStream os = new FileOutputStream(output[i]);
read = 0;
buffer = new byte[1024];
while (read != -1) {
read = is.read(buffer, 0, buffer.length);
if (read == -1)
break;
os.write(buffer, 0, read);
}
} finally {
is.close();
os.close();
}}
where files is an array of Strings to the pathes in your asset folder , and output is an array of strings of pathes to the output directory "on SD card "
Try out below code to copy assets folders & files into sdcard.
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);
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
out = new FileOutputStream("/sdcard/" + filename);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
}
}
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
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);
}