Reputation: 85
I am able to open the gallery and getting the path of gallery as= content://media/external/images/media/2 but not able to decode in imageview this is my code
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
b=(Button) findViewById(R.id.Button01);
b.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// in onCreate or any event where your want the user to
// select a file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,IMAGE_PICK);
}
});
}
//UPDATED
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == IMAGE_PICK) {
Uri selectedImageUri = data.getData();
//OI FILE Manager
filemanagerstring = selectedImageUri.getPath();
//MEDIA GALLERY
selectedImagePath = getPath(selectedImageUri);
if(selectedImagePath!=null)
{
path = selectedImageUri.toString();
m1.setPath(path);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap yourSelectedImage = BitmapFactory.decodeFile(path, options);
ImageButton img2=(ImageButton)findViewById(R.id.widget27);
img2.setImageBitmap(yourSelectedImage);
Toast.makeText(getBaseContext(), path, 1000).show();
}
}
}
}
public String getPath(Uri uri) {
String [] proj={MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(uri,
proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} }
Please help thanks in advance
Upvotes: 2
Views: 2249
Reputation: 454
We need to do following changes/fixes in our earlier onActivityResult()'s gallery picker code to run seamlessly on Kitkat and on all other earlier versions as well.
Uri selectedImgFileUri = data.getData();
if (selectedImgFileUri == null ) {
// user has not selected any photo
}
try {
InputStream input = mActivity.getContentResolver().openInputStream(selectedImgFileUri);
mSelectedPhotoBmp = BitmapFactory.decodeStream(input);
} catch (Throwable tr) {
// show message to try again
}
Upvotes: 0
Reputation: 11028
This piece of code should be put in your onActivityResult. It gives you the way to decode file path from fetched image URI:
Uri selectedImageUri = data.getData();
String[] projection = { MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(selectedImageUri, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
selectedImagePath = cursor.getString(column_index_data);
Bitmap galleryImage = BitmapFactory.decodeFile(selectedImagePath);
Upvotes: 8