How to reload NetworkImageView - android

I have an app which is displaying fragment with one NetworkImageView widget from android Volley.
The image at URL which is loaded in NetworkImageView is changing every 5 secounds so I want to uptade this image by pressing reload button that I have created in action bar.
I have found 2 problems:
If I call this code for loading Image again(it was already called when the fragment was created at onActivityCreated() method):
mNetworkImageView = (NetworkImageView) getView().findViewById(R.id.networkImageView);
mImageLoader = VolleySingleton.getInstance(getActivity()).getImageLoader();
mNetworkImageView.setImageUrl(IMAGE_URL, mImageLoader);
so nothing will happens(it is not the actual IMG of that link), I think its because it is now loading IMG from cache and not from internet.
If I call fragment reload() method:
public void reload(){
MainActivity activity = (MainActivity) getActivity();
IMAGE_URL = activity.getMyData();
mNetworkImageView = (NetworkImageView) getView().findViewById(R.id.networkImageView);
mImageLoader = VolleySingleton.getInstance(getActivity()).getImageLoader();
mNetworkImageView.setImageUrl(IMAGE_URL, mImageLoader);
from MainActivity:
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_reload) {
ViewFragment fragment = new ViewFragment();
fragment.reload();
}
return super.onOptionsItemSelected(item);
}
app get NullPointerException error after clicking action bar reload button.
the reload(); method is the place where I would like to reload NetworkImageView.
Can someone explain me pls how to correct this issues?

First get the cache from request queue.
Then remove your URL from the cache by calling remove function from the cache ( the key is your url).
then use your first approach:
mNetworkImageView = (NetworkImageView) getView().findViewById(R.id.networkImageView);
mImageLoader = VolleySingleton.getInstance(getActivity()).getImageLoader();
mNetworkImageView.setImageUrl(IMAGE_URL, mImageLoader);

This question and answers look old but those answers doesn't work on my project.
For your help, I write my solution here.
The problem is Volley's NetworkImageView caching the image as a map and there is no API for init the cache map.
Therefore my solution is make custom cache class which is includes removeCacheAll().
public class ImageCacheTrunk implements ImageLoader.ImageCache {
#Override
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}
#Override
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
public void removeCacheAll() {
mCache.evictAll();
}
}
and call removeCacheAll method before you need to reload.

Related

Android - ImageLoader won't re-retrieve the image from URL

This is the Activity page named PicURL. I want the app to retrieve the image from the URL whenever this Activity Page is called.
The problem is that this activity page will retrieve the image only once (the first time the activity is called.)
For instance, I open this activity I got picture "A" from the URL. Then I overwrite the picture "A" with picture"B". I reopen this activity but I still got picture "A" which it should be picture"B".
public class PicURL extends Activity {
ImageView imageView;
private ImageLoader imageLoader;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pic_url);
imageLoader = ImageLoader.getInstance();
imageView = (ImageView) findViewById(R.id.imageView);
DisplayImageOptions.Builder optionBuilder = new DisplayImageOptions.Builder();
optionBuilder.showImageForEmptyUri(R.drawable.ic_launcher);
optionBuilder.showImageOnFail(R.drawable.ic_launcher);
optionBuilder.cacheInMemory(true);
optionBuilder.cacheOnDisk(true);
DisplayImageOptions options = optionBuilder.build();
ImageLoaderConfiguration.Builder loaderBuilder =
new ImageLoaderConfiguration.Builder(getApplicationContext());
loaderBuilder.defaultDisplayImageOptions(options);
loaderBuilder.diskCacheExtraOptions(400, 400, null);
ImageLoaderConfiguration config = loaderBuilder.build();
if (!imageLoader.isInited()) {
ImageLoader.getInstance().init(config);
} // Checked if ImageLoader has been initialed or not.
String imageUri = "http://abcdef.com/pic1.png"; //Where the app retrieve the image from the link
imageLoader.displayImage(imageUri, imageView);
}
Apologize for my poor English.
Thanks for help!
Seem like you have ImageLoader's cache enabled:
optionBuilder.cacheInMemory(true);
optionBuilder.cacheOnDisk(true);
You need to set these options to false.
Disable caching will make your app re-load every images from the internet every time, which might be undesirable for you or your users.
You may enable caching, but generate a random query string to the URL that you really want to reload, e.g.,
String randomString = String.format("?random=%d", System.currentTimeMillis());
String imageUri = "http://abcdef.com/pic1.png"+randomString;
imageLoader.displayImage(imageUri, imageView);

Trying to load pictures from url using novoda ImageLoader

hi I am trying to load an image from an url using novoda Direct ImageLoader method
right now I have this class
public class MainActivity extends Activity {
public class DirectLoading extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv=(ImageView) findViewById(R.id.imageView1);
ImageView ivv=(ImageView) findViewById(R.id.imageView2);
Bitmap b=new DirectLoader()
.download("https://upload.wikimedia.org/wikipedia/commons/a/ad/SmallStellatedDodecahedron.jpg");
Bitmap pic=new DirectLoader()
.download("http://www.coetail.com/mamitakagi1129/wp-content/themes/twentyten/images/headers/cherryblossoms.jpg");
ivv.setImageBitmap(pic);
iv.setImageBitmap(b);
}
private void guiBuilder() {
}
}
}
I should get 2 images into the 2 imageViews, but I am getting a blank screen. There is a "Hello world" string in the layout that is not displayed so I guess I do get the images but they are not displayed graphically.
As it says in readme file https://github.com/novoda/ImageLoader/blob/develop/README.md you should handle threading when using DirectLoader.download(), for example, using AsyncTask.

how to retrieve image which stored in cloud

image stored in cloud "https://www1.copy.com/home/ganesh.jpg" on this link ..and i am not able to get image in application
public class AndroidLoadImageFromURLActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Loader image - will be shown before loading image
int loader = R.drawable.loader;
// Imageview to show
ImageView image = (ImageView) findViewById(R.id.image);
// Image url
String image_url = "https://www.copy.com/browse/copy/0017.JPG";
// ImageLoader class instance
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
// whenever you want to load an image from url
// call DisplayImage function
// url - image url to load
// loader - loader image, will be displayed before getting image
// image - ImageView
imgLoader.DisplayImage(image_url, loader, image);
}
}
Problem :
The link https://www1.copy.com/home/ganesh.jpg doenst return an image.
maybe you you need to login to the site and then you can get the image.
Solution :
Try ot get the srouce code of the ImageLoader jar and try to modify the URLConnection and pass the credentials to log in to the site.

How can I preserve images loaded before?

I've 3 fragments. Those fragments are placed into a TabsAdapter, for swipping between those fragments.
The problem, is that when the app loads, and the fragmentA view is created, it downloads the image and after it, changes a imageview:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.myFragmentView = inflater.inflate(R.layout.foto_setmana, container, false); //Això conté els "edittext i altres"
new DownloadImageTask(myFragmentView).execute("http://192.168.1.35/testing/fotos/foto1.jpg");
}
}
Code inside DownloadImageTask:
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
View myfragmentview;
public DownloadImageTask(View myfragmentview) {
this.myfragmentview=myfragmentview;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
Log.d("debugging","mIcon11"+mIcon11.getHeight());
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
ImageView imv=(ImageView)this.myfragmentview.findViewById(R.id.imageView1);
imv.setImageBitmap(result);
}
So this is what is happening:
1.- I click the app
2.- The app loads. This screen is taken before the app had time to retrieve the image.
3.- As it retrieve the image, its shown.
4.- I swipe the fragment to the next one.
5.- The second fragment
6.- I swipe again to the 3rd fragment.
7. The 3rd fragment.
8. I go back to the first fragment, and the image isnt loaded anymore. Obviously I wont call to DownloadImageTask again, because it would slow so much users experience.
What should I do to preserve the image?
Oh, and by the way. If I just swipe from 1st to 2nd, the image isnt "unloaded", it just happens if I go to 3rd or further. Any idea why is this happening? I'm just curious about this.
Thank you!
Use the LruCache to create a RAM cache of your bitmaps.
This great Google IO talk explains exactly how to use it:
http://youtu.be/gbQb1PVjfqM?t=5m
I found out a way that it's working:
What we've to do, is creating a variable on the fragment to know if we downloaded, and another to save the bitmap at all, something like:
int downloaded;
Bitmap pictureSaved;
Then we initialize it on "onAttach" method from fragment.
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.downloaded=0;
}
And after that, on the "main code":
if (this.downloaded==0){
//we're here if we didn't download yet
new DownloadImageTask(myFragmentView, this).execute("http://192.168.1.33/testing/fotos/foto1.jpg");
this.downloaded=1;
} else if(this.downloaded==1) {
//we're here if we already downloaded.
ImageView imv=(ImageView)myFragmentView.findViewById(R.id.imageView1);
imv.setImageBitmap(this.pictureSaved);
}
DownloadImageTask code, should download the image, and as you can see, I pass to his constructor "this", so I can access the fragment methods and variables.
I create a method into the fragment to update the picture:
public void update(Bitmap updated){
ImageView imv=(ImageView)myFragmentView.findViewById(R.id.imageView1);
this.pictureSaved=updated;
imv.setImageBitmap(this.pictureSaved);
this.downloaded=1;
}
DownloadImageTask calls fragment's method like this.
Variables:
MyFragment frag;
Constructor:
public DownloadImageTask(View myfragmentview, MyFragmentA parent) {
this.myfragmentview=myfragmentview;
this.frag=parent;
}
PostExecute:
protected void onPostExecute(Bitmap result) {
frag.update(result);
}
It works perfectly.
Hope this helps someone.

Display screenshot image from one activity to another activity

I am developing playing video application and taking screenshot of running video and display a screenshot in next activity, i am playing video and taking screenshot and i am not able to display screenshot in next activity please check my code and give me changes.
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
image = (ImageView) findViewById(R.id.ImageView01);
// image.setBackgroundDrawable(bitmapDrawable);
String bitmap = image.toString();
System.out.println("Image getting++++++ : " + bitmap);
Intent intent = new Intent(VideoDemo.this, ScreenshotView.class);
intent.putExtra("BitmapImage", bitmap);
startActivity(intent);
public class ScreenshotView extends Activity
{ private String filename;
private ImageButton back;
private ImageView screenshot;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.screenshot);
screenshot =(ImageView)findViewById(R.id.screen);
back = (ImageButton)findViewById(R.id.backbutton);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
finish();
}
});
System.gc();
Intent i = getIntent();
Bitmap bitmap = (Bitmap) i.getParcelableExtra("BitmapImage");
screenshot.setImageBitmap(bitmap);
}
}
Here your "bitmap" object is a string.
And you are passing a string object to your next activity.
That is why, you are not able to set image in you ImageView screenshot.
Can you try the below code and lemme know whether you fixed it.
Sending Object
Here is the code to send the Object from one to other class. One Important thing to send the Object is the class should implement the Serializable class.
The below Red Colored text should be same.
//MainActivity.java
Intent i = new Intent(MainActivity.this,startActivity.class);
ObjectClassName object = new ObjectClassName();
i.putExtra("THIS", Object);
Receiving Object
// startActivity.java
Intent i = getIntent();
ObjectClassName obj = (ObjectClassName) getIntent().getSerializableExtra("THIS");//
TypeCasting needed

Categories

Resources