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!
Related
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.
}
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);
:)
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);
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
Allright so here's my problem. I have an app which contains alot of pictures and i wanted to be able to pinchzoom these, but since the ImageViewer doesnt support this natively I thought I'd use the Webviewer instead, but heres the thing. all my pictures are saved into my "picture" int and its the picture function i want to load into the webviewer. not a specific \drawable\bla.jpg Searched all around but didnt find anything about it. I'll attach some code for reference.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=getIntent();
int picture=intent.getIntExtra("picture", 22);
setContentView(R.layout.pictureframe);
ImageView image = (ImageView) findViewById(R.id.pansarvagn);
image.setBackgroundResource(picture);
and its here where i want smth like
Webview image = (WebView) findViewById(R.id.pansarvagn);
image.(setdata bla bla)
and this is the picture function
public void displayPicture(int pictureresource){
Intent intent = new Intent();
intent.putExtra("picture", pictureresource);
intent.setClass(getApplicationContext(), Picture.class);
startActivity(intent);
called by
Button tank = (Button) findViewById(R.id.tank);
tank.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
displayPicture(R.drawable.tank);
}
To further elaborate i want to put an image into a webview which i will be able to zoom into. so some sort of imageview inside the webview to fill up the webview with my picture. and then I want to be able to zoom in on it.
You need to create you own ContentProvider and override it's opeenFile(..) method. Then you can feed it to WebView:
myWebView.loadUrl("content://your.content.provider/someImageName");
Here is an example: http://www.techjini.com/blog/2009/01/10/android-tip-1-contentprovider-accessing-local-file-system-from-webview-showing-image-in-webview-using-content/
Note: with this approach you will need to save your images somewhere. You will not be able to serve them from memory.