Image not displaying sometimes with Universal Image Loader - android

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

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.
}

Intent putExtra bitmap issue

I have the following code:
((ImageButton)findViewById(R.id.fish)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(StartupActivity.this, GameActivity.class);
intent.putExtra(NAME_EXTRA, ((EditText)findViewById(R.id.name)).getText().toString().trim());
intent.putExtra(TYPE_EXTRA, FishTypes.FISH.toString());
intent.putExtra(WORLD_TYPE_EXTRA, worldType);
intent.putExtra(LOGO_EXTRA, BitmapFactory.decodeResource(getResources(), R.drawable.logo));
startActivity(intent);
}
});
If I start activity and click on ImageButton it send me to the same Activity I was before (StartupActivity). However if I comment out the last putExtra like this:
//intent.putExtra(LOGO_EXTRA, BitmapFactory.decodeResource(getResources(), R.drawable.logo));
Then it works fine. It sends me to the GameActivity as I want. What could be the problem?
EDIT
My GameActivity looks like this:
public class GameActivity extends Activity {
public static GamePanel gamePanel;
public static String name;
public static FishTypes type;
public static String worldType;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(getWindow().FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
name = getIntent().getStringExtra(StartupActivity.NAME_EXTRA);
type = FishTypes.parse(getIntent().getStringExtra(StartupActivity.TYPE_EXTRA));
worldType = getIntent().getStringExtra(StartupActivity.WORLD_TYPE_EXTRA);
gamePanel = new GamePanel(this);
//gamePanel.bitmaps.put("logo", (Bitmap)getIntent().getParcelableExtra(StartupActivity.LOGO_EXTRA));
setContentView(gamePanel);
if(!StartupActivity.isNetworkAvailable()) {
Toast.makeText(StartupActivity.getInstance(), "You have no internet connection...", Toast.LENGTH_LONG).show();
finish();
}
}
#Override
protected void onDestroy() {
if(gamePanel.client != null)
gamePanel.client.disconnect();
StartupActivity.getInstance().reopen();
super.onDestroy();
}
}
What I want to achieve is preloading this bitmap in StartupActivity and then just send it to the GameActivity to the GamePanel and then draw it on the canvas as a loading image. I can't load this image in GameActivity because it will be late to show it. Do you understand?
So first of all, there is an Intent payload limit, as far as I know there is a limit of 1MB, but in some cases can be 500kb. If this limit is overreached the app will crash, in your case crashed and restarted.
Second of all, Bundle and Intent are used to send small amount of data to Activity and Fragments, usually some configs/params so that the activity/fragment will know how to build itself.
Third, is a really bad practice to pass around instances of bitmaps, you need just a second of distraction to create a huge leak in your app, that will cost you a lot of time to find and fix it.
Now the solution for you is really simple. You can pass the id of the bitmap you want to use in the next activity.
Intent intent = new Intent(StartupActivity.this, GameActivity.class);
intent.putExtra(NAME_EXTRA, ((EditText)findViewById(R.id.name)).getText().toString().trim());
intent.putExtra(TYPE_EXTRA, FishTypes.FISH.toString());
intent.putExtra(WORLD_TYPE_EXTRA, worldType);
intent.putExtra(LOGO_EXTRA, R.drawable.logo); // R.drawable.logo is actually an int.
startActivity(intent);
In your GameActivity
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(...)
int myAwesomeDrawable = getIntent().getExtra(LOGO_EXTRA, 0); // 0 is default value in case nothing is added to the key
if(myAwesomeDrawable != 0){ // safety check
imageView.setImageResource(myAwesomeDrawable);
// or do whatever you like with it.
}

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!

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

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