1

写真を撮って編集する必要があります。元のカメラを使用して大きなサイズの画像を返すため、編集する新しいテンプレート画像を作成しようとすると、outOfMemoryError のためにアプリケーションが強制的に閉じられます。アプリケーションにロードする前に画像のサイズを変更する方法はありますか?

私のコードはカメラで画像を撮ります:

void loadImageFromCamera(){
    Intent takePicture = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = null;
    try{
        // place where to store camera taken picture
        photo = this.createTemporaryFile("picture", ".jpg");
        photo.delete();
    } catch(Exception e){

    }
    mImageUri = Uri.fromFile(photo);
    takePicture.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
    startActivityForResult(takePicture, 0);
}
private File createTemporaryFile(String part, String ext) throws Exception{
    File tempDir = Environment.getExternalStorageDirectory();
    tempDir = new File(tempDir.getAbsolutePath() + "/.temp/");
    if(!tempDir.exists()){
        tempDir.mkdir();
    }
    return File.createTempFile(part, ext, tempDir);
}
public void grabImage(){
    this.getContentResolver().notifyChange(mImageUri, null);
    ContentResolver cr = this.getContentResolver();
    try {
        originImage = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);
    }catch (Exception e){

    }

}
4

1 に答える 1

1

はい、画像のサイズを変更できます 次の方法を使用して画像のサイズを変更します

public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
    int width = image.getWidth();
    int height = image.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height,
            matrix, false);
    return resizedBitmap;
}
于 2012-12-26T08:32:52.727 に答える