没有SD卡时,ProgressDialog没有显示在Galaxy S3上

时间:2013-02-27 18:21:21

标签: android progressdialog sd-card android-sdcard

好的,如果有人能回答,我在这里有一个奇怪的。

我有一个启动默认相机的应用程序拍摄照片并将其上传到服务器。上传过程发生在扩展AsyncTask的内部类中。在调用异步任务之前,我有一个显示进度对话框的方法,然后在AsyncTask的onPost Execute方法中将其解除。 这一切都适用于我自己的设备。当我在三星Galaxy S3上测试时会出现问题。

我在progressDialog.Show()上得到一个空指针异常;它不会使应用程序崩溃,它只是不显示对话框。但是当我尝试关闭onPostExecute对话框时,我确实遇到了崩溃。 我认为这是因为S3的相机似乎只是在风景中打开(尽管我的整个应用程序被锁定为纵向,Google可以告诉我可能的各种方式)并且方向更改导致活动刷新导致引用progressDialog将丢失。

无法更改对话框实例化的位置(onPreExecute()确实有效,但我仍然遇到相同的问题),检查它是否为null或保存实例状态已修复此问题。我在另一篇文章中读到,有些人在保存到SD卡时遇到了问题。 S3没有SD卡,但应用程序试图写入外部存储。我试着检查是否有SD卡使用。

Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

即使没有SD卡,它实际上也是如此。将图像写入外部存储器并不重要,因此我尝试使用此方法获取缓存副本。

File image = File.createTempFile(
            imageFileName, 
            JPEG_FILE_SUFFIX, 
            this.getCacheDir()
            );

这也没有区别。你问为什么我关心这个SD卡问题它不可能影响进度对话。好吧,如果我把SD卡放入S3,问题就会消失,进度对话框也会完美呈现。我不知道为什么会这样,但我不能保证使用该应用程序的人在他们的设备中会有SD卡,所以我需要找到一个合适的解决方案。

对于这篇文章感到抱歉,但我认为需要一些解释才能获得清晰的理解。任何想法或解决方案将不胜感激。

以下代码是我正在使用的精简版本,我试图仅包含相关资料。

 @Override  public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.compare_layout_activity);

          findViewById(R.id.bCompare).setOnClickListener(new OnClickListener() {            
          public void onClick(View v) {
        try{
           capturedImage = createImageFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if(isNetworkAvailable()){
            Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(capturedImage));
            startActivityForResult(i, REQ_CODE_PHOTO_TAKE);
        }
        else{
            displayAlert("Network Error", "You do not have access to the internet. Please turn on your WiFi.");
            }           
         }      
     });

}


protected File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = 
                new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
        File image = File.createTempFile(
                imageFileName, 
                JPEG_FILE_SUFFIX, 
                this.getCacheDir()
                );

        capturedImagePath = image.getAbsolutePath();

        return image;
    }

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {
        showProgressDialog();
        new LongOperation(getApplicationContext()).execute("");

    } else if (resultCode == Activity.RESULT_CANCELED) {

    }               
}


private void showProgressDialog(){
    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Checking progress");
    progressDialog.setCanceledOnTouchOutside(false);
    progressDialog.show();

  }

//I wont include the full async task because the code executes fine except for the crash on the show progress Dialog so i'll include the onPostExecute
    @Override
    protected void onPostExecute(String result) { 
    if (progressDialog != null) {
        progressDialog.dismiss(); 
    }

几乎忘了包含堆栈跟踪,尽管它不是最有用的信息。这发生在progressDialog.show();

02-27 18:11:33.945: E/WindowManager(21994): Activity com.activity.CompareActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41ed6318 that was originally added here
02-27 18:11:33.945: E/WindowManager(21994): android.view.WindowLeaked: Activity com.activity.CompareActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41ed6318 that was originally added here
02-27 18:11:33.945: E/WindowManager(21994):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:402)
02-27 18:11:33.945: E/WindowManager(21994):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:311)
02-27 18:11:33.945: E/WindowManager(21994):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
02-27 18:11:33.945: E/WindowManager(21994):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
02-27 18:11:33.945: E/WindowManager(21994):     at android.view.Window$LocalWindowManager.addView(Window.java:558)
02-27 18:11:33.945: E/WindowManager(21994):     at android.app.Dialog.show(Dialog.java:277)
02-27 18:11:33.945: E/WindowManager(21994):     at com.activity.CompareActivity.showProgressDialog(CompareActivity.java:324)
02-27 18:11:33.945: E/WindowManager(21994):     at com.activity.CompareActivity.onActivityResult(CompareActivity.java:249)
02-27 18:11:33.945: E/WindowManager(21994):     at android.app.Activity.dispatchActivityResult(Activity.java:5368)
02-27 18:11:33.945: E/WindowManager(21994):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3178)
02-27 18:11:33.945: E/WindowManager(21994):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2603)
02-27 18:11:33.945: E/WindowManager(21994):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644)
02-27 18:11:33.945: E/WindowManager(21994):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2130)
02-27 18:11:33.945: E/WindowManager(21994):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3553)
02-27 18:11:33.945: E/WindowManager(21994):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
02-27 18:11:33.945: E/WindowManager(21994):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1233)
02-27 18:11:33.945: E/WindowManager(21994):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 18:11:33.945: E/WindowManager(21994):     at android.os.Looper.loop(Looper.java:137)
02-27 18:11:33.945: E/WindowManager(21994):     at android.app.ActivityThread.main(ActivityThread.java:4898)
02-27 18:11:33.945: E/WindowManager(21994):     at java.lang.reflect.Method.invokeNative(Native Method)
02-27 18:11:33.945: E/WindowManager(21994):     at java.lang.reflect.Method.invoke(Method.java:511)
02-27 18:11:33.945: E/WindowManager(21994):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-27 18:11:33.945: E/WindowManager(21994):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-27 18:11:33.945: E/WindowManager(21994):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

如何使用不同的模式,例如:

  1. 活动A启动相机活动

  2. CameraActivity将控件返回到活动A

  3. 活动A立即启动活动B

  4. 活动B没有布局/相同的布局

  5. 活动B处理AsyncTask和ProgressDialog。

  6. 如果问题与Camera Activity之前和之后的活动有某种关联,这可能会通过创建一个全新的Activity(clean slate)来处理上传,从而解决了这个问题。你觉得怎么样?