Scaling down photo taken by camera and getting the picture Path

I am trying to scale the photos taken with a camera, but i am not sure where or how to do this. Right now the code is accessing the camera, taking a picture and displaying it in a listview, i would like to also get the picture path but am unsure how to do this aswell. Any help would be highly appreciated.

/**
     * This function is called when the add player picture button is clicked.
     * It accesses the devices gallery and the user can choose a picture
     * from the gallery.
     * Or if the user chooses to take a picture with the camera, it handles that
     */

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case TAKE_PICTURE:
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedImage = imageUri;
                getContentResolver().notifyChange(selectedImage, null);
                ContentResolver cr = getContentResolver();
                this.picPath = selectedImage.getPath();
                Bitmap bitmap;
                try {
                     bitmap = android.provider.MediaStore.Images.Media
                     .getBitmap(cr, selectedImage);
                    imageView = (ImageView) findViewById(R.id.imagePlayer); 
                    imageView.setImageBitmap(bitmap);
                    Toast.makeText(this, selectedImage.toString(),
                            Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                            .show();
                    Log.e("Camera", e.toString());
                }
            }

Thanks

Answers


After getting bitmap image you can use createScaledBitmap static method from Bitmap class

   Bitmap.createScaledBitmap(yourBitmap, 50, 50, true); // Width and Height in pixel e.g. 50

but in future extreme memory low condition... If you're not careful, bitmaps can quickly consume your available memory budget leading to an application crash due to the dreaded exception: java.lang.OutofMemoryError: bitmap size exceeds VM budget.

so To avoid java.lang.OutOfMemory exceptions, check the dimensions of a bitmap before decoding it, unless you absolutely trust the source to provide you with predictably sized image data that comfortably fits within the available memory.

  // below 3 line of code will come instead of 
//imageView.setImageBitmap(bitmap);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();         
    photo.compress(Bitmap.CompressFormat.JPEG,100,stream);
    imageView.setImageBitmap(decodeSampledBitmapFromByte(stream.toByteArray(),50,50));  

The BitmapFactory class provides several decoding methods (decodeByteArray(), decodeFile(), decodeResource(), etc.) for creating a Bitmap from various sources. Choose the most appropriate decode method based on your image data source. These methods attempt to allocate memory for the constructed bitmap and therefore can easily result in an OutOfMemory exception. Each type of decode method has additional signatures that let you specify decoding options via the BitmapFactory.Options class. Setting the inJustDecodeBounds property to true while decoding avoids memory allocation, returning null for the bitmap object but setting outWidth, outHeight and outMimeType. This technique allows you to read the dimensions and type of the image data prior to construction (and memory allocation) of the bitmap.

 // please define following two methods in your activity
    public Bitmap decodeSampledBitmapFromByte(byte[] res,
                int reqWidth, int reqHeight) {

            // First decode with inJustDecodeBounds=true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeByteArray(res, 0, res.length,options);

            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeByteArray(res, 0, res.length,options);
        }
         public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;

            if (height > reqHeight || width > reqWidth) {
                if (width > height) {
                    inSampleSize = Math.round((float)height / (float)reqHeight);
                } else {
                    inSampleSize = Math.round((float)width / (float)reqWidth);
                }
            }
            return inSampleSize;
        }

And please refer following link from Android Training any bitmap related java.lang.OutofMemoryError: bitmap size exceeds VM budget http://developer.android.com/training/displaying-bitmaps/load-bitmap.html


Need Your Help

FreeRTOS in C++ changes task memory?

c++ memory freertos

I have the following C++ wrapper for the FreeRTOS task:

Can I sign ActiveX control with root certificate?

activex certificate openssl code-signing ca

I have an ActiveX control, which is supposed to be installed at the first visit of web-page.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.