Reputation: 239
I'm a noob to android and i want to set an ImageButton image with a file form the SDcard. However, getBitmap isn't creating a working bitmap. When i set the ImageButton with the bitmap that has just been created, the dimensions of the imageButton change but the image doesn't appear. This is really frustrating and Any help resolving this is greatly appreciated.
MYCODE
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CODE:
// If the file selection was successful
if (resultCode == RESULT_OK) {
if (data != null) {
// Get the URI of the selected file
final Uri uri = data.getData();
try {
// Create a file instance from the URI
final File file = FileUtils.getFile(uri);
Toast.makeText(Profile_Barber.this,"File Selected: "+file.getAbsolutePath(), Toast.LENGTH_LONG).show();
Log.e("URI", uri.toString());//Returns: content://media/external/images/media/1834
Bitmap bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
if(bmp==null){
Log.e("BMP NULL", "This that bullshit!");
}else{
Log.e("BMP NOT NULL", bmp.toString()); //Returns: BMP NOT NULL android.graphics.Bitmap@4152b5a0
profilepic.setImageBitmap(bmp);
}
} catch (Exception e) {
Log.e("FileSelectorTestActivity", "File select error", e);
e.printStackTrace();
}
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
Upvotes: 1
Views: 1983
Reputation: 2158
How about using this to decode image?
Uri contentURI = Uri.parse(data.getDataString());
ContentResolver cr = getContentResolver();
InputStream in = cr.openInputStream(contentURI);
Bitmap pic = BitmapFactory.decodeStream(in,null,null);
Upvotes: 1