如何从uri获取图片ID?

时间:2014-10-20 05:40:14

标签: android uri

我有这段代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Button play= (Button) findViewById(R.id.play);
    play.setVisibility(1);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
}
public void Play(View view){
    int[] mImageIds = {R.id.imgView};
    Intent intent = new Intent(this, PuzzleSelectActivity.class);
    intent.putExtra("images", mImageIds);
    this.startActivity(intent);
}

我想从受保护的void onActivityResult获取图片ID并将其传递给mImageIds ......

1 个答案:

答案 0 :(得分:0)

 protected void onActivityResult(int requestCode, int resultCode, Intent data)
 {
   super.onActivityResult(requestCode, resultCode, data);
   if (resultCode == RESULT_OK)
    {
    Uri imageUri = data.getData();
    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
    ImageView imageView = (ImageView) findViewById(R.id.imgView);
    imageView.setImageBitmap(bitmap);
   }
}

Get Bitmap from an Uri [android]