Getting error on bitmap.recycle() in my app - android

I am creating a app with 160 plus images all are between 150 and 300KB. And after testing the app in crashes giving me a out of memory error. I have read posts on here about that but when i implement bitmap.recycle() it gives a me a red line underneath the bitmap.
heres my code:
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
final ImageView imageView = (ImageView) findViewById(R.id.iM1);
imageView.setImageResource(R.drawable.sample);
bitmap.recycle();
slider.animateClose();
}
});
Can anyone plese help?

You have to have a BitMap to call bitmap.recycle(). BitMap.recycle() removes the bitmap you have write before the .recycle().
Example:
Bitmap someBit= new Bitmap (this);
someBit.recycle();
Based on the code above this is the right answear.

Related

Glide image from gallery to bitmap variable

I have a variable Bitmap image. I have imageButton in my fragment. I can change image of imageButton with Glide.with().load().into(). But I want not to just change image but to save it into Bitmap variable so I can use it for some other task. I tried this
Bitmap image = Glide.with(getContext()).asBitmap().load(imagePath).submit().get();
imageButton.setImageBitmap(image);
But image of imageButton does not change. What's wrong with first line then? Because I am pretty sure that problem is there but don't understand yet what is exactly wrong.
A solution is to do
Bitmap image = BitmapFactory.
decodeStream(getContext().
getContentResolver().
openInputStream(selectedImage));
imageButton.setImageBitmap(image);
Where selectedImage is Uri selectedImage = data.getData() of onActivityResult method, but it will be total mess because of size of bitmap, so we have to use createScaledBitmap, I don't like it. For some reason Bitmap image = BitmapFactory.decodeFile(imagePath) doesn't work, imageButton just becomes tiny grey square, and I didn't find a solution for this.
first of all why are using the imageButton..use imageView it will good than
imagebutton and it will work
I don't know if you should even think about doing this in some serious project but for now it did what needs to be done for me. Earlier I declared private Bitmap jopa.
Glide.with(getContext()).asBitmap().
load(selectedImage).override(500,500).
into(new CustomTarget<Bitmap>()
{
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
imgShare.setImageBitmap(resource);
jopa = ((BitmapDrawable)imgShare.getDrawable()).getBitmap();
}
#Override
public void onLoadCleared(#Nullable Drawable placeholder) {
}
});
Now Bitmap jopa can be used for other tasks. I hope. Tested it to change picture of another imageview, it worked, so I'll pray it will work further. Full onActivityResult code here. Don't want question to get too messy

How to do image blur on touch (progressively)?

I want to blur a portion of bitmap and set into image view. But I am not getting any reference of the same.
Took me 40 seconds to find the solution in stackoverflow, Please use search :)
Use the blur from here:
https://stackoverflow.com/a/10028267/5618671
(Make sure to +1 Yahel, the user that posted the blur solution)
Get your Bitmap (can get it from your existing imageView), Call the fast blur when imageView has been clicked, and set the imageView with the new blurred Bitmap :
example
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//GET BITMAP FROM IMAGEVIEW
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmapFromImageView = drawable.getBitmap();
Bitmap blurredBitmap = fastblur(bitmapFromImageView, BITMAP_SCALE, BLUR_RADIUS)
imageView.setImageBitmap(blurredBitmap);
}
});
P.S Make sure imageview is declared final:
final ImageView imageView;

How can I take a screenshot of my app and share it in Android Studio?

I want to implement a button on my app that can let the user take a screenshot of the app and then share it as image on social networks, but I don't know how to do that, I've tried with Bitmap but didn't work.
This is what I've done, but is not working:
public static Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
Hope you can help me :D

Load part of bitmap in scrollable imageview and load the rest when scrolling?

I'm trying to load in part of a tall image into a scrollable imageview (imageview inside a scrollview).
It works with smaller images but if the images is bigger than 2048x2048 the app will crash due to open gl out of memory.
I can load part of the image with this:
private Bitmap decodeBitmapRegion(InputStream in, Rect region) {
BitmapRegionDecoder decoder = null;
try {
decoder = BitmapRegionDecoder.newInstance(in, false);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
region.right = decoder.getWidth();
Bitmap reg = decoder.decodeRegion(region, null);
return reg;
}
Where the rectangle can be something like (0,0,DisplayWidth,800).
That works in most cases since i'm using the imageview to let the user crop a part of the image and save it as a coveriamge.The imageview has the same width as the display and the height of 400 dp.
So the image the user saves will be the same width as the orginal but only 400 dp's tall.
Is there anyway to make it possible for the user to scroll all the way to the bottom of the orginal image?
How does webview load images that are bigger than 2048x2048?
Here's one thought.
Imagine your image broken into rows of bands. Use a ListView to display the bands. A ListView uses an adapter. In adapter.getView(), you use the decoder to load the appropriate band.
The ListView already does "recycling". So, it more-or-less only holds enough rows to fill the screen.
This technique should work well if your images aren't too wide. If they are, and it's not tall enough to occupy more than one screen height, you'll still have the same memory problems. A more complicated solution would be to use some sort of grid view, but then you'll have to do your own view recycling. Not too difficult, but harder than using ListView.
I managed to get the functionality I wanted. But i don't think this is the best solution?
I loaded the image into a webview and then i took a screenshot of the visible part of the webview. After that i can save that bitmap and do whatever i want to it.
But there must be a better way to do this than to use a webview to show the original image?
Anyway here is what I did:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
imgView = (ImageView) findViewById(R.id.imageview);
webView = (WebView) findViewById(R.id.webview);
webView.loadUrl("file:///android_asset/test.jpg");
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
imgView.setImageBitmap(getBitmapForVisibleRegion(webView));
}
});
}
public static Bitmap getBitmapForVisibleRegion(WebView webview) {
Bitmap returnedBitmap = null;
webview.setDrawingCacheEnabled(true);
returnedBitmap = Bitmap.createBitmap(webview.getDrawingCache());
webview.setDrawingCacheEnabled(false);
return returnedBitmap;
}

Resize Imageview and fitXY

I am resizing an ImageView and auto fitting the image to it, I need this for zoom in and out for image, this is the way i wish to implement this because the image is already 2048x2048 and I cant re-size it. Bu the code only works once for some reason, second click does not do anything. If you have other ideas or suggestions I would love to hear them. Thank you.
ImageView map = (ImageView) findViewById(R.id.imageViewMap);
Bitmap mapIm = BitmapFactory.decodeResource(getResources(),R.drawable.myMap);
map.setImageBitmap(mapIm); //first view
zoomIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
map.getLayoutParams().height = counter*5000 ;
map.getLayoutParams().width = counter*5000;
BitmapDrawable bmd = new BitmapDrawable(mapIm);
map.setImageDrawable(bmd);
bmd = null;
counter++;
}
});
For zooming your image view
imageview.setScaleType(ScaleType.FIT_XY);
or you can try the following link hope it may help you
zoomableimageview
try this hope it may help you
example zoom

Categories

Resources