AQuery select image from gallery - android

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.

Related

How to select GIF from gallery and show it in image-view

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.

Bitmap loading very slow

I'm using this method for loading albumArt in list
long thisAlbum = musicCursor.getLong(albumColumn);
Bitmap artWork = null;
Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, thisAlbum);
try {
artWork = MediaStore.Images.Media.getBitmap(
musicResolver, albumArtUri);
artWork = Bitmap.createScaledBitmap(artWork, 150, 150, true);
} catch (FileNotFoundException exception) {
exception.printStackTrace();
artWork = BitmapFactory.decodeResource(getResources(),
R.drawable.no_cover);
} catch (IOException e) {
e.printStackTrace();
}
songsList.add(new Songs(thisId, thisTitle, thisArtist, artWork));
}
everything is working fine but when i open my activity it takes more then 10 seconds to load the activity and when i remove this bunch of code activity open as normally ,can anyone tell me please why it is happening and please tell me also what to do or any update for code
Instead of Bitmap I always use glide library(It gives smoothness to app)
So just add it in build.gradle(Module App)
compile 'com.github.bumptech.glide:glide:3.7.0
code to load image in imageview using glide
Glide.with(context).load(imagePath).crossFade().diskCacheStrategy(DiskCacheStrategy.ALL).thumbnail(0.5f).into(imageView);
I Hope It will Help You :)
You are trying to make the bitmap from the images from your phone.
I think the images in your phone are too large.
You can use Glide to make Bitmap for your local image.
Glide.with(mContext)
.load(new File(pictureUri.getPath())) // Uri of the picture
.asBitmap().
into(100, 100). // Width and height
get();
Remember to run this code within AsyncTask or Thread.
Hope it will helpfull to you.

Android - Get URI from Picture that i just have taken (without save the image again)

I've found a lot of answers for questions like mine but they doesn't make sense for me. Let me explain.
I have a ImageButton that let the user take a picture and display it on interface. When i try to get image URI, it returns null:
Uri uri = data.getData();
I've made some searches on internet and found solutions like:
#Override
public void onActivityResult(int requestCode, int resultCode,
Intent data) {
try {
if (resultCode == Activity.RESULT_OK) {
updateProfilePicure = Boolean.TRUE;
switch(requestCode){
case 0:
Bundle extras = data.getExtras();
Object xx = data.getData();
Bitmap imageBitmap = (Bitmap) extras.get("data");
Uri tempUri = getImageUri(imageBitmap);
imageView.setImageBitmap(imageBitmap);
break;
default: break;
}
}
} catch(Exception e){
e.printStackTrace();
}
}
public Uri getImageUri(Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(
ApplicationContext.getInstance().getContext().getContentResolver(), inImage,
"Title", null);
return Uri.parse(path);
}
For me, it doesn't make sense because when the call goes to method onActivityResult(), the picture is already saved on DCIM folder and don't have any reason to save it again. So why should i use it?
Is possible to find another way to retrieve the URI from captured image?
Thank's in advance.
the picture is already saved on DCIM folder and don't have any reason to save it again.
Not necessarily. Quoting the documentation for ACTION_IMAGE_CAPTURE:
The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field.
(the "extra field" here is an extra keyed as data)
The code snippet that you pasted is retrieving the data extra, and so the image is not stored anywhere.
Is possible to find another way to retrieve the URI from captured image?
You already have the code for this, in your first code snippet -- if you are specifying the Uri as EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE request, you will get a Uri back to the image that was taken in the Intent handed to onActivityResult().

Share image with ShareActionProvider from Picasso

i spand few hours to find this solution...
so i decided to share this informatiom, maybe some one itwill helpful :)
The first way, shown below, takes the bitmap from the view and loads it into a file.
// Get access to ImageView
ImageView ivImage = (ImageView) findViewById(R.id.ivResult);
// Fire async request to load image
Picasso.with(context).load(imageUrl).into(ivImage);
and then later assuming after the image has completed loading, this is how you can trigger a share:
// Can be triggered by a view event such as a button press
public void onShareItem(View v) {
// Get access to bitmap image from view
ImageView ivImage = (ImageView) findViewById(R.id.ivResult);
// Get access to the URI for the bitmap
Uri bmpUri = getLocalBitmapUri(ivImage);
if (bmpUri != null) {
// Construct a ShareIntent with link to image
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/*");
// Launch sharing dialog for image
startActivity(Intent.createChooser(shareIntent, "Share Image"));
} else {
// ...sharing failed, handle error
}
}
// Returns the URI path to the Bitmap displayed in specified ImageView
public Uri getLocalBitmapUri(ImageView imageView) {
// Extract Bitmap from ImageView drawable
Drawable drawable = imageView.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable){
bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
return null;
}
// Store image to default external storage directory
Uri bmpUri = null;
try {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
Make sure to add the appropriate permissions to your AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
The second way to share an Image does not require you to write the image into a file. This code can safely be executed on the UI thread. The approach was suggested on this webpage http://www.nurne.com/2012/07/android-how-to-attach-image-file-from.html .
ImageView siv = (ImageView) findViewById(R.id.ivResult);
Drawable mDrawable = siv.getDrawable();
Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();
String path = Images.Media.insertImage(getContentResolver(),
mBitmap, "Image Description", null);
Uri uri = Uri.parse(path);
return uri;
You get the Drawable from the ImageView. You get the Bitmap from the Drawable. Put that bitmap into the Media image store. That gives you a path which can be used instead of a file path or URL. Note the original webpage had an additional problem with immutable bitmaps, solved by drawing the bitmap into a canvas (never shown on screen). See linked page above for details.

converting image to bitmap in android

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);

Categories

Resources