我正在尝试使用junRAR提取RAR文件。但是当我从这个问题中读到:OutOfMemoryError when I decompress RAR file on Android时,junRAR尚未针对Android进行优化。所以,我想要做的是检查提取是否成功,然后在失败时给出错误消息。(因为我的应用程序崩溃,如果提取不成功)。有人能告诉我怎么做那个检查?我的主题是在这里完成的:
public class MyTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog progress;
public MyTask(ProgressDialog progress) {
this.progress = progress;
}
public void onPreExecute() {
progress.show();
}
@Override
protected Void doInBackground(Void... params) {
if(taskType==1){
extractArchive(rarFile, destinationFolder);
}
return null;
}
public void onPostExecute(Void unused) {
progress.dismiss();
if(taskType==1){
refreshFileList();
Toast.makeText(MainActivity.this, "Extracted to " + targetPath, Toast.LENGTH_LONG).show();
}
}
这是我的Logcat错误:
03-02 16:59:40.777: E/AndroidRuntime(28573): FATAL EXCEPTION: AsyncTask #1 03-02 16:59:40.777: E/AndroidRuntime(28573): java.lang.RuntimeException: An error occured while executing doInBackground() 03-02 16:59:40.777: E/AndroidRuntime(28573): at android.os.AsyncTask$3.done(AsyncTask.java:278) 03-02 16:59:40.777: E/AndroidRuntime(28573): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 03-02 16:59:40.777: E/AndroidRuntime(28573): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 03-02 16:59:40.777: E/AndroidRuntime(28573): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 03-02 16:59:40.777: E/AndroidRuntime(28573): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 03-02 16:59:40.777: E/AndroidRuntime(28573): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 03-02 16:59:40.777: E/AndroidRuntime(28573): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 03-02 16:59:40.777: E/AndroidRuntime(28573): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 03-02 16:59:40.777: E/AndroidRuntime(28573): at java.lang.Thread.run(Thread.java:856) 03-02 16:59:40.777: E/AndroidRuntime(28573): Caused by: java.lang.OutOfMemoryError 03-02 16:59:40.777: E/AndroidRuntime(28573): at com.github.junrar.unpack.ppm.SubAllocator.startSubAllocator(SubAllocator.java:146) 03-02 16:59:40.777: E/AndroidRuntime(28573): at com.github.junrar.unpack.ppm.ModelPPM.decodeInit(ModelPPM.java:216) 03-02 16:59:40.777: E/AndroidRuntime(28573): at com.github.junrar.unpack.Unpack.readTables(Unpack.java:656) 03-02 16:59:40.777: E/AndroidRuntime(28573): at com.github.junrar.unpack.Unpack.unpack29(Unpack.java:165) 03-02 16:59:40.777: E/AndroidRuntime(28573): at com.github.junrar.unpack.Unpack.doUnpack(Unpack.java:120) 03-02 16:59:40.777: E/AndroidRuntime(28573): at com.github.junrar.Archive.doExtractFile(Archive.java:501) 03-02 16:59:40.777: E/AndroidRuntime(28573): at com.github.junrar.Archive.extractFile(Archive.java:443) 03-02 16:59:40.777: E/AndroidRuntime(28573): at com.jearom.thesisfmanager.MainActivity.extractArchive(MainActivity.java:2270) 03-02 16:59:40.777: E/AndroidRuntime(28573): at com.jearom.thesisfmanager.MainActivity$MyTask.doInBackground(MainActivity.java:1962) 03-02 16:59:40.777: E/AndroidRuntime(28573): at com.jearom.thesisfmanager.MainActivity$MyTask.doInBackground(MainActivity.java:1) 03-02 16:59:40.777: E/AndroidRuntime(28573): at android.os.AsyncTask$2.call(AsyncTask.java:264) 03-02 16:59:40.777: E/AndroidRuntime(28573): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 03-02 16:59:40.777: E/AndroidRuntime(28573): ... 5 more 03-02 16:59:41.437: E/WindowManager(28573): Activity com.jearom.thesisfmanager.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41d3a0c0 that was originally added here 03-02 16:59:41.437: E/WindowManager(28573): android.view.WindowLeaked: Activity com.jearom.thesisfmanager.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41d3a0c0 that was originally added here 03-02 16:59:41.437: E/WindowManager(28573): at android.view.ViewRootImpl.(ViewRootImpl.java:344) 03-02 16:59:41.437: E/WindowManager(28573): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267) 03-02 16:59:41.437: E/WindowManager(28573): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215) 03-02 16:59:41.437: E/WindowManager(28573): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140) 03-02 16:59:41.437: E/WindowManager(28573): at android.view.Window$LocalWindowManager.addView(Window.java:537) 03-02 16:59:41.437: E/WindowManager(28573): at android.app.Dialog.show(Dialog.java:278) 03-02 16:59:41.437: E/WindowManager(28573): at com.jearom.thesisfmanager.MainActivity$MyTask.onPreExecute(MainActivity.java:1867) 03-02 16:59:41.437: E/WindowManager(28573): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561) 03-02 16:59:41.437: E/WindowManager(28573): at android.os.AsyncTask.execute(AsyncTask.java:511) 03-02 16:59:41.437: E/WindowManager(28573): at com.jearom.thesisfmanager.MainActivity$6.onClick(MainActivity.java:752) 03-02 16:59:41.437: E/WindowManager(28573): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166) 03-02 16:59:41.437: E/WindowManager(28573): at android.os.Handler.dispatchMessage(Handler.java:99) 03-02 16:59:41.437: E/WindowManager(28573): at android.os.Looper.loop(Looper.java:137) 03-02 16:59:41.437: E/WindowManager(28573): at android.app.ActivityThread.main(ActivityThread.java:4456) 03-02 16:59:41.437: E/WindowManager(28573): at java.lang.reflect.Method.invokeNative(Native Method) 03-02 16:59:41.437: E/WindowManager(28573): at java.lang.reflect.Method.invoke(Method.java:511) 03-02 16:59:41.437: E/WindowManager(28573): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787) 03-02 16:59:41.437: E/WindowManager(28573): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) 03-02 16:59:41.437: E/WindowManager(28573): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
UI thread
上调用 onPostExecute(Result)。背景计算的result
作为参数传递给此方法。
有关详细信息:http://developer.android.com/reference/android/os/AsyncTask.html
这是一个例子
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
答案 1 :(得分:0)
您可以使用SharedPreferences
创建一个SharedPreference
文件来解决此问题,该文件保存您对文件执行的每个操作的状态,并使用其名称作为KEY和boolean
值描述它的状态是否成功提取。如果进程成功,则使方法返回true
,否则返回false
。
修改构造函数,如下所示,以保存应用程序的上下文
private ProgressDialog progress;
private Context mContext;
public MyTask(ProgressDialog progress, Context context) {
this.progress = progress;
this.mContext = context;
}
将doInBackground
方法替换为此方法:
@Override
protected Void doInBackground(Void... params) {
if(taskType==1){
if(extractArchive(rarFile, destinationFolder)){
SharedPreferences myLog = mContext.getSharedPreferences("state_holder", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = myLog.edit();
editor.putBoolean("file_name", true);
editor.commit();
}
}
return null;
}
有关LINK
中SharedPReference
的更多信息