<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
Related
I am trying to get a list of images from a folder that I created in my FirebaseStorage bucket. There are total of 20 images and while the app gets all the images, I want to show a ProgressBar. Problem is I don't even see the progressbar before it gets dismissed. I believe this is something related to asynchronous calls that FirebaseStorage uses and I tried to solve it by using AsyncTask but no luck.
private void getBigFlagsFromFirebase() {
bigFlagsList = new ArrayList<>();
progressBar.setVisibility(View.VISIBLE);
if (Utils.isInternetConnectionAvailable(this)) {
FirebaseStorage storage = FirebaseStorage.getInstance("gs://big-quiz-adrien.appspot.com/");
StorageReference storageReference = storage.getReference().child("level1_data/big_flags");
storageReference.listAll().addOnSuccessListener(new OnSuccessListener<ListResult>() {
#Override
public void onSuccess(final ListResult listResult) {
final AtomicInteger filesCount = new AtomicInteger(listResult.getItems().size());
for (final StorageReference file : listResult.getItems()) {
file.getDownloadUrl()
.addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
bigFlagsList.add(uri);
if (filesCount.decrementAndGet() == 0) {
progressBar.setVisibility(View.GONE);
Toast.makeText(QuestionActivity.this, "Size: " + bigFlagsList.size(), Toast.LENGTH_SHORT).show(); //this gives me 20
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
if (filesCount.decrementAndGet() == 0) {
progressBar.setVisibility(View.GONE);
Toast.makeText(QuestionActivity.this, "Something went wrong. Please try again later.", Toast.LENGTH_SHORT).show();
}
}
});
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(QuestionActivity.this, "Something went wrong. Please try again later.", Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(this, "Please check your internet connection and try again later.", Toast.LENGTH_LONG).show();
}
Toast.makeText(this, "Size: " + bigFlagsList.size(), Toast.LENGTH_SHORT).show(); //this gives me 0
}
This is my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f7f7f7"
tools:context=".QuestionsActivity">
<ProgressBar
android:id="#+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/ads">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/themeName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#drawable/less_border_radius"
android:backgroundTint="#00B050"
android:gravity="center_horizontal"
android:padding="15dp"
android:text="#string/level_1"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/flagQuestionsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
tools:listitem="#layout/country_questions_item" />
</LinearLayout>
</ScrollView>
<TextView
android:id="#+id/ads"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#fff"
android:gravity="center_horizontal"
android:padding="15dp"
android:text="Ads Banner"
android:textSize="18sp" />
</RelativeLayout>
So your code seems to be doing the following:
Showing the progress bar.
Adding a success and failure listener for loading your data.
Dismissing the progress bar.
Loading the data is asynchronous, so until the loading succeeds or fails, you want the progress bar to stay displayed, but you're immediately dismissing it after registering the listener. So progressBar.setVisibility(View.GONE) should be inside the onSuccessListener and onFailureListener instead.
[Edit]
The reason your progress bar is dismissed after the first image is loaded is because you aren't waiting for all the images to load. For each result, you're registering a listener, but you dismiss the progress bar right afterwards. You should wait for all the file.getDownloadUri() calls to complete (either by succeeding or failing).
// I'm not sure if these listeners run concurrently and on which threads, so to be safe I'm using an AtomicInteger, feel free to change it to an int if it's thread safe.
final AtomicInteger filesCount = new AtomicInteger(listResult.getItems().size());
for (StorageReference file : listResult.getItems()) {
file.getDownloadUrl()
.addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
// Do something with the Uri
if (filesCount.decrementAndGet() == 0) {
progressBar.setVisibility(View.GONE);
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
if (filesCount.decrementAndGet() == 0) {
progressBar.setVisibility(View.GONE);
}
}
});
}
You need to do that with FrameLayout as it help you have the progressbar behind the recyclerview so once your image got loaded, the recyclerview will hide
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
android:id="#+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/flagQuestionsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
tools:listitem="#layout/country_questions_item" />
</FrameLayout>
remove progressBar.setVisibility(View.GONE); line from bottom of funcion, and place when you got success/fail callback:
private void getFilesFromFirebase() {
progressBar.setVisibility(View.VISIBLE);
FirebaseStorage storage = FirebaseStorage.getInstance("gs://big-quiz-adrien.appspot.com/");
StorageReference storageReference = storage.getReference().child("level1_data/small_flags");
storageReference.listAll().addOnSuccessListener(new OnSuccessListener<ListResult>() {
#Override
public void onSuccess(final ListResult listResult) {
progressBar.setVisibility(View.GONE);
for (StorageReference file : listResult.getItems()) {
file.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
smallFlagsList.add(uri);
Toast.makeText(QuestionsActivity.this, "Size: " + smallFlagsList.size(), Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
}
});
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
progressBar.setVisibility(View.GONE);
}
});
}
and also you should maintain the sequence of view based on which view will render on top and which are behind.
Last view will be on top of all view, if no view contain elevation.
Am trying to load a file as bitmap into TouchImageview. If I use normal image view instead of Touch image view Glide library is able to load image into it from file object but in case on touch image view Glide unable to load image.
Used following code as well:
Glide.with(this).asBitmap().load(file).into(new SimpleTarget<Bitmap>(250, 250) {
#Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
touchImageView.setImageBitmap(resource);
}
#Override
public void onLoadFailed(#Nullable Drawable errorDrawable) {
super.onLoadFailed(errorDrawable);
}
});
But OnLoadFailed() is called with errorDrawable as null.
Ok I just found about xml. please take note that, you HAVE TO USE RELATIVE LAYOUT!
<RelativeLayout
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<my.zuhrain.kit.TouchImageView
android:layout_marginTop="20dp"
android:id="#+id/image_view_main"
android:layout_width="400dp"
android:layout_height="400dp" />
</RelativeLayout>
and here we go
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
//things need to know
//USE RELATIVE LAYOUT!
Glide.with(getApplicationContext())
.asBitmap()
.load(uri)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
touchImageView.setImageBitmap(resource);
}
});
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
});
I wonder if I'm doing anything wrong. My code is pretty straight forward and would like an example of how you guys made it work if you have a progress bar to load an image in any of your apps, thanks! I'm using volley's imageloader and the progress bar never ever appears
holder.mProgressBar.setVisibility(View.VISIBLE);
if (video.thumbnail != null) {
mImageLoader.get(video.thumbnail, new ImageLoader.ImageListener() {
#Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
holder.mProgressBar.setVisibility(View.GONE);
holder.mScreenShot.setImageBitmap(response.getBitmap());
}
#Override
public void onErrorResponse(VolleyError error) {
holder.mProgressBar.setVisibility(View.GONE);
}
});
}
My XML
<RelativeLayout
android:id="#+id/image_load"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:background="#android:color/darker_gray"
android:id="#+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="#dimen/album_cover_height"
android:clickable="true"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:scaleType="fitXY" />
<ProgressBar
android:visibility="gone"
android:id="#+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
Definitely a timing issue. I replicated your scenario as best as I could.
public class MainActivity extends AppCompatActivity {
ImageView image;
ProgressBar pb;
public String url;
public ImageLoader imageLoader;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
pb = (ProgressBar) findViewById(R.id.pb);
RequestQueue queue = Volley.newRequestQueue(this);
url = "http://www.hrwiki.org/w/images/thumb/d/d5/currentbad.png/180px-currentbad.png";
imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(20);
#Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
#Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
show();
new testTask().execute(imageLoader);
}
public void show() {
image.setVisibility(View.INVISIBLE);
pb.setVisibility(View.VISIBLE);
}
public void hide() {
image.setVisibility(View.VISIBLE);
pb.setVisibility(View.INVISIBLE);
}
private class testTask extends AsyncTask<ImageLoader, Void, Void> {
#Override
protected Void doInBackground(ImageLoader... params) {
SystemClock.sleep(5000);
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
imageLoader.get(url, new ImageLoader.ImageListener() {
#Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
hide();
image.setImageBitmap(response.getBitmap());
}
#Override
public void onErrorResponse(VolleyError error) {
}
});
}
}
}
With this layout file:
<FrameLayout
android:layout_width="100dp"
android:layout_height="100dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:id="#+id/image" />
<ProgressBar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:id="#+id/pb"/>
</FrameLayout>
I saw the same behavior until I put the aSync task in which basically waits for 5 seconds before loading the image. This definitely shows that OnResponse is getting called so quickly that you aren't seeing the progress bar.
Looks like Michelle's answer has what you would look for to implement some pre-check logic.
Refer to this previous question for a detailed explanation for how onResponse(ImageLoader.ImageContainer response, boolean isImmediate) is working.
Volley, How many times the onResponse in ImageLoader.ImageListener called
I believe it is basically just immediately turning off your progress bar. Haven't done this particular thing before but based on the other question I think you want to add a check for:
if(!isImmediate)
holder.mProgressBar.setVisibility(View.GONE);
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?
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!