Android - 拍照并在popUp窗口中显示

时间:2015-07-25 12:05:15

标签: java android

我想拍一张照片并在弹出窗口中显示。问题是,在我拍摄照片后,我得到一个NullPointerException,因为显然imageview在

中为空
  

ImageView imageView =(ImageView)findViewById(R.id.imageView_photo);

我不明白为什么。

这是我的主要活动:

   private void takepic() {
        // Check Camera
        if (getApplicationContext().getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CAMERA)) {
            // Open default camera
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

            // start the image capture Intent
            startActivityForResult(intent, 100);

        } else {
            Toast.makeText(getApplication(), "Camera not supported", Toast.LENGTH_LONG).show();
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 100 && resultCode == RESULT_OK) {

            selectedImage = data.getData();
            photo = (Bitmap) data.getExtras().get("data");

            // Cursor to get image uri to display

            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            picturePath = cursor.getString(columnIndex);
            cursor.close();

            Bitmap photo = (Bitmap) data.getExtras().get("data");


            // Show pop up window
            LayoutInflater layoutInflater = LayoutInflater.from(poi_photos.this);
            View promptView = layoutInflater.inflate(R.layout.input_photo, null);
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(poi_photos.this);
            alertDialogBuilder.setView(promptView);
            ImageView imageView = (ImageView) findViewById(R.id.imageView_photo);
            imageView.setImageBitmap(photo);

            alertDialogBuilder.setCancelable(false)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    })

                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            // create an alert dialog
            AlertDialog alert = alertDialogBuilder.create();
            alert.show();
        }
    }

和input_photo.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Your photo: "
        android:id="@+id/textView1" />

    <ImageView
        android:layout_width="300dp"
        android:layout_height="400dp"
        android:id="@+id/imageView_photo"
        android:layout_gravity="center_horizontal" />


</LinearLayout>

和我的logcat

FATAL EXCEPTION: main
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/25717 typ=image/jpeg (has extras) }} to activity {com.example.diana.track/com.example.diana.track.poi_photos}: java.lang.NullPointerException
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3448)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3491)
            at android.app.ActivityThread.access$1100(ActivityThread.java:149)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5214)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.diana.track.poi_photos.onActivityResult(poi_photos.java:264)
            at android.app.Activity.dispatchActivityResult(Activity.java:5322)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3444)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3491)
            at android.app.ActivityThread.access$1100(ActivityThread.java:149)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5214)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
            at dalvik.system.NativeStart.main(Native Method)

知道可能出现什么问题吗?

1 个答案:

答案 0 :(得分:2)

此:

if(!$scope.$$phase) {
   $scope.$apply();
}

应该是这样的:

ImageView imageView = (ImageView) findViewById(R.id.imageView_photo);