Android Reduce Size Of Camera Picture

冷暖自知 提交于 2019-11-27 19:56:12

So far I have my app which is taking a picture and then using an Intent to carry the picture over and display in a new Activity.

In most cases, passing data between activities using Intent extras is a fine solution. Large bitmaps, though, cause problems that way due to memory consumption. This is one case where I would carefully use a static data member.

In my logcat I am getting java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=7431KB, Allocated=2956KB, Bitmap Size=19764KB) which I think means that the image is too large.

It means that you do not have enough memory for whatever it is you are trying to do.

Looking through the code I think it searches for the smallest resouloution/size and then takes the camera using those settings am I right in thinking that

Yes.

if so how can I implement that into my code?

Copy and paste usually works. :-)

To tell the camera what size picture to take, use setPictureSize() on Camera.Parameters. However, you have to choose a size it supports, and not all devices will support arbitrary sizes. Calling getSupportedPictureSizes() will tell you which sizes are supported. It is up to you to determine which of those sizes to use. In the example I linked to from your other question, I iterate over those sizes and find the smallest one in terms of area.

That being said, if the only issue with image size is passing it from activity to activity, I would go the static data member route first. Just be sure to null out that static data member when you no longer need the image.

Something like this maybe?

Again, that depends on what your objective is. You seem to be flailing around looking for solutions, when you have only articulated one problem (and, even there, you have not told us where you are getting the out of memory error).

If your goal is to take a full-resolution picture, save it in a file, and then hold in memory a thumbnail, createScaledBitmap() is a fine solution.

If your goal is to take a thumbnail-ish picture in the first place (i.e., you do not need or want a full-resolution image), use setPictureSize().

If your goal is to use a full-resolution picture throughout, carefully use a static data member to eliminate any unnecessary copies of the Bitmap and see if that is sufficient. It might not be, depending on what you are trying to do with the image.

Try this capturing image from Camera.

Intent cameraActivity=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            cameraActivity.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(left));
            startActivityForResult(cameraActivity,CAMERA_PIC_REQUEST_LEFTIMAGE);

This will return you large size image , now for scaling it down u can use something like this

    BitmapFactory.Options options=new BitmapFactory.Options();
                        options.inSampleSize = 6;
                        FileInputStream fileInputStream;    
                        try {
                                fileInputStream=new FileInputStream(left);
                                leftPhoto=BitmapFactory.decodeStream(fileInputStream,null,options);
                                leftImage.setImageBitmap(leftPhoto);
                                fileInputStream.close();
                            } catch (FileNotFoundException e) {
                                Toast.makeText(getCurrentContext(), e.getMessage(),Toast.LENGTH_LONG).show();
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                                Toast.makeText(getCurrentContext(), e.getMessage(),Toast.LENGTH_LONG).show();
                            }

Now u can add on click on this .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!