private static int RESULT_LOAD = 1;
String img_Decodable_Str;
ImageView imageView = (ImageView) findViewById(R.id.Gallery);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD);
}
});}
@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 && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
img_Decodable_Str = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.Gallery);
// Set the Image in ImageView after decoding the String
imageView.setImageBitmap(BitmapFactory
.decodeFile(img_Decodable_Str));
} else {
Toast.makeText(this, "Hey pick your image first",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went embrassing", Toast.LENGTH_LONG)
.show();
}
}}
我有 ImageView 。如果用户点击 ImageView ,他将能够添加他选择的图像。当我点击 ImageView 时,我被重定向到图库,但是当我选择图像时,该图像未显示在 ImageView 中。哪里出错了?
答案 0 :(得分:0)
在短期内,请替换:
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
img_Decodable_Str = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.Gallery);
// Set the Image in ImageView after decoding the String
imageView.setImageBitmap(BitmapFactory
.decodeFile(img_Decodable_Str));
使用:
Uri selectedImage = data.getData();
imageView.setImageURI(selectedImage);
稍后,使用图像加载库,例如Picasso或Glide,因为setImageURI()
会在主应用程序线程上加载图像,只要该工作需要,就会冻结UI。