i have problem with frame layout drawing cache.
i capture the video from camera and display it in frame layout.
i want to read this image(s) and send it to another client with TCP or UDP.
now the problem is the cache dosent update and i have the same image (complete black image) always
here is my code:
selfImage.setDrawingCacheEnabled(true);
selfImage.buildDrawingCache();
mainImage = selfImage.getDrawingCache();
Bitmap bmp = mainImage.copy(Bitmap.Config.ARGB_8888,false);
streamImage.reset();
bmp.compress(Bitmap.CompressFormat.JPEG, 50, streamImage);
Bitmap temp = bmp;
runOnUiThread(new uiInterface(12, temp));
sendVideo(streamImage);
selfImage.destroyDrawingCache();
mainImage.recycle();
selfImage.setDrawingCacheEnabled(false);
self image is my frame layout!
please help me.
thanks
I used this code. It works in my case.
setDrawingCacheEnabled(true);
destroyDrawingCache();
return getDrawingCache();
Related
I have a web view that I need to take a screenshot from a site. People suggested me use the function capturePicture(), this function is deprecated. So I create bitmap from current site like this and save it as byte[ ] to the database:
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getWidth(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, stream);
DatabaseHelper.updateData(id, stream.toByteArray());
bitmap.recycle();
And load it in another Activity with Glide:
Glide.with(context).load(items.get(position).getImage())
.placeholder(R.drawable.ic_defualt)
.into(holder.ivShot);
Now, If the number of screenshots increases, The speed of the app decreases and Memory size increases. I know that the issue of bitmaps is very complicated, But there must be a way to take a picture of the web view without any memory leaks or slowdowns.
I try to use threads to get bitmap but it doesn't help. Anyone know How I can do it?
I am trying to solve problem when I get bitmap from view (with bad quality of childviews) and store it as PNG. There are some taken pictures using this code:
Bitmap b = Bitmap.createBitmap(
Math.round(General.getDisplayWidthPx()), Math.round(General.getDisplayHeightPx()), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas( b );
wallFrame.invalidate();
wallFrame.draw( canvas );
FileOutputStream out = new FileOutputStream(file);
b.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
When the childview is small the quality of childview is terrible.
But when it's larger the quality is better.
As you can see the smaller the picture, the lower the quality. I also tried deprecated methods:
wallFrame.setDrawingCacheEnabled(true);
Bitmap b = wallFrame.getDrawingCache();
wallFrame is ZoomLayout which extends RelativeLayout. Child views in wallFrame are SubScalingImageView which extends View. I think it's not caused by SubScalingImageView I've tried standard ImageView and result was the same. When I take screenshot of app, the result is better.
Pictures are loaded from user storage using Uri (so not from drawable). SubscalingImageView has own decoders. In app the quality of pictures is perfect.
I think problem is somewhere in view.draw(canvas). But I have no idea how to edit it to make it work.
I appreciate every idea. Thank you!
I try to change this code into resized or scaled bitmap cause if I use compress bitmap, it will get too small image. How can I change it?
FixBitmap3.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream3);
byteArray3 = byteArrayOutputStream3.toByteArray();
ConvertImage3 = Base64.encodeToString(byteArray3, Base64.DEFAULT);
NoLg3 = getIntent().getStringExtra("no_lg");
AsyncTaskUploadClass AsyncTaskUploadClassOBJ = new AsyncTaskUploadClass();
AsyncTaskUploadClassOBJ.execute();
There's a plugin called Android Drawable Importer that is essential. You can google it and download it. It does the heavy lifting for you with respect to resizing images.
In Application i need to load a few Bitmaps on create.
To save Memory i'm loading one image, rescale it by creating a scaledBitmap out of it, recycle the unscaled Bitmap on so on:
bmpUnscaled = BitmapFactory.decodeResource(getResources(), R.drawable.cultivation_plant_resized_1);
plant1 = Bitmap.createScaledBitmap(bmpUnscaled,(int) plant[0].getWidth(), (int) plant[0].getHeight(), true);
bmpUnscaled.recycle();
System.gc();
bmpUnscaled = BitmapFactory.decodeResource(getResources(), R.drawable.cultivation_plant_resized_2);
plant2 = Bitmap.createScaledBitmap(bmpUnscaled,(int) plant[0].getWidth(), (int) plant[0].getHeight(), true);
bmpUnscaled.recycle();
System.gc();
and so on...
I am doing this 10 Images, wich are scaled relative to the screen resolution.
The orginal Image is a PNG (570x900 (max. 660KB))
Does anybody have some Ideas to save Memory simply?
I'm desperate right now...
Have you read: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html?
The article contain great information about working with BitMap.
I tried searching for a relevant example but found none, so I'll try to be as precise as I can.
Back in the gingerbread era I made a draw bitmap to canvas code that worked flawlessly and here it is.
//this one is drawing the background for my picture
mBackground = BitmapFactory.decodeResource(getResources(), R.drawable.base);
//setting a new canvas
mComboImage = new Canvas(mBackground);
//adding another bitmap to the canvas, don't worry about the size or the i variables
mBackImage = BitmapFactory.decodeResource(getResources(), R.drawable.base);
mBackImage=Bitmap.createScaledBitmap(mBackImage, size, 105, false);
mComboImage.drawBitmap(mBackImage, 100f+1f*size*i, 170f, null);
mBackImage.recycle();
//this is setting my background for an icon to the completed image
Drawable d =new BitmapDrawable(getResources(),mBackground);
aaa.setBackground(d);
anyways the code doesn't seem to fit now. One problem I have faced is converting the bitmap into mutable which you can check out here if you are stuck on it as I was for a while.
My problem is that the background is perfectly drawn but the mBackImage doesn't show up at all.
What worries me more is that this used to work perfectly before.
I really have tried searching for a newer explanation but haven't really found any on stackoverflow so
I drew bitmap as background image for my app as well. I used: setBackgroundDrawable() instead of setBackground() in your last statement.
Also, when preparing the bitmap, I set two options:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inInputShareable=true;
bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.image1), options);
You can give it a try.