How to improve Image resolution - android

String urlThumnail = currentNewsItem.getUrlThumbnail();
loadImages(urlThumnail, newsHolder);
break;
case 2:
break;
default:
break;
}
}
private void loadImages(String urlThumbnail, final NewsViewHolder holder) {
if (urlThumbnail != null) {
imageLoader.get(urlThumbnail, new ImageLoader.ImageListener() {
#Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
holder.thumbnail.setImageBitmap(response.getBitmap());
}
#Override
public void onErrorResponse(VolleyError error) {
}
});
}
}
xml
<ImageView
android:id="#+id/DetailsNewsImage_cardView"
android:layout_below="#+id/DetailNews_title"
android:layout_width="match_parent"
android:layout_height="195dp"
android:scaleType="centerCrop">
I want to improve the resolution of the image, I am receiving image through JSon, then I have enlarged its size in Xml after loading it. The image that I am receiving after all this is blurred. I wish to improve the image resolution just like the original image. How do I improve image resolution?

Related

how to show prograss bar before loading the image in Picasso. and then if image is loaded then start stories progressview in android

I am working on the project to create the stories like whatsapp or instagram.
we are using the library from Github
Our image are loaded from firebase.
but the problem is that stories start with loading the image because image size is large.
#Override
public void onFirebaseLoadSuccess(final List<Movie> movieList) {
storiesProgressView.setStoriesCount(movieList.size());
storiesProgressView.setStoryDuration(1500L);
Picasso.get().load(movieList.get(counter).getImage()).into(imageView, new Callback() {
#Override
public void onSuccess() {
progressBar.setVisibility(View.GONE);
storiesProgressView.startStories();
}
#Override
public void onError(Exception e) {
}
});
storiesProgressView.setStoriesListener(new StoriesProgressView.StoriesListener() {
#Override
public void onNext() {
if(counter < movieList.size()){
counter++;
Picasso.get().load(movieList.get(counter).getImage()).into(imageView);
}
}
#Override
public void onPrev() {
if(counter > 0){
counter--;
Picasso.get().load(movieList.get(counter).getImage()).into(imageView);
}
}
#Override
public void onComplete() {
counter = 0;
Toast.makeText(MainActivity.this,"Completed!",Toast.LENGTH_LONG).show();
finish();
}
});
}
Picasso doesn't has a function to check when load started so i highly recommend you to use Glide instead of Picasso.
Here, you can show progress bar when image loading is started via onLoadStarted() function :
Glide.with(this).load(movieList.get(counter).getImage()).into(new Target<GlideDrawable>() {
#Override
public void onStart() {
}
#Override
public void onStop() {
}
#Override
public void onDestroy() {
}
#Override
public void onLoadStarted(Drawable placeholder) {
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
}
#Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
progressBar.setVisibility(View.GONE);
imageView.setImageDrawable(resource);
storiesProgressView.startStories();
}
#Override
public void onLoadCleared(Drawable placeholder) {
}
#Override
public void getSize(SizeReadyCallback cb) {
}
#Override
public void setRequest(Request request) {
}
#Override
public Request getRequest() {
return null;
}
});

TouchImageView Is not Showing Image After Downloading form Volley

<com.example.util.TouchImageView
android:id="#+id/imageViewRc"
android:layout_width="match_parent"
android:layout_height="450dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="2dp"
android:layout_marginRight="3dp"
android:src="#drawable/placeholder_background"
android:scaleType="fitXY" />
Below is the code where I am wiring this up:
private void setImageWithPicaso(String imageUrl) {
if (!(imageUrl == null)) {
Picasso.with(getActivity()).load(imageUrl).into(imageView, new Callback() {
#Override
public void onSuccess() {
spinner.setVisibility(View.GONE);
}
#Override
public void onError() {
spinner.setVisibility(View.GONE);
AppLog.showToastMessage(getActivity(), "Rate Card loading failed!");
}
});
} else {
spinner.setVisibility(View.GONE);
AppLog.showToastMessage(getActivity(), "Rate Card loading failed!");
}
}
The download is happening , and when I start pinching the zoom it works
Appreciate your time.
Picasso.with(getActivity()).load(urlToLoad).placeholder(R.drawable.placeholder_background).into(imageView, new Callback()
modify with above code and put some pic rename it placeholder_background and put it in drawables

Glide assert: java.lang.IllegalArgumentException: You must call this method on the main thread

Has anyone used Glide to fetch images from a background thread? I keep getting this assert:
java.lang.IllegalArgumentException: You must call this method on the main thread
but according to this thread, it should work:
https://github.com/bumptech/glide/issues/310
Yet, I cannot get it to work, unless I call it from the main thread.
Here's is what I am trying to do from the main thread:
Glide.get(mContext);
loadUserImage(userImageUrl);
// wait 5 seconds before trying again
int imageLoadingTimeOut = mContext.getResources().getInteger(R.integer.image_loading_time_out);
if (imageLoadingTimeOut > 0) {
new Timer().schedule(new TimerTask() {
#Override
public void run() {
if (!mUserImageLoaded) {
loadUserImage(userImageUrl);
}
}
}, imageLoadingTimeOut);
}
}
and the loadUserImage:
private boolean mUserImageLoaded = false;
private void loadUserImage(String userImageUrl) {
if (userImageUrl != null && !userImageUrl.isEmpty() && !mUserImageLoaded) {
Glide.with(mContext).using(Cloudinary.getUrlLoader(mContext)).load(userImageUrl).crossFade().listener(new RequestListener<String, GlideDrawable>() {
#Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
#Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
mImageMessageContent.invalidate();
mUserImageLoaded = true;
return false;
}
}).into(mImageMessageContent);
} else {
mImageMessageContent.setVisibility(View.GONE);
}
}
and mContext is just the activity "this" pointer.
Anyway, can I use Glide from a thread different than main?
Update image in main ui thread
runOnUiThread(new Runnable() {
#Override
public void run() {
Glide.with(MainActivity.this)
.load("image URL")
.into(imageView);
}
});
The into(ImageView) method of Glide requires you to call it only on main thread, but when you pass the loading to a Timer it will be executed in a background thread.
What you can do is to retrieve a bitmap by calling get() instead of into() and then set that bitmap on the ImageView by calling setImageBitmap().
Glide.with(getApplicationContext())
.load("your url")
.asBitmap()
.into(new BitmapImageViewTarget(imgView) {
#Override
protected void setResource(Bitmap resource) {
//Play with bitmap
super.setResource(resource);
}
});
You can also take a look at this document for more information.
Posting the code just in case it helps someone.
Bitmap myBitmap = Glide.with(applicationContext)
.load(yourUrl)
.asBitmap()
.centerCrop()
.into(Target.SIZE_ORIGINAL,Target.SIZE_ORIGINAL)
.get()
imageView.setImageBitmap(myBitmap);
Here is the Kotlin-way solution
Glide.with(context).asBitmap().load(photo_url).into(
BitmapImageViewTarget(imgYourResourceID)
)
In my case i want to show notification from FirebaseMessagingService with image which will be downloaded via Glide, what gave me success is this piece of code:
try {
Bitmap bitmap= Glide.with(getApplicationContext())
.load(imageUrl) // image url in string
.asBitmap().into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).get();
// now i can pass bitmap to notificationBuilder like
notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap));
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
I need to decrypt and display large images in a recyclerView
And because it was a heavy process, I had to do it in the background, so that's why :
public void decryptImage(Uri uri, ViewHolder holder) {
Runnable runnable = new Runnable() {
#Override
public void run() {
try {
Glide.with(G.context)
.asBitmap()
.load(decrypt(G.getEncryptionKey(), G.getConstantKey(), uri.getPath()))
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
Glide.with(G.context).load(resource).into(holder.mImageView);
}
});
}
};
new Thread(runnable).start();
}
private class AsyncTaskRunner extends AsyncTask<String, String, RequestBuilder>
{
ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(MainActivity.this,
"Please Wait",
"Image is loading...");
}
#Override
protected RequestBuilder<Drawable> doInBackground(String... strings) {
URL url = null;
try {
url = new URL(strings[0]);
} catch (MalformedURLException e) {
e.printStackTrace();
}
RequestBuilder<Drawable> g= Glide.with(MainActivity.this).load(url);
return g;
}
#Override
protected void onPostExecute(RequestBuilder v) {
v.into(imageView);
imageView.setVisibility(View.VISIBLE);
progressDialog.dismiss();
}
}

Cannot load Image from url using picasso

I use piccaso 2.5.2 and I have okhttp 2.4.0 and okhttp-urlconnection 2.4.0 in my gradle. The ImageView is a part custom list item layout. The error is that nothing appears in the ImageView. It occurs only for the first item of the list. That list is used to populate in SwipeFlingAdapterView which is similar to ListView. SwipeFlingAdapterView is from this library https://github.com/Diolor/Swipecards
Ok, here are my codes.
Picasso.with(mActivity)
.load(url)
.resize(540, 720)
.centerCrop()
.into(vh.iv_profile);
ImageView in xml file.
<ImageView
android:id="#+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#android:color/white"/>
The above code doesn't work so I try to log the error with this.
Picasso picasso = new Picasso.Builder(mActivity).listener(new Picasso.Listener() {
#Override public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
exception.printStackTrace();
Log.e("Picasso","description",exception);
}
}).build();
picasso.load(url)
.resize(540, 720)
.centerCrop()
.into(vh.iv_profile);
It doesn't show error. It doesn't even get into onImageLoadFailed. So, I try other way around.
picasso.load(url)
.resize(540, 720)
.centerCrop()
.into(vh.iv_profile, new Callback() {
#Override
public void onSuccess() {
if(vh.iv_profile.getHeight() == 0){
vh.iv_profile.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.img_default));
Toast.makeText(mActivity, "we get here", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onError() {
}
});
OnSuccess in callback, I can't even set Image from drawable anymore. If I set Drawable before using picasso.load, I can set it successfully. I also tried to use it without .centerCrop(). It doesn't work.
here is the final code.
try {
Picasso.with(mActivity).load(url).into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.i("picasso error", "bitmap loaded");
if (bitmap != null) {
vh.iv_profile.setImageBitmap(bitmap);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Log.i("picasso error", width + ", " + height);
}
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
}catch(Exception e){
Log.i("picasso error", "exception");
}
onBitmapLoaded, I get a bitmap with width and height of 243 but it doesn't appear in the ImageView.
Please help!

NetworkImageView with InfoWindowAdapter

I'm having trouble trying to use Volley's NetworkImageView with an InfoWindow of Google Maps API v2.
My markers shows an InfoWindow with an image fetched from the internet. As you know, the only way to do this is to show the InfoWindow and refresh it when finished downloading the image.
So I need a way to know when the NetworkImageView have finished downloading the image so I can refresh that view.
I'm looking for something like onLoadComplete
Any ideas?
Modify NetworkImageView
public void setImageUrl(String url, ImageLoader imageLoader, ImageListener myListener) {
mUrl = url;
mImageLoader = imageLoader;
// The URL has potentially changed. See if we need to load it.
loadImageIfNecessary(false, myListener);
}
/** #param myListener notified when image has loaded or on error */
private void loadImageIfNecessary(final boolean isInLayoutPass, final ImageListener myListener) {
...
ImageContainer newContainer = mImageLoader.get(mUrl,
new ImageListener() {
#Override
public void onErrorResponse(VolleyError error) {
if (myListener != null) {
myListener.onErrorResponse(error);
}
if (mErrorImageId != 0) {
setImageResource(mErrorImageId);
}
}
...
#Override
public void onResponse(final ImageContainer response, boolean isImmediate) {
...
if (response.getBitmap() != null) {
if (myListener != null) {
myListener.onResponse(response, false); // pay attention to boolean isImmediate param
}
setImageBitmap(response.getBitmap());
...
}
}
}
...
Now you can use it in your Adapter or any other code:
public void bind(Item item) {
imageView.setImageUrl(imageUrl, app.getImageLoader(), new ImageListener() {
#Override
public void onResponse(ImageContainer response, boolean isImmediate) {
// isImmediate == true - taken from cache
// isImmediate == false - successfully loaded from server
}
#Override
public void onErrorResponse(VolleyError error) { }
});
}
I created a a custom NetworkImageView as oleksandr_yefremow suggested and got it working although the cache is not working by some reason.
public View getInfoWindow(final Marker marker) {
final CustomImageView imageView = ...
imageView.setImageUrl(imageUrl, app.getImageLoader(), new ImageListener() {
#Override
public void onResponse(ImageContainer response, boolean isImmediate) {
imageView.setImageBitmap(response.getBitmap());
marker.showInfoWindow();
// isImmediate == true - taken from cache
// isImmediate == false - successfully loaded from server
}
#Override
public void onErrorResponse(VolleyError error) { }
});
}

Categories

Resources