How to select both normal and gif images from the gallery, and how to set condition whether the image selected is a gif or an image?
Below is the code where the image is selected from the gallery and is set in the PhotoView which is inside FrameLayout.The code MediaStore.Images.Media.getBitmap() is used to select images from the gallery but how to select gif from the gallery and how to set check condition after the image is selected that whether that selected image is a gif or simple image
PhotoView iv_add_player;
String playerimagesave;
Uri uri = data.getData();
Glide.with(this).load(uri).into(iv_add_player);
Bitmap bm = null;
try {
bm=MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
//How to set Gif in above line??
}
catch (IOException e){
e.printStackTrace();
}
playerimagesave = getImage(bm);
iv_add_player.setImageBitmap(bm);
The above code is for the selection of gallery images but how to set gif images from gallery?
This onActivityResult(int requestCode, int resultCode, Intent data){} function has the above code and is used here below-
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
You can use the latest Glide Library to show the gif, below is the code for reference:
GlideApp
.with(context)
.asGif()
.load(uri)
.into(imageView);
This will solve your problem of showing gif bitmap in your application.
Related
Noticed while testing on an older device a galaxy s4 api 17 that when choosing an image from the gallery. Done like this.
} else { // pick from file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), Constants.PICK_FROM_FILE);
}
the image would essentially be empty/hidden, except that in another functionality in my app I could still do a crop on this hidden/empty image and return a correct crop of the hidden section except this was now visible.
To test I set it to the image view I was putting in the cropped image and instead but in the result of the bitmap I received but it was also empty still it was done like this.
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK) {
if (requestCode == Constants.PICK_FROM_FILE ) {
if (data != null) {
try {
isFromCamera = false;
Constants.INSTANCE.IMAGE_PATH = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Constants.INSTANCE.IMAGE_PATH);
Constants.INSTANCE.IMAGE = bitmap;
imageViewUser.setImageBitmap(bitmap);
Constants.INSTANCE.mFromPhotoSelection = true;
performCrop(Uri.parse(""));
Now this seems to work on most images on the s4, and every image on the lollipop devices I have been testing on.
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Constants.INSTANCE.IMAGE_PATH);
This only works if you get an image from the MediaStore. There is no requirement that the user choose an activity to handle your ACTION_GET_CONTENT request that will return an image from the MediaStore. Worse, you are doing this IPC and disk I/O on the main application thread, freezing your UI in the meantime.
There are many image loading libraries available for Android, such as Picasso. Most will take a Uri and load your image asynchronously. I strongly encourage you to use one.
Otherwise, use openInputStream() on your ContentResolver, along with BitmapFactory, on a background thread, to get your Bitmap.
I have a button in my code to take a picture:
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/cameralogo"
android:id="#+id/buttonCamera" />
When i click it it opens the camera and saves a picture, path is String mCurrentPhotoPath;
after the camera intent was displayed i want the button to show the image as background (android:background="mCurrent.....")???
how do do this?
Here is the solution.
You cannot set background only by path or URI, you'll need to create a Bitmap( and use ImageButton) or a Drawable out of it.
Using Bitmap and ImageButton:
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
yourImageButton.setImageBitmap(bitmap);
Using Drawable and Button:
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
Drawable d = new BitmapDrawable(getResources(),bitmap);
yourButton.setBackground(d);
Have you had a look at this question yet?
How to set the button background image through code
You cant do this in the xml but only programmatically. Just get a reference to the newly created picture like described here:
How to get path of a captured image in android
To start the camera intent:
...
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
activity.startActivityForResult(takePictureIntent, PHOTO_ACTIVITY_REQUEST_CODE);
...
Where PHOTO_ACTIVITY_REQUEST_CODE is just a integer constant unique within activity to be used as request codes while starting intent for results.
To Receive photo in the onActivityResult, and update background of the view
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PHOTO_ACTIVITY_REQUEST_CODE && data != null) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = (Bitmap) extras.get("data");
if (photo != null) {
// mView should refer to view whose reference is obtained in onCreate() using findViewById(), and whose background you want to update
mView.setBackground(new BitmapDrawable(getResources(), photo));
}
}
}
The above code does not use full size photo. For that, you will have to ask Photo intent to save it to a file, and read the file. Details are presenthere
UPDATE!!
So Lokesh put me on the right path, and showed me that the problem was the size of the file being too large to show in the imageview. I was able to fix the imageview preview problem with the following code under my onActivityResult:
try {
Bitmap picture = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+"/td01.png");
int nh = (int) ( picture.getHeight() * (512.0 / picture.getWidth()) );
Bitmap scaled = Bitmap.createScaledBitmap(picture, 512, nh, true);
Log.v("Path", Environment.getExternalStorageDirectory().getPath()+"/td01.png");
pic1.setImageBitmap(scaled);
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
Thanks Lokesh!
----------------- ORIGINAL ISSUE BELOW THIS LINE -------------------
So I'm trying to both save an image to the SD card for use later, AND display the saved image in an imageview which also serves as the button which takes the photo. Here's the code:
pic1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent camera_intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File image1 = new File(Environment.getExternalStorageDirectory(),"td01.png");
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image1));
startActivityForResult(camera_intent, CAMERA_PIC_REQUEST1);
}
});
followed by:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case 1:
if(resultCode==RESULT_OK){
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
pic1.setImageBitmap(thumbnail);
}
}
Now, if I remove the following code from the onclick, it shows the thumbnail as I expect it to:
File image1 = new File(Environment.getExternalStorageDirectory(),"td01.png");
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image1));
...but without that code, it doesn't save the file to my sd card.
The problem is, if I don't remove that code, it saves the image to my SD Card but immediately crashes before returning to the activity after tapping SAVE in the camera activity, unless I remove the following code from my onActivityResult:
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
pic1.setImageBitmap(thumbnail);
I've also tried many variations of the following code in my onActivityResult, hoping to display it from the actual file in a different way, but it never works and only shows a blank imageview, but at least in that case the crash doesn't occur, because I removed the get extras get data code:
Bitmap photo1 = BitmapFactory.decodeFile("/sdcard/td01.png");
pic1.setImageBitmap(photo1);
I've been struggling with this for days and am at a loss here. Hope someone can show me what stupid thing I'm doing wrong and explain why this isn't working.
Thanks!
Try this:
try {
Bitmap picture = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+"/DCIM/MyPhoto.jpg");
Log.v("Path", Environment.getExternalStorageDirectory().getPath()+"/DCIM/MyPhoto.jpg");
mImageView.setImageBitmap(picture);
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
do NOT access the SD card directly, try accessing it trough Environment Like this
String imageDir = Environment.getExternalStorageDirectory()+"/apple.jpg";
and then you can call BitmapFactory:
Bitmap myBitmap = BitmapFactory.decodeFile(imageDir);
I am using AQuery to load images taken by camera and displaying them in my activity in a image view. The problem is that when I try to do the same but instead of taking a picture, just select the image from my gallery, my image view appears to have nothing in it. This is my code:
//Get image uri that was selected in gallery
Uri selectedImage = data.getData();
//Convert uri to string path
strMainPic = selectedImage.toString();
//Create file to add as parameter to AQuery
File Main = new File(strMainPic);
aq.id(R.id.image_one).image(Main, 100);
If I use the selectedImage and change it to a Bitmap with BitmapFactory, it works but the performance suffers. What am I doing wrong?
I just solved it a couple of seconds ago. I used this code:
Uri selectedImage = data.getData();
try {
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
aq.id(R.id.image_one).image(bmp, AQuery.RATIO_PRESERVE);
I just added this to my onActivityResult() method.
i want to choose a picture from SD card of the mobile. i am using below code to choose and to display in my activity
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Uri uri = Uri.parse(selectedImagePath);
uploadimage.setImageURI(uri);
It is working fine, but I want to convert this image into Bitmap, I have image path and URI.
How to convert image to Bitmap in this case? Please help me, thanks in advance.
use this code
Bitmap bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),uri);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);