ImageView.setImageBitmap为空

时间:2013-07-16 16:56:17

标签: android android-camera android-imageview

我使用MediaStore拍了照并保存了。我可以在我的画廊中看到图像。但是,当我尝试将图像位图设置为我的ImageView时,它变为空白。

String abspath;

public void getPicture(View view) { 

        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        File imageFile = new File(PATH + "/image.jpg");
        abspath = imageFile.getAbsolutePath().toString();

        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(abspath)))

        startActivityForResult(cameraIntent, RESULT_CAMERA);        
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    setContentView(R.layout.activity);

    if(requestCode == RESULT_CAMERA) {
            // open the image file
            Bitmap bitmap = BitmapFactory.decodeFile(abspath);  
            ImageView imageView = (ImageView)findViewById(R.id.imageView);

            imageView.setImageBitmap(bitmap); //imageView is now blank, although my saved image is not a blank image
            //imageView.setImageURI(Uri.fromFile(new File(abspath))); same result

}

}

3 个答案:

答案 0 :(得分:1)

我不知道,但是在API版本17中将照片作为onActivityResult()的一部分从相机获取时会有一些变化。您正在尝试使用imagePath获取图像。但实际上相机并没有按名称存储它。它使用自己的机制存储图像。你可以通过进入图库看到。图像名称将与您给出的名称不同。建议最好使用ContentResolver查询上次捕获的图像。您可以根据需要修改查询。以下只是一个例子。

 protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PIC_CAMERA_IMAGE && resultCode == RESULT_OK) {

            Uri selectedImage=null;
                    // Works on API 16 or Below
            if(null!=data)
            {
                selectedImage = data.getData(); 
            }
                    // For API 17 
            if(null==selectedImage)
            {
                final ContentResolver cr = getContentResolver();    
                final String[] p1 = new String[] {
                        MediaStore.Images.ImageColumns._ID,
                        MediaStore.Images.ImageColumns.DATE_TAKEN
                };                  
                Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC");     
                if ( c1.moveToFirst() ) {
                    String uristringpic = "content://media/external/images/media/" +c1.getInt(0);
                    selectedImage = Uri.parse(uristringpic);                    
                }
                c1.close();

            }


        }

    }

答案 1 :(得分:0)

在我自己的代码中,我在尝试从媒体URI读取之前进行了以下调用:

activity.getContentResolver().notifyChange(mediaUri, null);
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(mediaUri);
activity.sendBroadcast(intent);

这是为了确保系统已正确更新以解析内容URI。

编辑抱歉,我注意到您正在尝试从文件路径而不是URI中读取。我不知道为什么会失败(除了权限问题),但也许您可以尝试通过从URI.fromFile(imageFile)获取的URI来访问该文件。

答案 2 :(得分:0)

这对我有用:

Intent intent = new Intent(
                            MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT,
                            mCapturedImageURI);
                    startActivityForResult(intent, TAKE_PICTURE)

接下来是:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    String imagePath = null;                
        if (requestCode == TAKE_PICTURE)
            if (resultCode == Activity.RESULT_OK) {                 
                imagePath = getPath(mCapturedImageURI);
            }
    }

    if (imagePath != null) {
        // TODO: to this from asynctask
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(imagePath);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        mBabymScaledPhoto = mApp.getBabyBitmapFromPath(imagePath,
                orientation);
        ImageView iv = (ImageView) fragView
                .findViewById(R.id.welcome_iv_babyPhoto);
        iv.setImageBitmap(mBabymScaledPhoto);
    }

}
public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    CursorLoader cursorLoader = new CursorLoader(fragView.getContext(),
            uri, projection, null, null, null);
    Cursor cursor = cursorLoader.loadInBackground();
    if (cursor != null) {
        // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
        // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } else
        return null;
}