几张照片后相机意图使我的应用程序崩溃

时间:2014-05-22 16:14:22

标签: java android android-intent camera crash

我从android.provider.MediaStore.ACTION_IMAGE_CAPTURE回来时遇到了一些手机的问题。在HTC和Alcatel上它运行良好,但在LG上它崩溃了我的应用程序。我可以保存一些图片,之后我的应用程序崩溃了抱歉! MyApplication已停止运行。我已经尝试了很多方法来避免这种情况,但我仍然会遇到同样的错误。

首先我使用了这个

1:尝试

用图像路径拍摄照片(没有任何异常):

public static void saveImage(String saveImgDir, String image, int TAKE_PHOTO_CODE, Activity activity) {
        try {
            GlobalVariables.incorrectButtonPressed = false;
             // CREATE DIR IF NOT EXISTS
             File directory = new File(saveImgDir);
             directory.mkdirs();
             // CREATE AND SAVE IMG
             File imageFile = new File(saveImgDir + File.separator + image);
             try {
             imageFile.createNewFile();
             } catch (IOException e) {
             Log.d("IMG", "FILE CREATE: " + e.toString(), e.getCause());
             }
             Uri outputFileUri = Uri.fromFile(imageFile);
             // CALL CAMERA ACTIVITY
             Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
             cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
             activity.startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
        } catch (Exception ex) {
            Log.d("CAMPROBLEM", "bblockScenario", ex);
        }

    }

关于活动结果(没有任何例外):

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    try {
    if (requestCode == B_TAKE_PHOTO_CODE_AFTER && resultCode == RESULT_OK) {
        Log.d("CAMPROBLEM", "ACTIVITY RESULT B_TAKE_PHOTO_CODE_AFTER RESULT OK");
        imageTakenAfter++;
        Toast.makeText(context, "PCITURE SAVED", Toast.LENGTH_LONG).
        return;
        }
        if (requestCode == B_TAKE_PHOTO_CODE_AFTER && resultCode != RESULT_OK) {
            Log.d("CAMPROBLEM", "ACTIVITY RESULT B_TAKE_PHOTO_CODE_AFTER RESULT NOT OK");
            Toast.makeText(context, "PCITURE NOT SAVED", Toast.LENGTH_LONG).show();
            return;
        }
    } catch (Exception ex) {
        Log.d("CAMPROBLEM", "bblockScenario", ex);
    }

}

2:尝试

然后我尝试使用异步任务(只是为了确保不是内存泄漏)。我从AsyncTask类调用了saveImage函数,但仍然遇到了同样的问题。 (没有得到任何例外)

3:尝试

在没有图像保存路径的情况下调用intent:

public static void saveImage(int TAKE_PHOTO_CODE, Activity activity) {
        try {
            GlobalVariables.incorrectButtonPressed = false;
            // CREATE DIR IF NOT EXISTS
            Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            activity.startActivityForResult(it, TAKE_PHOTO_CODE);
        } catch (Exception ex) {
            Log.d("CAMPROBLEM", "bblockScenario", ex);
        }

    }

将我的照片保存在我的onActivityResult

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            String image = ServerTime.getServerDate() + ".jpg";
            File file = new File(saveImgDirectory + File.separator + image);
            try {
                if (data == null) {
                    Toast.makeText(context, "IMAGE NOT SAVED", Toast.LENGTH_LONG).show();
                    bblockScenario();
                    Log.d("CAMPROBLEM", "NULL DATA");
                    return;
                }
                Bitmap photo = (Bitmap) data.getExtras().get("data");
                FileOutputStream out = null;

                out = new FileOutputStream(file);
                photo.compress(Bitmap.CompressFormat.PNG, 90, out);
                if (out != null) {
                    out.close();
                }
            } catch (Exception e) {
                Toast.makeText(context, "IMAGE NOT SAVED", Toast.LENGTH_LONG).show();
                bblockScenario();
                Log.d("CAMPROBLEM", "E", e);
                return;
            } finally {
                try {

                } catch (Throwable ignore) {
                }
            }
            Toast.makeText(context, "IMAGE SAVED", Toast.LENGTH_LONG).show();
            return;
        } catch (Exception ex) {
            Log.d("CAMPROBLEM", "bblockScenario", ex);
        }

    }

在onActivityResult中,我在每3张图片上获得空数据。无法理解为什么。

我的清单中有所有的前提:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false"/>

任何有关此问题的帮助和知识都会让我的生活更轻松。希望有人可以帮助我。

提前致谢

编辑:

logcat的:

05-23 10:26:17.412: E/AndroidRuntime(14022): FATAL EXCEPTION: main
05-23 10:26:17.412: E/AndroidRuntime(14022): java.lang.IllegalArgumentException: View not attached to window manager
05-23 10:26:17.412: E/AndroidRuntime(14022):    at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:671)
05-23 10:26:17.412: E/AndroidRuntime(14022):    at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:364)
05-23 10:26:17.412: E/AndroidRuntime(14022):    at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:163)
05-23 10:26:17.412: E/AndroidRuntime(14022):    at android.app.Dialog.dismissDialog(Dialog.java:347)
05-23 10:26:17.412: E/AndroidRuntime(14022):    at android.app.Dialog.dismiss(Dialog.java:330)
05-23 10:26:17.412: E/AndroidRuntime(14022):    at es.zonsko.bitola.blocator.activity.BlocatorTabHostActivity$SyncTask.onPostExecute(BlocatorTabHostActivity.java:381)
05-23 10:26:17.412: E/AndroidRuntime(14022):    at es.zonsko.bitola.blocator.activity.BlocatorTabHostActivity$SyncTask.onPostExecute(BlocatorTabHostActivity.java:1)
05-23 10:26:17.412: E/AndroidRuntime(14022):    at android.os.AsyncTask.finish(AsyncTask.java:631)
05-23 10:26:17.412: E/AndroidRuntime(14022):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-23 10:26:17.412: E/AndroidRuntime(14022):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
05-23 10:26:17.412: E/AndroidRuntime(14022):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 10:26:17.412: E/AndroidRuntime(14022):    at android.os.Looper.loop(Looper.java:153)
05-23 10:26:17.412: E/AndroidRuntime(14022):    at android.app.ActivityThread.main(ActivityThread.java:5070)
05-23 10:26:17.412: E/AndroidRuntime(14022):    at java.lang.reflect.Method.invokeNative(Native Method)
05-23 10:26:17.412: E/AndroidRuntime(14022):    at java.lang.reflect.Method.invoke(Method.java:511)
05-23 10:26:17.412: E/AndroidRuntime(14022):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-23 10:26:17.412: E/AndroidRuntime(14022):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-23 10:26:17.412: E/AndroidRuntime(14022):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您可以在onActivityResult中处理此异常,请在onActivityResult()的开头使用下面的代码

if(resultcode != RESULT_OK)
return;