How to send a uri path of an image to another activity and convert it to image. I tried the below
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
//file name
Uri selectedImage = data.getData();
Intent i = new Intent(this,
AddImage.class);
i.putExtra("imagePath", selectedImage);
startActivity(i);
and get it like this
String imagePath = getIntent().getStringExtra("imagePath");
imageview.setImageURI(Uri.parse(imagePath ));
Convert you URI to String while adding to Intent like given below
i.putExtra("imagePath", selectedImage.toString());
and in your NextActivity get the String and convert back to URI like ->
Intent intent = getIntent();
String image_path= intent.getStringExtra("imagePath");
Uri fileUri = Uri.parse(image_path)
imageview.setImageURI(fileUri)
First Activity
Uri uri = data.getData();
Intent intent=new Intent(Firstclass.class,secondclass.class);
intent.putExtra("imageUri", uri.toString());
startActivity(intent);
Second class
Imageview iv_photo=(ImageView)findViewById(R.id.iv_photo);
Bundle extras = getIntent().getExtras();
myUri = Uri.parse(extras.getString("imageUri"));
iv_photo.setImageURI(myUri);
to use the returned uir from the calling activity and then set it to a imageview you can do this
Uri imgUri=Uri.parse(imagePath);
imageView.setImageURI(null);
imageView.setImageURI(imgUri);
This is a workaround for refreshing an ImageButton, which tries to cache the previous image Uri. Passing null effectively resets it.
For converting the inputStream into a bitmap you could do this
InputStream in = getContentResolver().openInputStream(Uri.parse(imagePath));
Bitmap bm = BitmapFactory.decodeStream(getContentResolver().openInputStream(in));
and then call
image.setImageBitmap(bm);
to set it it a imageview,
you could also check this link for an example
hope i could help
in Next activity get that URI like this;
Intent intent = getIntent();
String image_path= intent.getStringExtra("YOUR Image_URI");
and to convert that Image_URI to Image use Below mentioned Code
File imgFile = new File(image_path);
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imageView.setImageBitmap(myBitmap);
}
To pass an image Uri to the next activity, you can just use setData() and getData(). There is no need to convert the Uri to anything.
First Activity
Intent intent = new Intent(this, SecondActivity.class);
intent.setData(uri);
startActivity(intent);
Second Activity
// get Uri
Uri uri = getIntent().getData();
// decode bitmap from Uri
if (uri == null) return;
try {
InputStream stream = getContentResolver().openInputStream(uri);
if (stream == null) return;
Bitmap bitmap = BitmapFactory.decodeStream(stream);
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Related
In my app, when users clicking an imageview, users can choose an image from gallery or capture an image from camera and display it on imageview. I can display the image on imageview if the image is selected from gallery but it failed to display if the image is captured. The imageUri is null if the image is captured.
Can anyone help me to solve this problem?
There are some codes below, if you need more info please comment below
private final static int PICK_IMAGE_REQUEST = 1;
private final static int CAMERA = 2;
private Uri imageUri;
private void takePhotoFromCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA);
}
public void selectImage() {
Intent photoPickerIntent = new Intent();
photoPickerIntent.setType("image/*");
photoPickerIntent.setAction(Intent.ACTION_PICK);
startActivityForResult(photoPickerIntent, PICK_IMAGE_REQUEST);
}
#Override
public 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) {
imageUri = data.getData();
Picasso.get().load(imageUri).into(circleImageView);
System.out.println("haha pic " + imageUri);
}
if (requestCode == CAMERA && resultCode == RESULT_OK && data != null && data.getData() != null){
imageUri = data.getData();
Picasso.get().load(imageUri).into(circleImageView);
System.out.println("haha camera " + imageUri);
}
}
I would suggest you display your image as a bitmap on imageview, after that convert it into url form and store into database.
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
call this method in your onActivityResult method and put this code inside
Bundle extras = data.getExtras();
bitmap = (Bitmap) extras.get("data");
circleImageView.setImageBitmap(bitmap);
imageUri = getImageUri(getApplicationContext(),bitmap);
I'm creating an app to take photos and delete image from gallery after specific process. Here is my code
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(takePictureIntent, requestCode);
}
And I handle the result just like this
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
image1 = (Bitmap) extras.get(IMAGE_BUNDLE_NAME);
imageView1.setImageBitmap(image1);
imageUri1 = data.getData();
}
}
The problem is that data.getData(); returns null in some devices. I tried to replace URI with this code
imageUri1 = getImageUri(getActivity(), image1);
And this method
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
Whe I use this Uri I can't delete file from device. This is how I delete images
private void deleteFileFromMediaStore(final ContentResolver contentResolver, int requestCode) {
String canonicalPath;
File fdelete = new File(imageUri1.getPath());
if(fdelete != null){
try {
canonicalPath = fdelete.getCanonicalPath();
} catch (IOException e) {
canonicalPath = fdelete.getAbsolutePath();
}
final Uri uri = MediaStore.Files.getContentUri(Constants.EXTERNAL_STORAGE_CONSTANT);
final int result = contentResolver.delete(uri,
MediaStore.Files.FileColumns.DATA + "=?", new String[] {canonicalPath});
if (result == 0) {
final String absolutePath = fdelete.getAbsolutePath();
if (!absolutePath.equals(canonicalPath)) {
contentResolver.delete(uri,
MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath});
}
}
}
}
How can I delete the photo or how can I make data.getData() non null on all devices?
The problem is that data.getData(); returns null in some devices
It will return null with most camera apps, as it is supposed to return null.
How can I delete the photo
Save the thumbnail photo to a file (compress() and a FileOutputStream).
Or, use EXTRA_OUTPUT to request that the camera app save a full-size photo to a location that you specify (e.g., using a FileProvider-supplied Uri).
Or, use a library like Fotoapparat to take photos directly in your app, rather than relying on one of hundreds of camera apps.
how can I make "data.getData()" non null on all devices?
You can't.
I want to pass image picked from gallery to other intent. But when I click upload button and then select image from gallery it shows me the same activity. As if i am stuck on that activity. Can anyone help?????
This is my code..
if (resultCode == RESULT_OK && data != null) {
Uri photouri = data.getData();
if (photouri != null) {
try {
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(photouri,
filePath, null, null, null);
cursor.moveToFirst();
int ColIndex = cursor.getColumnIndex(filePath[0]);
String FilePath = cursor.getString(ColIndex);
cursor.close();
photo = BitmapFactory.decodeFile(FilePath);
Intent intent2 = new Intent(Activity1.this, Activity2.class);
intent2.putExtra("BitmapImage", photo);
startActivity(intent2);
} catch (Exception e) {
}
}
Code in Activity2
pic1= (Bitmap) this.getIntent().getParcelableExtra("BitmapImage");
img.setImageBitmap(pic1);
I am doing this in my app but instead of calling decodeFile in first activity,
I just passed the actual filename (path) selected from the gallery and
let the second activity decode it.
Intent intent2 = new Intent(Activity1.this,Activity2.class);
Activity2.intent2.putString("BitmapFilename",FilePath);
startActivity(intent2);
I implemented the application for getting image from the camera album in sdcard.But it is not working properly.
Here Intent returns like this Intent { act=com.htc.HTCAlbum.action.ITEM_PICKER_FROM_COLLECTIONS dat=content://media/external/images/media/9 typ=image/jpeg (has extras) }
In the code
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
Here (Bitmap) data.getExtras().get("data")
this part returns null.
How to get the bitmap here please can anybody help me.
Code:
cam_images_btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent cam_ImagesIntent = new Intent(Intent.ACTION_GET_CONTENT);
cam_ImagesIntent.setType("image/*");
startActivityForResult(cam_ImagesIntent, CAMERA_IMAGES_REQUEST);
}
});
if(requestCode == CAMERA_IMAGES_REQUEST && resultCode==Activity.RESULT_OK)
{
System.out.println("data(CAMERA_IMAGES_REQUEST):"+data);
if(data != null)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
System.out.println("Bitmap(CAMERA_IMAGES_REQUEST):"+thumbnail);
System.out.println("cap_image(CAMERA_IMAGES_REQUEST):"+cap_image);
cap_image.setImageBitmap(thumbnail);
}
else
{
System.out.println("SDCard have no images");
Toast.makeText(camera.this, "SDCard have no images", Toast.LENGTH_SHORT);
}
}
thanks
Do the following in your code:
if(data != null)
{
Uri selectedImageUri = data.getData();
filestring = selectedImageUri.getPath();
Bitmap thumbnail = BitmapFactory.decodeFile(filestring, options2);
System.out.println("Bitmap(CAMERA_IMAGES_REQUEST):"+thumbnail);
System.out.println("cap_image(CAMERA_IMAGES_REQUEST):"+cap_image);
cap_image.setImageBitmap(thumbnail);
}
This should work.
Edit:
Also if you want a "thumbnail" do the following:
Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(
getContentResolver(), selectedImageUriId,
MediaStore.Images.Thumbnails.MICRO_KIND,
(BitmapFactory.Options) null);
Well the way go about doing this is very easy, just:
//Get incoming intent
Intent intent = getIntent();
intent.setType("image/*");
String action = intent.getAction();
String type = intent.getType();
if(Intent.ACTION_SEND.equals(action) && type != null){
handleIncomingData(intent);
}
public void handleIncomingData(Intent data){
Uri imageSelected = data.getParcelableExtra(Intent.EXTRA_STREAM);
try{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageSelected);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
Remember to put this code after everything is initialized first or else you will get a NullPointerException. I prefer to put it at the bottom of the onCreate()
This problem can be solved by writing fewer lines of codes.
if(requestCode== your_request_code){
if(resultCode==RESULT_OK){
Uri uri = data.getData(); //<----get the image uri
imageView.setImageURI(uri); //<----set the image uri
}
I have this code:
public void onGalleryRequest() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent,
getResources().getString(R.string.selectImage)),
GALLERY_REQ);
}
and then in onActivityResult I make this test:
if (requestCode == CAMERA_PIC_REQUEST && data != null
&& resultCode != 0)
it works for me api level 7
I've read the example to do this:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Uri imageUri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
}
}
But i get java.io.FileNotFoundException: No content provider: /sdcard/Hello/1310610722879.jpg
My code is here:
Uri uri1 = Uri.parse(Config.getPhotoPath(this));
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri1);
attachButton.setImageBitmap(bitmap);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Any ideas how to make it work?
Ok I messed around, u have to do this:
Uri uri1 = Uri.parse("file://" + Config.getPhotoPath(this));
Ok I messed around, u have to do this:
Uri uri1 = Uri.parse("file://" + Config.getPhotoPath(this));
Or you can do
File file = new file(Config.getPhotoPath(this));
Uri uri1 = Uri.fromFile(file);