I use this to let the user pick an image from gallery:
Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.setType("image/*");
i.putExtra("crop", "true");
i.putExtra("scale", true);
i.putExtra("return-data", true);
startActivityForResult(i, RESULT_LOAD_IMAGE);
Then it will be displayed in an ImageView:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
final Bundle extras = data.getExtras();
Bitmap photo = extras.getParcelable("data");
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageBitmap(photo);
}
}
It works but the picture now has a bad quality.
How to fix this?
You shouldn't specify any of those extras because they are device-dependent. Just do this:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
and
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK)
{
Uri selectedImage = data.getData();
//load Uri into ImageView
}
}
I would look into using a library like Picasso to load the images instead of just doing imageView.setImageBitmap(photo) if your app is image intensive.
Related
Help me to set the uri of the image button to the image that i select form the internal storage. I have already given required permissions in the android_manifest.xml file. The problem is that when i browse to the internal storage of phone the images are unselectable.
public void onUploadImageClick(View view){
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("Image/*");
startActivityForResult(galleryIntent, GALLERY_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
uri=data.getData();
imageButton = (ImageButton)findViewById(R.id.image_button);
imageButton.setImageURI(uri);
}
}
public void onUploadImageClick(View view){
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY_REQUEST);
}
Then in your on activity result method keep this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
uri=data.getData();
imageButton = (ImageButton)findViewById(R.id.image_button);
imageButton.setImageURI(uri);
}
}
EDIT
yes the problem is I forgot to add .Media
so use this line of code:
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
You can try below code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
uri=data.getData();
String pathName = uri.toString();
Bitmap bmp = BitmapFactory.decodeFile(pathName);
imageButton = (ImageButton)findViewById(R.id.image_button);
imageButton.setImageBitmap(bmp);
}
}
Can you try this. You could replace ImageView to ImageButton
String qrPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Image.png";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap mustOpen = BitmapFactory.decodeFile(qrPath, options);
setContentView(R.layout.qr_image);
ImageView imageView = (ImageView) findViewById(R.id.qr_image);
imageView.setImageBitmap(mustOpen);
I'm trying to open an image using intent.
The path of the image in my storage is /storage/emulated/0/nitp/download/logo.png.
My code is
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DEFAULT);
intent.setDataandType(Uri.parse("/storage/emulated/0/nitp/download/logo.png"),"image/*");
startActivity(intent);
I also tried putting file://storage/emulated/0/nitp/download/logo.png
and content://storage/emulated/0/nitp/download/logo.png
What is the path I should use?
Solved
Have to use file:///storage/emulated/0/nitp/downloads/logo.png
For Pick image from media Storage or SD Card:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
And For get the path of Image and set in ImageView:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
try {
//Getting the Bitmap from Gallery
imgpath= MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting the Bitmap to ImageView
imageViewProfileImage.setImageBitmap(imgpath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Hope this work.
I am trying to give the user the option to either choose an image from the phone's storage or take one with the phone's camera app and then display that image in an imageView. With the help of this and this question I am able to display the image when it is choosen from the storage. However if I choose to take a picture with camera although I don't get an error the image is not shown in the imageView.
The function for choosing the picture looks like:
public void choosePic(View view) {
Intent pickIntent = new Intent();
pickIntent.setType("image/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String pickTitle = "Select or take a new Picture"; // Or get from strings.xml
Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
chooserIntent.putExtra
(
Intent.EXTRA_INITIAL_INTENTS,
new Intent[] { takePhotoIntent }
);
startActivityForResult(chooserIntent, SELECT_PICTURE);
}
After that it is handled by
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_IMAGE && data != null && data.getData() != null) {
Uri _uri = data.getData();
//User had pick an image.
Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
image = (ImageView) findViewById(R.id.imageView);
image.setImageURI(_uri);
cursor.close();
}
super.onActivityResult(requestCode, resultCode, data);
}
EDIT
With #Murtaza Hussain's helps I was able to find a working solution:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("requestCode",""+requestCode);
if(requestCode == PICK_IMAGE && resultCode==RESULT_OK && data != null && data.getData() != null) {
Uri _uri = data.getData();
//User had pick an image.
Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
image = (ImageView) findViewById(R.id.imageView);
image.setImageURI(_uri);
cursor.close();
}
else if(requestCode== PICK_IMAGE && resultCode==RESULT_OK && data.getData() == null){
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
image =(ImageView) findViewById(R.id.imageView);
image.setImageBitmap(thePic);
}
super.onActivityResult(requestCode, resultCode, data);
}
You need to add code for it in onActivityResult()
if(requestCode==0 && resultCode==RESULT_OK ){
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
image =(ImageView) findViewById(R.id.imageView);
image.setImageBitmap(thePic);
}
In your activity when to capture image from camera. write the below code.
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"/imageLarge"+System.currentTimeMillis()+".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, 111);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
In onActivityResult() write the below code..
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 111:
if(resultCode == RESULT_OK)
{
Log.v("","After camera capture path is --> "+mImageCaptureUri.toString().substring(8));
Bitmap bitmap=BitmapFactory.decodeFile(mImageCaptureUri.toString().substring(8));
image.setImageBitmap(bitmap);
}
else
{
}
}
}
Do not forget to add one permission.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
In case of any query, Please let me know. Thanks.
How do I read and write image to/from a local file in android?
I want to take an image from a file, save it in memory and then read it from file.
I am using this code to write image in file:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String fileName=GetFileName().replace('.', ' ')+".png";
File out = new File(Environment.getExternalStorageDirectory()+File.separator +fileName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
startActivityForResult(intent, REQUEST_IMAGE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
wb.loadUrl(sendUrl);
if(requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
String imgPath= data.getExtras().get("Uri").toString();
bm = BitmapFactory .decodeFile(imgPath);
}
}
How can I read the image back?
Try this---
Uri mUriSavedImage;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String fileName=GetFileName().replace('.', ' ')+".png";
File out = new File(Environment.getExternalStorageDirectory()+File.separator +fileName);
mUriSavedImage= Uri.fromFile(out);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mUriSavedImage);
startActivityForResult(intent, REQUEST_IMAGE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
wb.loadUrl(sendUrl);
if(requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
String imgPath= mUriSavedImage.getPath();
bm = BitmapFactory.decodeFile(imgPath);
}
}
I have an activity in which i am calling intent of camera on click, but when I capture image and get back to the activity result my Image view reference is null. I am getting URI correctly. This happens in android Lollipop.
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
//retrieve a reference to the ImageView
Log.i("dataa",String.valueOf(thePic));
Log.i("dataaimg",String.valueOf(image));
Log.i("dataaimgno",String.valueOf(imageno));
image.setImageBitmap(thePic); // here is the problem image reference is null when get back here
}
}
This code is working fine in versions below lollipop.
Try this:
private static final int CAPTURE_IMAGE_ACTIVITY_REQ = 0;
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQ );
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQ) {
if (resultCode == RESULT_OK) {
Bitmap bp = (Bitmap) data.getExtras().get("data");
image = setImageBitmap(bp);
}
}