I am making a Widget and in the Widget service in the getViewAt method i have set corrently the text to my widget but it is complicated to pass my images from my object.
#Override
public RemoteViews getViewAt(int position) {
RemoteViews views = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
Automoviles autoService=mArraylistAutomoviles.get(position);
String imagen=autoService.getImagen();
String marca =autoService.getMarca();
// int imagenInt= Integer.parseInt(imagen);
// Update the plant image
views.setImageViewResource(R.id.widget_image_item, R.drawable.mazda2azul);
views.setTextViewText(R.id.textView,marca);
return views;
}
Notice that in order to retreive the images i need to use Glide(Thats what i use in my project) .At the moment i have R.drawable.mazda2azul that is an int so the method doesnt complain .String imagen=autoService.getImagen return the following Url = https://firebasestorage.googleapis.com/v0/b/udacitycapstonefinal.appspot.com/o/CarImages%2Fford_escape_negra.jpeg?alt=media&token=89ea95d3-852c-4d24-9f09-b9ef519928a3
I tried to do
int imagenInt= Integer.parseInt(imagen);
but it didnt load the widget images or Text .
So i need a way to do the Glide part and pass it to the
setImageResource second parameter. This is my big problem.
You can use this method:
Bitmap bitmap = Glide.
with(context).
load(imagen).
asBitmap().get();
views.setImageViewBitmap(R.id.widget_image_item, bitmap);
Related
Hi i have some images displayed in gridView using Picasso, when i try to open each image (after onItemClickListener) in full screen mode (inside new acivity) it display only the first image in my gridView whatever the image i click, this is my problem.
This is my code where i display the images from the url inside gridView and OnItemClickListener.
Picasso.with(ToolDescription.this)
.load(SaveSettings.ServerURL +"Images/"+ imagepath)
.into(imageView);
ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(ToolDescription.this, ImageFull.class);
intent.putExtra("imagepath", SaveSettings.ServerURL +"Images/"+ imagepath);
startActivity(intent);
}
});
return myView;
This is my second activity (named ImageFull) where i want to display the clicked image:
ImageView imageView = (ImageView) findViewById(R.id.fullImage);
String imagepath = getIntent().getStringExtra("imagepath");
Picasso.with(ImageFull.this)
.load(imagepath)
.noFade()
.resize(800, 800)
.centerCrop()
.into(imageView);
Either set an individual click listener to each imageview or use the int position to pull the correct imagepath from your gridview item array.
Grid View Android Doc
This link has a easy to follow code for exactly what you're trying to do.. Use the position to find which element you are clicking on an get the respective image url
Looks like what's happening is that you're calling ls.setOnItemClickListener() every time getView() returns.
AdapterView.OnItemClickListener is a single listener for the entire AdapterView, and you're intended to differentiate which item was clicked on by using its position argument.
I'm not sure where imagepath is coming from inside getView(), but I assume it's something like String imagepath = getItem(position).getImagePath(). If that is the case, you can fix your problem by:
1) Move ls.setOnItemClickListener() out of getView()... probably to your onCreate() or whever you set up your GridView.
2) Change
intent.putExtra("imagepath", SaveSettings.ServerURL +"Images/"+ imagepath);
to
String currentImagePath = getItem(position).getImagePath();
intent.putExtra("imagepath", SaveSettings.ServerURL +"Images/"+ currentImagePath);
Probably there will be syntax errors because I can't see the rest of your code, but hopefully this is enough for you to be able to work through them.
I have viewflipper which contains 3 childs, one GridView and two custom ListView, every view has different adapter , and every adapter has a image loader with Universal Image Loader library.
The items are the same for all adapters, my goal is to show content in different way (grid , list , and big list), but in this way every image loads 3 times. Is there any way to load images once and show them to their childs?
So you need an object common to the three views, which manages image loading and holds memory. You have it: it's the Adapter. Use a single one and just switch layouts.
For instance, you could define this method inside the adapter:
int layoutResId;
public void changeLayout(int layoutResId) {
this.layoutResId = layoutResId;
notifyDataSetChanged(); //force the adapter to call getView() again
}
Then in your getView() method you just inflate the layout defined by layoutResId.
Well the thing that you seem to be asking is if you can pass the images along to the other adapters. It would be something like if image is not empty them use it in the other views, if not then use the image loader.
public class Constants {
public static Constants INSTANCE = new Constants();
public Constants() {
}
public Uri IMAGE_PATH = Uri.EMPTY;
public File IMAGE_FILE = null;
public Bitmap IMAGE = null;
}
Presumably you would choose only one of these kinds to save from your view. And then check them to see if constants possesses an image for you to display.
I am currently trying to make the scrolling of my GridView smoother by moving some work from the UI-thread into other threads, but I get strange behavior: sometimes one image is drawn to more then one view. I am using Xamarin (C#).
Below you can find the simplyfied version of my current code. LoadShowImageAsync is called from within the GetView() in my gridview Adapter. If I call LoadShowImageDoWork() singlethreaded everything is fine but when I call it via the ThreadPool it shows the strange behaviour.
In the debuglogs I see that the method is left after decoding the image and the RunOnUiThread stuff is executed combined for several images later. Then some other Images gets decoded and then RunOnUiThread stuff is done combined for them. I guess that somehow the bitmap content gets mixed up here.
Has anyone an idea what I can do to make it work multithreaded?
public class LoadImageAsyncDatas {
public int ImageId { get; private set;}
public ImageView ImageView { get; private set;}
public LoadImageAsyncDatas (ImageView imageView,int imageId) {
ImageId = imageId;
ImageView = imageView;
}
}
public class LoadImageAsync {
private object locker;
Activity activity;
public LoadImageAsync (Activity activity) {
this.activity = activity;
locker = new object ();
}
public void LoadShowImageAsync (ImageView imageView,int imageId) {
object stateInfo = new LoadImageAsyncDatas (imageView, imageId);
// call LoadShowImageDoWork() either multithreaded (strange behaviour) or single threaded (works)
ThreadPool.QueueUserWorkItem (new WaitCallback (LoadShowImageDoWork), stateInfo);
// LoadShowImageDoWork (stateInfo);
}
private void LoadShowImageDoWork (object stateInfo) {
lock (locker) { // lock is only for debugging
LoadImageAsyncDatas imageData = stateInfo as LoadImageAsyncDatas;
byte[] imageBytes = LoadFileByImageId(imageData.ImageId);
Bitmap bitmap = BitmapFactory.DecodeByteArray (imageBytes, 0, imageBytes.Length, myBitmapOptions);
activity.RunOnUiThread(() => {
imageData.ImageView.SetImageBitmap(bitmap);
});
}
}
}
Thanks!
Jens
If you want each bitmap in an Android list control to be unique then you must manage unique bitmaps for each visible "cell" on the display. If you try to re-use a bitmap that is currently being used to display another cell, you'll change both of them. For example, if you use a single bitmap to handle all of the GetView() requests of the list, each time you update the bitmap, all of the cells in the list which were set to that bitmap object will display the new bitmap. In my applications I manage this situation by setting a unique ID value (the list index) into the ImageView object. When a GetView() request is sent, I can then tell if the bitmap has been re-used and now needs to display new information. It also allows me to know when I need to create a new bitmap versus re-use an existing one.
Use these two functions to manage the IDs of your bitmaps set into your ImageViews:
ImageView.setTag()
ImageView.getTag()
i am creating a gridview activity which downloads the images asynchronously as in the example in android developers. When some images aren't downloaded correctly, because of slow internet connection, i have one refresh button that downloads only these images and not all from the beginning. The problem is how can i bind these images to their unique image-view in grid-view?
Override the getItem(int) method of your adapter, and use it to get the ImageView.
public Object getItem(int position) {
return myCollection[position];
}
public void update(Drawable draw, int position) {
ImageView view = (ImageView) mAdapter.getItem(position);
view.setImageDrawable(draw);
mAdapter.notifyDataSetChanged();
}
I have used both Gallery and ViewPager widgets, I am fetching images from web service,
but I couldn't place them in the ViewPager, but in Gallery it is easy.
I used this tutorial for ViewPager
http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/
I want android Gallery widget scroll like the ViewPager scroll,
is this possible, how can I do this.
Here is a bare bones example of the instantiateItem() method for your PagerAdapter that would fill it with image views dynamically.
#Override
public Object instantiateItem( final View pager, final int position )
{
//Note: if you do not have a local reference to the context make one and
//set it to the context that gets passed in to the constructor.
//Another option might be to use pager.getContext() which is how its
//done in the tutorial that you linked.
ImageView mImg = new ImageView(context);
/*Code to dynamically set your image goes here.
Exactly what it will be is going to depend on
how your images are stored.
In this example it would be if the images
are on the SD card and have filenames
with incrementing numbers like: (img0.png, img1.png, img2.png etc...)*/
Bitmap mBitmap = BitmapFactory.decodeFile(
Environment.getExternalStorageDirectory() + "/img" + position + ".png");
mImg.setImageBitmap(mBitmap);
((ViewPager) collection).addView(mImg, 0);
return mImg;
}