无法提供结果从图库中获取图像

时间:2013-10-13 14:27:25

标签: android image

我想从图库中检索图像,但我遇到了一些问题。

这是LogCat:

 10-13 15:56:34.062: E/AndroidRuntime(25446): FATAL EXCEPTION: main
    10-13 15:56:34.062: E/AndroidRuntime(25446): java.lang.RuntimeException: Failure delivering result ResultInfo{who=Full List, request=1, result=-1, data=Intent { dat=content://media/external/images/media/31 (has extras) }} to activity {com.freerdp.afreerdp/com.freerdp.freerdpcore.presentation.TabViewActivity}: java.lang.NullPointerException
    10-13 15:56:34.062: E/AndroidRuntime(25446):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3224)
    10-13 15:56:34.062: E/AndroidRuntime(25446):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3267)
    10-13 15:56:34.062: E/AndroidRuntime(25446):    at android.app.ActivityThread.access$1200(ActivityThread.java:146)
    10-13 15:56:34.062: E/AndroidRuntime(25446):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
    10-13 15:56:34.062: E/AndroidRuntime(25446):    at android.os.Handler.dispatchMessage(Handler.java:99)
    10-13 15:56:34.062: E/AndroidRuntime(25446):    at android.os.Looper.loop(Looper.java:137)
    10-13 15:56:34.062: E/AndroidRuntime(25446):    at android.app.ActivityThread.main(ActivityThread.java:4949)
    10-13 15:56:34.062: E/AndroidRuntime(25446):    at java.lang.reflect.Method.invokeNative(Native Method)
    10-13 15:56:34.062: E/AndroidRuntime(25446):    at java.lang.reflect.Method.invoke(Method.java:511)
    10-13 15:56:34.062: E/AndroidRuntime(25446):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
    10-13 15:56:34.062: E/AndroidRuntime(25446):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
    10-13 15:56:34.062: E/AndroidRuntime(25446):    at dalvik.system.NativeStart.main(Native Method)
    10-13 15:56:34.062: E/AndroidRuntime(25446): Caused by: java.lang.NullPointerException
    10-13 15:56:34.062: E/AndroidRuntime(25446):    at com.freerdp.freerdpcore.presentation.ChangeView.onActivityResult(ChangeView.java:270)
    10-13 15:56:34.062: E/AndroidRuntime(25446):    at android.app.ActivityGroup.dispatchActivityResult(ActivityGroup.java:122)
    10-13 15:56:34.062: E/AndroidRuntime(25446):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3220)
    10-13 15:56:34.062: E/AndroidRuntime(25446):    ... 11 more
    10-13 16:01:40.195: I/Process(25446): Sending signal. PID: 25446 SIG: 9

这是onActivityResult()的代码:

@Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if(requestCode == PICK_IMAGE && data != null && data.getData() != null) {
                Uri _uri = data.getData();
                Log.v(TAG,"getData()");
                //User had pick an image.
                Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
                cursor.moveToFirst();

                //Link to the image
                final String imageFilePath = cursor.getString(0);
                File f = new File(imageFilePath);
                Log.v(TAG, "path:"+ imageFilePath);
                cursor.close();


            ImageView image = (ImageView) findViewById(R.id.list_image);
            Bitmap bitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
            image.setImageBitmap(bitmap);
        }
        }

这是Intent

Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);

你能告诉我我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

在onActivityResult方法中使用以下代码,假设您的requestCode为1000.它应该可以正常工作。

 if (requestCode == 1000) {
                if (resultCode == Activity.RESULT_OK) {
                    Uri selectedImage = data.getData();
                    System.out.println("Content Path : " + selectedImage.toString());

                    if (selectedImage != null) {
                        img1.setImageBitmap(getScaledBitmap(selectedImage));
                    } else {
                        Toast.makeText(PhotoTake.this, "Error getting Image",
                                Toast.LENGTH_SHORT).show();
                    }           
                } else if (resultCode == Activity.RESULT_CANCELED) {
                    Toast.makeText(PhotoTake.this, "No Photo Selected",
                            Toast.LENGTH_SHORT).show();
                }
            }
上面使用的

私有Bitmap getScaledBitmap(Uri uri)方法将是这样的。

private Bitmap getScaledBitmap(Uri uri){
        Bitmap thumb = null ;
        try {
            ContentResolver cr = getContentResolver();
            InputStream in = cr.openInputStream(uri);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize=8;
            thumb = BitmapFactory.decodeStream(in,null,options);
        } catch (FileNotFoundException e) {
            Toast.makeText(PhotoTake.this , "File not found" , Toast.LENGTH_SHORT).show();
        }
        return thumb ; 
    }

希望它有所帮助。