How to get the Uri path from a ImageView? - android

I am trying to create a wallpaper application. All images will come from internet.
Before of set as background, I need to crop the image. And for this, I am using an API named AndroidImageCropper. This API need of a URI object to execute the crop.
How can I get the Uri from ImageView? Because my image is from internet. The image is not on drawable folder. I am confusing. Maybe you guys can help me.
public class MainActivity extends AppCompatActivity {
private NetworkImageView imageView;
private Uri mCropImageUri;
public static final String URL_PHOTO = "http://cdn.wonderfulengineering.com/wp-content/uploads/2016/02/iron-man-wallpaper-42-610x1084.jpg";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (NetworkImageView) findViewById(R.id.image_view);
// Singleton for Volley API, Load image from internet
ImageLoader loader = MySingleton.getInstance(this).getImageLoader();
loader.get(URL_PHOTO, ImageLoader.getImageListener(imageView, R.drawable.temp_picture, android.R.drawable.ic_dialog_alert));
imageView.setImageUrl(URL_PHOTO, loader);
// Put the image on URI object
// #################################
// ### mCropImageUri = <-- ###
// #################################
}
// Action for my button
public void cropImage(View view) {
// API to crop an image
CropImage.activity(mCropImageUri)
.start(this);
}
}
Do you have any suggestion to me?
Thanks.

So you're using Volley? I'm not very familiar with Volley, both just some of my thoughts.
After image is downloaded to Android device, try to find the absolute path of the file and use android.net.Uri.fromFile to get a uri from the image.
Or else, can you use Bitmap object directly, instead of an uri.
OK, after some investigation, I think you have two solutions.
Using volley to download image as file, without using ImageLoader. In this way it's easy to get file uri of the image.
For Android-Image-Cropper, you can use Activity or use View(refer here). You can get the Bitmap object of the image in method onLoadingComplete of ImageLoadingListener. Then use com.theartofdev.edmodo.cropper.CropImageView to crop the image.

Related

Glide does not rotate images from camera correctly

I have a problem with Glide:
If I take a picture from Camera PORTRAIT mode (in my case Pixel XL), the image that is loaded later is rotated by 90° counter clock wise.
Used Glide:
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
Here is my method to load images:
public class BetterRoundedImageView extends RoundedImageView {
private static final String TAG = BetterRoundedImageView.class.getSimpleName();
public BetterRoundedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* Loads an image given by param url (this sets a default placeholder)
*
* #param url The url of the desired image to be loaded
*/
public void loadImage(final String url) {
loadImage(url, R.drawable.tracktics_logo);
}
/**
* Loads an image given by param url and a default placeholder while loading
*
* #param url The url of the desired image to be loaded
* #param placeholderResId the resource Id of the placeholder shown while loading image
*/
public void loadImage(final String url, #DrawableRes final int placeholderResId) {
Glide.with(getContext())
.asBitmap()
.load(url)
.circleCrop()
.placeholder(placeholderResId)
.into(BetterRoundedImageView.this);
}
}
This class is extending a RoundedImageView from
implementation 'com.makeramen:roundedimageview:2.3.0'
And it is used like this:
playerPhoto.loadImage(data.getProfilePictureUrl(), R.drawable.ic_default_profilepicture);
Can anyone help me out here?
Edit:
Maybe it has something to do how data is stored? In the Activity where I actually take the picture, the image is loaded correctly. But then when I go back to another view, the image is fetched from server (CDN) and cached locally to a Realm db.
Is there something that I miss when storing images on server by sending the base64 string to preserve exif information?
I resolved the issue by replacing the library:
https://github.com/MLSDev/RxImagePicker
by another Image Picker lib:
https://github.com/ArthurHub/Android-Image-Cropper
and now rotations are properly handled....
Seems like it was not a Glide issue after all!
I ran into the same issue, with this code, the image was rotated 90 degrees
Glide.with(activity).load(BitmapFactory.decodeFile(photoPath)).into(imgPicture)
Using this code it works as expected
Glide.with(activity).asBitmap().load(photoPath).into(imgPicture)
Not so much an answer, but maybe a hint to help you on your way.

Image Manager - convert a URI from Google Play Game Services to a Drawable

I am getting the URI from google with this method:
Player me = Games.Players.getCurrentPlayer(mGoogleApiClient);
Uri uri = me.getHiResImageUri();
I read in various posts that because of Google's security restrictions I need to use the ImageManager class.
This class only provides voids which directly set the URI as the drawable for the ImageView I provide. This is the method: loadImage(ImageView imageView, Uri uri);
The problem is that I need to get a variable of the Drawable class from this Uri and use that. Having the Uri set as the drawable of the image does not work in my specific case.
I don't know if there is a class similar to ImageManager or if there is a workaround to get the drawable.
Thanks for all replies in advance!
One approach is to use HttpURLConnection and call the URL, you'll then get the InputStream and use it to instantiate a BitMap by decoding it thru BitmapFactory.decodeStream. As indicated by #señor-reginold-francis in his answer, don't forget to add the "User-agent" property as it may be denied access if its absent (considering you're getting it from a google domain)
You still have to determine yourself what you want to do with the image.
final ImageView imgView = (ImageView) findViewById(R.id.imageView);
ImageManager.create(context)
.loadImage(new ImageManager.OnImageLoadedListener() {
#Override
public void onImageLoaded(Uri uri, Drawable drawable, boolean b) {
imgView.setImageDrawable(drawable);
}
}, participant.getHiResImageUri(), R.drawable.placeholder);

Android + Volley: how to get image bitmap using ImageLoader?

I'm trying to get images for list view using Volley Library. I created simple HTTP helper with the following method.
/**
* Processing Image request and gets the image with given URL
*/
public Bitmap makeImageRequest(String url) {
ImageLoader il = new ImageLoader(queue, new BitmapLruCache());
il.get(url, new ImageLoader.ImageListener() {
#Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
mBitmap = processImageResponse(response);
}
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(Constants.Global.ERROR, "Error: " + error.getMessage());
mBitmap = null;
}
});
return mBitmap;
}
But problem is that:
new BitmapLruCache()
Method is not recognized.
So i tried to create ImageLoader using the following code which i found on the URL:
http://www.androidhive.info/2014/05/android-working-with-volley-library-1/
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
But in thos code i cannot find out where to get
AppController
Because the method code is triggered from the custom
public class HttpHelperClass
And called from the activity using the:
// Try to load remote image from URL
Bitmap bm = http.makeImageRequest("http://camranger.com/wp-content/uploads/2014/10/Android-Icon.png");
ImageView iv = (ImageView) findViewById(R.id.imageView);
iv.setImageBitmap(bm);
Is it the right approach how to load images and how can i repair my code to make succcessfull request?
Many thanks for any advice.
I think your makeImageRequest method will always return null because it takes some time for the onResponse or onErrorResponse listeners to get called but you return the mBitmap immidately!
If you want to use Volley's ImageLoader you'd better get image inside your activity or ... not from another class like your HttpHelperClass.
Also AppController is a class that extends Application and you should create it yourself. (It's in your AndroidHive link. Section 3. Creating Volley Singleton class)
And also for caching images you should not create a new ImageLoader everytime because in this way the caching gets meaningless. You should get that from your AppController class.
Furthermore I suggest you using Picasso because it's way better that Volley in image loading and a lot easier!
With Picasso you only need to call the following line to load an image from web into an ImageView:
Picasso.with(context).load(urlString).to(imageView);

Parsing URI stored in local storage on device to ImageView

I'm creating the app where i have to display multiple URI's the user has chosen from their own images, currently i'm just trying to display one:
Current code:
(TouchImageView is just a library i'm used that works exactly like imageview)
TouchImageView img = (TouchImageView) findViewById(R.id.img);
img.setImageURI(Uri.parse("file://storage/emulated/0/DCIM/Camera/1411724483755.jpg"));
I have also tried parsing the URI to a bitmap but i can't get it to work.
You may not want to use setImageURI - as the documentation points out, this will decode the image on the UI thread, which is usually not a good idea.
Depending on the rest of the context, I'd suggest using an AsyncTask to load the file into a Bitmap on a separate thread, then after that is done, you can use ImageView.setImageBitmap on the UI thread.
So for example, something like this:
private class ImageLoadTask extends AsyncTask<Uri, Void, Bitmap> {
#Override
protected Bitmap doInBackground(Uri... uris) {
return BitmapFactory.decodeFile(uris[0].getPath());
}
#Override
protected void onPostExecute(Bitmap bmp) {
mImageView.setImageBitmap(bmp);
}
}
...
mImageView = (TouchImageView)findViewById(R.id.img);
new ImageLoadTask.execute(uri);
okay the answer was to parse it as a file and do a .toString()
This is the code that works:
TouchImageView img = (TouchImageView) findViewById(R.id.img);
img.setImageURI(Uri.parse((new File("file://storage/emulated/0/DCIM/Camer/1411724483755.jpg").toString())));

OutOfMemoryException when showing a listview with videos thumbnail

i need to pick multiple videos from gallery, and I've implemented a custom gallery starting from https://github.com/luminousman/MultipleImagePick .
When i'm loading thumbnails in the adapter, it gives me OutOfMemoryException after i starti the gallery Activity twice sequentially.
The code on adapter:
VideoThumbnailImageLoader thumb=
new VideoThumbnailImageLoader(
thumbPath,
MediaStore.Video.Thumbnails.MICRO_KIND);
holder.imgQueue.setImage(thumb,
R.drawable.no_media);
and the VideoThumbnailImageLoader code:
public class VideoThumbnailImageLoader implements SmartImage {
private String videoPath;
private int thumbnailKind;
public VideoThumbnailImageLoader(String videoPath, int thumbnailKind) {
this.videoPath=videoPath;
this.thumbnailKind=thumbnailKind;
}
#Override
public Bitmap getBitmap(Context ctxt) {
return ThumbnailUtils.createVideoThumbnail(videoPath, MediaStore.Images.Thumbnails.MICRO_KIND);
}
}
I'm using http://loopj.com/android-smart-image-view/ to load video thumbnail.
How can I avoid that?
Set android:largeHeap="true" in your activity proAndroidManifest.xml

Categories

Resources