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

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);

Related

Assign image resource in an image view with string

(1) I am using recyleview to fetch image, text & audio file from API. For text I used volley & for image use picasso, what I will use for audio file.
(2) after fetch the image & text when apply onclicklistener pass the info to another activity. Can pass only text not images.
What I need to do to solve this problem?
this is adapter part
viewHolder.textViewStory.setText(banglaItem.getStoryName());
viewHolder.textViewWritter.setText(banglaItem.getWritterName());
Picasso.get().load(banglaItem.getStoryImage()).fit().into(viewHolder.imageView);
viewHolder.linear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,AudioActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("storyName",banglaItem.getStoryName());
intent.putExtra("storyImage",banglaItem.getStoryImage());
context.startActivity(intent);
}
});
this is the new activity part
public class AudioActivity extends AppCompatActivity {
TextView name;
ImageView image;
String nameStory;
String imageStory;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
name = findViewById(R.id.audioName);
image = findViewById(R.id.audioImage);
nameStory = getIntent().getStringExtra("storyName");
imageStory = getIntent().getStringExtra("storyImage");
name.setText(nameStory);
int resourceId = getResources().getIdentifier(imageStory,"drawable", getPackageName());
image.setImageResource(resourceId);
}
}
(1.) if you have complete url of your audio file then you can directly stream audio file using media player classes provided by android.
(2.) you are passing storyImage in next activity ,which is a url that you get from api. so you have to find same on next activity i.e
TextView name;
ImageView image;
String nameStory;
String imageStory;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
name = findViewById(R.id.audioName);
image = findViewById(R.id.audioImage);
nameStory = getIntent().getStringExtra("storyName");
imageStory = getIntent().getStringExtra("storyImage");
name.setText(nameStory);
Picasso.get().load(imageStory).fit().into(image);
//here image story contain url that you sent through first activity. As picasso uses cache, it will load (already loaded image in first activity) very smoothly from it cache.
}

Android remove Bitmap from Memory when leave activity

I am developing an app that needs a image gallery. i am at the point where i have all the images displaying in a grid view. when i touch an image i want it to open an activity with the touched image full screen, which is also working but when i go back to the grid and open another image the memory consumption keeps growing. i have tried setting the feature image to null, Bitmap.recycle() and calling finish() on the activity none of which seem to stop the memory consumption increasing when i open a different image.
Open Detail Activity
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
intent.putExtra("image", adapter.file[position].getPath());
//Start details activity
startActivity(intent);
Feature Image Activity
public class DetailActivity extends AppCompatActivity {
private Intent intent = getIntent();
private Bitmap featureImage;
private ImageView featureView;
private final String TAG ="Image Detail --";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Bundle extras = getIntent().getExtras();
final String imgPath = extras.getString("image");
final BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inDensity = 2;
bitmapOptions.inTargetDensity = 1;
featureImage = BitmapFactory.decodeFile(imgPath, bitmapOptions );
featureView = (ImageView) findViewById(R.id.featureImageView);
featureView.setImageBitmap(featureImage);
}
#Override
protected void onPause(){
super.onPause();
//featureImage = null;
//featureView.setImageBitmap(null);
//featureImage.recycle();
this.finish();
}
}
the memory continues to grog up to about 40MB and then drops to 32MB and cycles throgh in this pattern.
Any tipps/suggestions are welcome.
The problem is that you're not scaling the images down to fit the ImageView properly. You have two options:
Read and implement what's shown in this lesson. The benefits of this approach are you only use the code you need, the drawback is that it's quite a lot of work for something that should be straight forward.
Use a library like Glide. It's simple to use and well supported. The drawback is that it does add many methods to your project that you otherwise wouldn't use, adding to the apk size.
Personally, whenever I've got to display an image, I use Glide!

How to pass an image from one activity to another using Intents in Xamarin Android?

I'm a web developer but recently started exploring the Android development world using Xamarin but I'm struggling to find a way to do this task.
My image is located in drawables-hdpi.
In my main activity, I've set a header image on an imageview using this tutorial http://developer.xamarin.com/recipes/android/resources/general/load_large_bitmaps_efficiently/
Now, I created another activity where when a user clicks on my header image, the second activity comes into action and allows the user to pan and zoom around the image.
I need the second activity to dynamically receive the image from the first activity.
Here's what I've tried but with no success. I'm using the same "load images efficiently" code in the second activity as well, but I don't think that matters anyway.
// set image
BitmapFactory.Options options = await GetBitmapOptionsOfImage();
Bitmap bitmapToDisplay = await LoadScaledDownBitmapForDisplayAsync (Resources, options, 400, 400);
headerImage.SetImageBitmap(bitmapToDisplay);
headerImage.Click += (object sender, EventArgs e) => {
var intent = new Intent(this, typeof(ImageScaleActivity));
headerImage.BuildDrawingCache();
Bitmap image = headerImage.GetDrawingCache(true);
Bundle extras = new Bundle();
extras.PutParcelable("imagebitmap", image);
intent.PutExtras(extras);
// TODO: dynamically get image name and send to next activity
StartActivity(intent);
};
That code isn't working, I'm not getting any errors however when I tap on my header image, nothing shows up in the second activity, so the image is obviously not being sent.
Here's the code in my second activity.
// set image
BitmapFactory.Options options = await GetBitmapOptionsOfImage();
Bitmap bitmapToDisplay = await LoadScaledDownBitmapForDisplayAsync (Resources, options, 400, 400);
//expandedImage.SetImageBitmap(bitmapToDisplay);
Bundle extras = Intent.Extras;
Bitmap bmp = (Bitmap) extras.GetParcelable("imagebitmap");
expandedImage.SetImageBitmap(bmp);
I just don't think I'm going about this the proper way and I don't see why something like this seems so difficult to do!
If you have an image in your drawable then there is no need to send it to the second activity. Just set it drawable in your second activity like this.
ImageView imgView=(ImageView) findViewById(R.id.imgView);
Drawable drawable = getResources().getDrawable(R.drawable.img);
imgView.setImageDrawable(drawable);
You need to put the image in the Intent referring to the Activity 2.
You can use byte array to send the Image.
Byte[] pic; // Contains the Picture.
In you Main Activity make an intent of Activity2.
Intent myIntent = new Intent(this, typeof(Activity2));
myIntent.PutExtra("Picture_from_Activity1",pic);
this.StartActivity(myIntent);
Now in Activity2 Receive and display the picture.
byte[] recPic = Intent.GetByteArrayExtra("Picture_from_Activity1");
Now convert this Picture to bitmap and display it.
Bitmap bitmap = BitmapFactory.DecodeByteArray(recPic,0,recPic.Length);
ImageView iv;
iv.SetImageBitmap(bitmap);
:)

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.

Image not displaying sometimes with Universal Image Loader

I have an activity that contains an ImageView, I call this activity every time I want to open an image for better viewing. The problem is that sometimes my ImageLoader works fine but sometimes it does not display the image. I use universal-image-loader-1.8.4.jar and android 4.2.2
When my ImageLoader not display my image I get in the log:
ImageView is reused for another image. Task is cancelled.
My configuration:
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
.enableLogging()
.threadPriority(Thread.MAX_PRIORITY)
.build();
imageLoader.init(config);
My method:
private void getImage() {
Bundle bundle = getIntent().getExtras();
String dir= bundle.getString("poster");
imageLoader.displayImage(dir, imgPoster);
}
Can anyone help me?
I meet the problem too, this is because you want to set the adapter or show the image before the activity was created.
Try this:
String dir = "";
Handler mHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch(msg.what){
case 0:
imageLoader.displayImage(dir, imgPoster);
break;
}
};
};
private void getImage() {
Bundle bundle = getIntent().getExtras();
dir= bundle.getString("poster");
mHandler.sendEmptyMessageDelayed(0,500);
}
Try and check if the image url's have spaces in them.
In that case, replace the spaces with "%20".
stringURL.replaceAll(" ", "%20");

Categories

Resources