Android 从库中选择图像后,应用程序强制关闭

Android 从库中选择图像后,应用程序强制关闭,android,Android,我不知道错误在哪里,我的代码是从一个工作正常的项目复制粘贴的。图片可以拍摄并保存到gallery中,但当我想将其加载到imageview中时,应用程序崩溃。我一直在四处寻找答案,但都不适合我。这是应用程序崩溃时的日志 09-07 10:17:06.371 745-745/henderson.application E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated

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

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>

我在调试模式下运行代码,发现声明的位图selectedImage在该行之后为空,但我看不出有什么问题。

尝试以下操作:

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();
        }
    }

试试这个…我已经用过很多次了。。。。并检查权限

AndroidManifest.xml中的权限==>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
关于活动结果方法==>

    @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;
         }
        }

好的,我刚刚看到日志的权限被拒绝了,我已经有了那个标记,但我不确定它是否是正确的…
NullPointerException:trust to invoke virtual method'。。。空对象引用上的ImageView.setBackground(…)-该崩溃是因为
imageViewPLRD
imageViewIC
或两者都为空。我在尝试时出错,说decodeFile无法应用于InputStreamReference另一个代码可能会帮助您:好的,添加了String.valueOf(image)来更正它,现在它说NullPointer你给出的链接中的代码示例与我的完全相同@Bapusaheb Shindei我不认为我会尝试这种方法,我已经解决了权限部分,现在当行imageView.setImageResource(d)时,我有空对象引用;运行,即使d不是空的。您输入了错误的代码来设置图像,这就是空指针错误出现的原因…请尝试以下代码===>
imageView.setImageDrawable(res)
图像视图。可设置背景图(bg)
    @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;
         }
        }