从Camera Intent

时间:2017-04-25 01:36:15

标签: android android-intent android-camera-intent

我有2个imageView,我想从Camera Intent设置图像。当我希望有人能帮助启发我时,我遇到了以下问题。

  1. 当我把第一张照片设置为imageView1时,它设置为imageView2。第一张照片应该设置为imageView1。

  2. 当我拍摄第二张照片并将相机中的图像保存到imageView1时,我的应用程序崩溃了。第二张照片应该设置为imageView2。

  3. 我使用的代码如下。

        public void Camera (View view) {
            Intent intent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
            file1 = Uri.fromFile(getOutputMediaFile());
            intent.putExtra(MediaStore.EXTRA_OUTPUT, file1);
    
            startActivityForResult(intent, 100);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent 
        data) {
            if (requestCode == 100) {
                if (resultCode == RESULT_OK && imageView1 == null) {
                    imageView1.setImageURI(file1);
                } else {
                    imageView2.setImageURI(file1);
                }
            }
        }
    

    logcat的:

    E / AndroidRuntime:致命异常:主要                                                                              java.lang.OutOfMemoryError                                                                                  在android.graphics.BitmapFactory.nativeDecodeStream(本机方法)                                                                                  在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)                                                                                  在android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)                                                                                  在android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)                                                                                  在android.graphics.drawable.Drawable.createFromStream(Drawable.java:783)                                                                                  在android.widget.ImageView.resolveUri(ImageView.java:673)                                                                                  在android.widget.ImageView.setImageURI(ImageView.java:407)                                                                                  在MainActivity.onActivityResult(MainActivity.java:117)                                                                                  在android.app.Activity.dispatchActivityResult(Activity.java:5563)                                                                                  在android.app.ActivityThread.deliverResults(ActivityThread.java:3514)                                                                                  在android.app.ActivityThread.handleSendResult(ActivityThread.java:3561)                                                                                  在android.app.ActivityThread.access $ 1200(ActivityThread.java:168)                                                                                  在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1377)                                                                                  在android.os.Handler.dispatchMessage(Handler.java:99)                                                                                  在android.os.Looper.loop(Looper.java:137)                                                                                  在android.app.ActivityThread.main(ActivityThread.java:5493)                                                                                  at java.lang.reflect.Method.invokeNative(Native Method)                                                                                  在java.lang.reflect.Method.invoke(Method.java:525)                                                                                  在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1209)                                                                                  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)                                                                                  在dalvik.system.NativeStart.main(本地方法)

    谢谢!

1 个答案:

答案 0 :(得分:0)

我设法得到了我想要将2张图像设置为各自的imageViews的结果。我修改后的代码如下。

public void Camera (View view) {
    Intent intent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);

    if (file1 == null) {
        file1 = Uri.fromFile(getOutputMediaFile());
        intent.putExtra(MediaStore.EXTRA_OUTPUT, file1);
        startActivityForResult(intent, 1);

    } else {

        file2 = Uri.fromFile(getOutputMediaFile());
        intent.putExtra(MediaStore.EXTRA_OUTPUT, file2);
        startActivityForResult(intent, 2);
    }

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            imageView1.setImageURI(file1);
        }
    } else {
        imageView2.setImageURI(file2);
    }
}

然而,如果我拍摄第3张照片,它将设置为imageView2 ....需要纠正此问题。

如果我在没有关闭此应用程序的情况下启动另一个应用程序,当我返回时,图像不再显示在imageViews上。