从库中选择图像后,App力会关闭

时间:2017-09-07 02:38:49

标签: android

我不知道错误在哪里,我的代码是从一个工作正常的项目中复制粘贴的。可以拍摄照片并将其保存到图库中,但是当我想将其加载到图像视图中时,应用程序会崩溃。一直在寻找答案,但所有这些都不适合我。这是应用程序崩溃时的日志,

09-07 10:17:06.371 745-745/henderson.application E/BitmapFactory: Unable to 
decode stream: java.io.FileNotFoundException: 
/storage/emulated/0/DCIM/Camera/20170907_101702.jpg: open failed: EACCES 
(Permission denied)
09-07 10:17:06.381 745-828/henderson.application D/mali_winsys: 
new_window_surface returns 0x3000,  [1536x2048]-format:1
09-07 10:17:06.411 745-745/henderson.application I/Timeline: Timeline: 
Activity_idle id: android.os.BinderProxy@54ccc23 time:51142783
09-07 10:17:13.501 745-745/henderson.application D/ViewRootImpl: 
ViewPostImeInputStage processPointer 0
09-07 10:17:13.541 745-745/henderson.application D/ViewRootImpl: 
ViewPostImeInputStage processPointer 1
09-07 10:17:13.726 745-745/henderson.application V/ActivityThread: 
updateVisibility : ActivityRecord{b1c68e8 
token=android.os.BinderProxy@54ccc23 
{henderson.application/henderson.application.Application_admin_1}} show : 
true
09-07 10:17:15.281 745-745/henderson.application E/BitmapFactory: Unable to 
decode stream: java.io.FileNotFoundException: 
/storage/emulated/0/DCIM/Camera/20170907_101702.jpg: open failed: EACCES 
(Permission denied)
09-07 10:17:15.281 745-745/henderson.application D/AndroidRuntime: Shutting 
down VM
09-07 10:17:15.281 745-745/henderson.application E/AndroidRuntime: FATAL 
EXCEPTION: main
                                                               Process: 
henderson.application, PID: 745
                                                               java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/526 flg=0x1 (has extras) }} to activity {henderson.application/henderson.application.Application_admin_1}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setBackground(android.graphics.drawable.Drawable)' on a null object reference
                                                                   at android.app.ActivityThread.deliverResults(ActivityThread.java:4927)
                                                                   at android.app.ActivityThread.handleSendResult(ActivityThread.java:4970)
                                                                   at android.app.ActivityThread.access$1600(ActivityThread.java:223)
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1851)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                   at android.os.Looper.loop(Looper.java:158)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:7231)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setBackground(android.graphics.drawable.Drawable)' on a null object reference
                                                                   at henderson.application.Application_admin_1.onActivityResult(Application_admin_1.java:82)
                                                                   at android.app.Activity.dispatchActivityResult(Activity.java:7138)
                                                                   at android.app.ActivityThread.deliverResults(ActivityThread.java:4923)
                                                                   at android.app.ActivityThread.handleSendResult(ActivityThread.java:4970) 
                                                                   at android.app.ActivityThread.access$1600(ActivityThread.java:223) 
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1851) 
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                   at android.os.Looper.loop(Looper.java:158) 
                                                                   at android.app.ActivityThread.main(ActivityThread.java:7231) 
                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

这是方法

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


    switch (requestCode) {
        case SELECTED_PICTURE:
            if (resultCode == RESULT_OK) {
                Uri uri = data.getData();
                String[] projection = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(projection[0]);
                image_file_path = cursor.getString(columnIndex);
                cursor.close();

                File f = new File(image_file_path);
                if (f.exists()){
                    Bitmap selectedImage = BitmapFactory.decodeFile(image_file_path);
                    //Bitmap reducedSizeBitmap = getBitmap(selectedImage);
                    Drawable d = new BitmapDrawable(selectedImage);
                    if (btnNo == 1) {
                        imageViewPLRD.setBackground(d);

                    } else if (btnNo == 2) {
                        imageViewIC.setBackground(d);

                    }
                }
            }
            break;
    }
}

这是xml

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:id="@+id/linearLayoutImage"
        android:layout_marginTop="5dp"
        android:layout_below="@+id/btnCamera"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:contextClickable="false">

        <ImageView
            android:layout_width="200dp"
            android:layout_height="500dp"
            android:id="@+id/imageViewPLRD"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:background="#e4e4e4" />

        <ImageView
            android:layout_width="200dp"
            android:layout_height="500dp"
            android:layout_marginLeft="2dp"
            android:id="@+id/imageViewIC"
            android:layout_weight="1"
            android:background="#e4e4e4" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:id="@+id/linearLayout"
        android:layout_below="@+id/linearLayoutImage"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Insert PLRD"
            android:id="@+id/btnPLRD"
            android:onClick="btnPLRDOnClick"
            android:layout_weight="1" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Insert IC"
            android:onClick="btn2OnClick"
            android:id="@+id/btnIC"
            android:layout_weight="1" />
    </LinearLayout>

我在调试模式下运行代码并看到声明的Bitmap selectedImage在该行之后为null,但我没有看到错误。

2 个答案:

答案 0 :(得分:0)

试试这个:

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);

以下代码是您的活动结果

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


        if (resultCode == RESULT_OK) {
            try {
                final Uri imageUri = data.getData();
                final InputStream imageStream = getContentResolver().openInputStream(imageUri);
                final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                image_view.setImageBitmap(selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(PostImage.this, "Something went wrong", Toast.LENGTH_LONG).show();
            }

        }else {
            Toast.makeText(PostImage.this, "You haven't picked Image",Toast.LENGTH_LONG).show();
        }
    }

答案 1 :(得分:0)

尝试这个......我已经多次使用过了....并且还检查了许可

AndroidManifest.xml中的

权限==&gt;

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

通话方式=====&gt;

private void getImagefromGallery() {

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(
                Intent.createChooser(intent, "Complete action using"),
                RESULT_FROM_GALLERY);
    } else {
        Intent mediaChooser = new Intent(Intent.ACTION_PICK);
        mediaChooser.setType("image/*");
        startActivityForResult(mediaChooser, RESULT_FROM_GALLERY);
    }
}

关于活动结果方法====&gt;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    switch (resultCode) {
        case RESULT_OK:

            if (requestCode == RESULT_FROM_GALLERY) {
                String picturePath = "";

                Uri selectedUri = data.getData();
                Log.d("videopathyri===>", String.valueOf(selectedUri));


                Intent intent = new Intent(getApplicationContext(),
                        MainActivityCrop.class);
                intent.putExtra("baseimage", selectedUri);

                startActivity(intent);
            }
            break;
        default:
            break;
         }
        }