0

I must save image to the sqlite db.It is working.For example I can save the photo which is come from camera. But when I select a photo from photo gallery (700kb or more than) it is not saving into the DB.So I thought that I can make small this big size photos.So I have written these codes below;

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     if(requestCode == SELECT_PHOTO && resultCode == RESULT_OK){
        Uri selectedImage = data.getData();
        InputStream imageStream = null;
        try {
            imageStream = getContentResolver().openInputStream(selectedImage);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //Resmin boyutu küçültüldükten sonra DB'ye atılacak.102400,512000
        Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
        cekilenFoto.setImageBitmap(yourSelectedImage);
        resimData = getBitmapAsByteArray(yourSelectedImage);
    }
}

It is not making small. And It is closing the app on device. What is going wrong here?

emreturka
  • 846
  • 3
  • 18
  • 44

2 Answers2

0

I think so rescaling the image works and reduces the size of the image.

if ( yourSelectedImage!= null) {
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(bit, width,
                height, true);
    cekilenFoto.setImageBitmap(resizedBitmap);
    resimData = getBitmapAsByteArray(resizedBitmap);
   }

width and height and variable size you can give in int. ex.

int width=500;
int height=500;
Sridhar
  • 95
  • 1
  • 8
0

While Sridhar's answer will work, one thing to keep in mind is that it requires first decoding the InputStream at full size, increasing memory usage. This can be an issue if the InputStream represents a very large image, because it can cause OutOfMemoryExceptions. Here's a method that will go directly from InputStream to scaled bitmap:

public static Bitmap scaledBitmapFromStream(Context context, InputStream tempIs) {
    // Buffer the InputStream so that it can be accessed twice.
    InputStream is = new BufferedInputStream(tempIs);

    // Find the dimensions of the input image, and calculate the sampleSize.
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(is, null, options);
    options.inJustDecodeBounds = false;

    // Calculate the inSampleSize. This is the factor that the image will be rescaled by.
    options.inSampleSize = calculateInSampleSize(context, options.outWidth, options.outHeight);

    // Reset the input stream, so it can be read again.
    try {
        is.reset();
    } catch (IOException e) {
        throw new RuntimeException("BufferedInputStream.reset() failed.", e);
    }

    // The 'options' parameter here tells the BitmapFactory to downscale.
    Bitmap output = BitmapFactory.decodeStream(is, null, options);

    // Close the input stream.
    try {
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return output;
}

/**
 * How much you want to downsample will vary based on your application, but this implementation
 * calculates a safe display size based on devices screen resolution and OpenGL MAX_TEXTURE_SIZE.
 */
public static int calculateInSampleSize(Context context, int inputWidth, int inputHeight) {
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    final int maxWidth = Math.min(displayMetrics.widthPixels, GLES20.GL_MAX_TEXTURE_SIZE);
    final int maxHeight = Math.min(displayMetrics.heightPixels, GLES20.GL_MAX_TEXTURE_SIZE);

    int inSampleSize = 1;
    if (inputWidth > maxWidth || inputHeight > maxHeight) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) inputHeight / (float) maxHeight);
        final int widthRatio = Math.round((float) inputWidth / (float) maxWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

See https://developer.android.com/training/displaying-bitmaps/load-bitmap.html for more information on inJustDecodeBounds and Bitmap sampling.

Nic Dahlquist
  • 1,005
  • 12
  • 15
  • I used like ,InputStream imageStream = null; try { imageStream = getContentResolver().openInputStream(selectedImage); } catch (FileNotFoundException e) { e.printStackTrace(); } //Resmin boyutu küçültüldükten sonra DB'ye atılacak.102400,512000 Bitmap yourSelectedImage = scaledBitmapFromStream(getApplicationContext(), imageStream); – emreturka Sep 28 '13 at 09:59
  • java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. – emreturka Sep 28 '13 at 13:27