activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="showgallery.example.com.imagelocation2.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check for update!"
android:onClick="gotoUpdate"/>
</LinearLayout>
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
AsyncTaskRunner taskRunner;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
taskRunner = new AsyncTaskRunner(this);
taskRunner.execute();
}
public void gotoUpdate(View view)
{
taskRunner = new AsyncTaskRunner(this);
taskRunner.execute();
}
}
AsyncTaskRunner.java
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.os.Environment;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/** * Created by pkm1 on 8/26/2016. */public class AsyncTaskRunner extends AsyncTask<Void,String,Void> {
ProgressDialog progressDialog;
private Context ctx;
public AsyncTaskRunner(Context myContext) {
this.ctx = myContext;
}
@Override protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(ctx,"Check Update", "Processing...");
}
@Override protected Void doInBackground(Void... voids) {
publishProgress("Processing..."); // Calls onProgressUpdate()
try {
copyDirorfileFromAssetManager("imagesrc", "GM/imagesrc/");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
// execution of result of Long time consuming operation progressDialog.dismiss();
}
public String copyDirorfileFromAssetManager(String arg_assetDir, String arg_destinationDir) throws IOException {
System.out.print(arg_assetDir); System.out.print(arg_destinationDir);
File sd_path = Environment.getExternalStorageDirectory();
String dest_dir_path = sd_path + addLeadingSlash(arg_destinationDir);
File dest_dir = new File(dest_dir_path);
createDir(dest_dir);
AssetManager asset_manager = ctx.getAssets();
String[] files = asset_manager.list(arg_assetDir);
for (int i = 0; i < files.length; i++) {
String abs_asset_file_path = addTrailingSlash(arg_assetDir) + files[i];
String sub_files[] = asset_manager.list(abs_asset_file_path);
if (sub_files.length == 0) {
// It is a file String dest_file_path = addTrailingSlash(dest_dir_path) + files[i];
copyAssetFile(abs_asset_file_path, dest_file_path);
} else {
// It is a sub directory copyDirorfileFromAssetManager(abs_asset_file_path, addTrailingSlash(arg_destinationDir) + files[i]);
}
}
return dest_dir_path;
}
public void copyAssetFile(String assetFilePath, String destinationFilePath) throws IOException {
InputStream in = ctx.getAssets().open(assetFilePath);
OutputStream out = new FileOutputStream(destinationFilePath);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
out.write(buf, 0, len);
in.close();
out.close();
}
public String addTrailingSlash(String path) {
if (path.charAt(path.length() - 1) != '/') {
path += "/";
}
return path;
}
public String addLeadingSlash(String path) {
if (path.charAt(0) != '/') {
path = "/" + path;
}
return path;
}
public void createDir(File dir) throws IOException {
if (dir.exists()) {
if (!dir.isDirectory()) {
throw new IOException("Can't create directory, a file is in the way");
}
} else {
dir.mkdirs();
if (!dir.isDirectory()) {
throw new IOException("Unable to create directory");
}
}
}
}
No comments:
Post a Comment