0

画像をsqlite dbに保存する必要があります。動作しています。たとえば、カメラからの写真を保存できます。しかし、フォトギャラリーから写真を選択すると (700kb 以上)、DB に保存されません。そこで、この大きなサイズの写真を小さくすることができると考えました。そこで、これらのコードを以下に記述しました。

 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);
    }
}

小さくしているわけではありません。そして、デバイスでアプリを閉じています。ここで何がうまくいかないのですか?

4

2 に答える 2

0

Sridhar の答えは機能しますが、覚えておくべきことの 1 つは、最初に InputStream をフルサイズでデコードする必要があり、メモリ使用量が増加することです。これは、InputStream が非常に大きな画像を表す場合に問題になる可能性があります。これは、OutOfMemoryExceptions が発生する可能性があるためです。InputStream からスケーリングされたビットマップに直接移動するメソッドを次に示します。

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;
}

inJustDecodeBounds とビットマップ サンプリングの詳細については、 https: //developer.android.com/training/displaying-bitmaps/load-bitmap.html を参照してください。

于 2013-09-27T18:35:15.793 に答える
0

画像の再スケーリングが機能し、画像のサイズが縮小されると思います。

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

幅と高さ、および int で指定できる可変サイズ。元。

int width=500;
int height=500;
于 2013-09-27T15:10:20.833 に答える