从图库中选取图像并设置为imageview

时间:2015-10-08 08:59:21

标签: android bitmap cursor gallery

我试图从图库中选择图片并将其设置为imageview。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        // When an Image is picked
        if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                && null != data) {

            Log.e("1","1");
            // Get the Image from data

            Uri selectedImage = data.getData();
            Log.e("2","2");

            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Log.e("3","3");

            // Get the cursor
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            Log.e("4","4");
            // Move to first row
            cursor.moveToFirst();
            Log.e("5", "5");

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            Log.e("6","6");
            imgDecodableString = cursor.getString(columnIndex);
            Log.e("7","7");
            cursor.close();
            ImageView imgView = (ImageView) findViewById(R.id.imgView);
            Log.e("8","8");
            // Set the Image in ImageView after decoding the String
            imgView.setImageBitmap(BitmapFactory
                    .decodeFile(imgDecodableString));

        } else {
            Toast.makeText(this, "You haven't picked Image",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                .show();
    }

}

在日志中,它仅显示第三个日志,然后显示tocat中的哪个。我使用三星S4,而Android版本是5.0.1。 我认为,问题是关于光标,但无法修复它。请帮帮我。

0 个答案:

没有答案