0

Hi i am new one for android....i want to crop and save the image in sd card when i capturing the image....when we capture the image it does not store in sd card...after cropping only it save in sd card?...and the image quality should not affect...and we can know this path..i don't have code about this...please provide full code if you can..this code is force closed..i dont know how to resolve this.. pls tell me thanks in advance

    import java.io.FileNotFoundException;
    import java.io.IOException;   
    import android.content.ContentResolver;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.ParcelFileDescriptor;
    import android.util.Log;

    public class ImageResize {
      private Context mContext;
      private int mWidth;
      private int mHeight;
      private Uri mImageUri;
      private BitmapFactory.Options mBitMapOptions;
      private Bitmap mBitMap;
      private Bitmap tempBitMap;

      public ImageResize(Context context, int width, int height, Uri imgUri){
        this.mContext = context;
        this.mWidth = width;
        this.mHeight = height;
        this.mImageUri = imgUri;
      }

      public Bitmap getResizeImage(){
        ContentResolver resolver = mContext.getContentResolver();
        mBitMapOptions = new BitmapFactory.Options();

        if(mImageUri != null){
          ParcelFileDescriptor fd = null;
          try {
            fd = resolver.openFileDescriptor(mImageUri, "r");
            int sampleSize = 1;

            mBitMapOptions.inJustDecodeBounds = true;
            BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, mBitMapOptions);

            int nextWidth = mBitMapOptions.outWidth >> 1;
            int nextHeight = mBitMapOptions.outHeight >> 1;

            while(nextWidth > mWidth && nextHeight > mHeight){
              sampleSize <<= 1;
              nextWidth >>= 1;
              nextHeight >>= 1;
            }

            mBitMapOptions.inSampleSize = sampleSize;
            mBitMapOptions.inJustDecodeBounds = false;

            mBitMap = BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, mBitMapOptions);
            Log.d("Result","Image use Size : " +mWidth+"," +mHeight);
            Log.d("Result","Image Size : " +mBitMap.getWidth()+"," + mBitMap.getHeight());
            Log.d("Result","aa : " +(mWidth*mBitMap.getHeight())/mBitMap.getWidth());
            if(mBitMap!=null){
              if(mBitMapOptions.outWidth != mWidth || mBitMapOptions.outHeight != mHeight){ 
                //??????? :  ???? ????????  =  (???? ???????? * ?????????) / ????????? 
                tempBitMap = Bitmap.createScaledBitmap(mBitMap, mWidth, (mWidth*mBitMap.getHeight())/mBitMap.getWidth(), true);
                mBitMap.recycle();
                mBitMap = tempBitMap;
              }
            }

            return mBitMap;

          } catch (FileNotFoundException e) {
            Log.e(getClass().getSimpleName(), e.getMessage(), e);
          } finally {
              try { if(fd != null) fd.close(); } catch (IOException e) { Log.e(getClass().getSimpleName(), e.getMessage(), e);}
              if(mBitMap != null) mBitMap = null;
              if(tempBitMap != null) tempBitMap = null;
          }
        }
        return null;
      }
    }
Bala
  • 107
  • 1
  • 2
  • 10

1 Answers1

0

If you already have the bitmap, following code will crop it and save it at the specified path (make sure the path exists). The file will be saved in the format specified. If you choose jpeg, the quality factor has to be specified. Saving in png will ignore the quality argument.

File outputFile = new File("/sdcard/SaveDir/SaveBitmap.png");
//x,y is the starting point and width,height is the distance from start
//createBitmap(Bitmap source, int x, int y, int width, int height)
Bitmap bitmap = Bitmap.createBitmap(originalBitmap, 0, 0, 20, 20);
OutputStream fout = null;

try {
    fout = new FileOutputStream(outputFile);
    //if format is png(lossless) then quality argument is ignored
    //(Bitmap.CompressFormat format, int quality, OutputStream stream)
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fout);
    fout.flush();
    fout.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}
Srikant Sahay
  • 885
  • 6
  • 9