I try to make an app that show 100 pictures.
I want to zip folder of photos and then put it on my project.
now i need to understand How can copy a zip folder to internal storage,And then unzip it in android?
You can include your .zip file on Assets folder of apk, and them on code, copy the .zip to internal storage and unzipping using a ZipInputStream.
First copy de .zip file to internal storage, and after unzip a file:
protected void copyFromAssetsToInternalStorage(String filename){
AssetManager assetManager = getAssets();
try {
InputStream input = assetManager.open(filename);
OutputStream output = openFileOutput(filename, Context.MODE_PRIVATE);
copyFile(input, output);
} catch (IOException e) {
e.printStackTrace();
}
}
private void unZipFile(String filename){
try {
ZipInputStream zipInputStream = new ZipInputStream(openFileInput(filename));
ZipEntry zipEntry;
while((zipEntry = zipInputStream.getNextEntry()) != null){
FileOutputStream zipOutputStream = openFileOutput(zipEntry.getName(), MODE_PRIVATE);
int length;
byte[] buffer = new byte[1024];
while((length = zipInputStream.read(buffer)) > 0){
zipOutputStream.write(buffer, 0, length);
}
zipOutputStream.close();
zipInputStream.closeEntry();
}
zipInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
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/
I want to copy zip file that includes images from asset to internal storage,And then unzip it.
This is my code :
protected void copyFromAssetsToInternalStorage(String filename){
AssetManager assetManager = getAssets();
try {
InputStream input = assetManager.open(filename);
OutputStream output = openFileOutput(filename, Context.MODE_PRIVATE);
copyFile(input, output);
} catch (IOException e) {
e.printStackTrace();
}
}
private void unZipFile(String filename){
try {
ZipInputStream zipInputStream = new ZipInputStream(openFileInput(filename));
ZipEntry zipEntry;
while((zipEntry = zipInputStream.getNextEntry()) != null){
FileOutputStream zipOutputStream = openFileOutput(zipEntry.getName(), MODE_PRIVATE);
int length;
byte[] buffer = new byte[1024];
while((length = zipInputStream.read(buffer)) > 0){
zipOutputStream.write(buffer, 0, length);
}
zipOutputStream.close();
zipInputStream.closeEntry();
}
zipInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
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 i have this error :
java.lang.IllegalArgumentException: File filename/ contains a path separator
What should i do?
From openFileOutput documentation:
name The name of the file to open; can not contain path separators.
Hope this helps
Yaron
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
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);
}
}