26

私は、画像を選択しsd cardて画像ビューに表示する必要があるアプリケーションに取り組んでいます。ここで、ユーザーがボタンをクリックして幅を増減し、SD カードに保存するようにします。

画像を選んでUIに表示しました。しかし、サイズを変更する方法が見つかりません。誰かがそれを達成する方法を教えてください。

4

7 に答える 7

50

ちょうど昨日、私はこれをやった

File dir=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
Bitmap b= BitmapFactory.decodeFile(PATH_ORIGINAL_IMAGE);
Bitmap out = Bitmap.createScaledBitmap(b, 320, 480, false);

File file = new File(dir, "resize.png");
FileOutputStream fOut;
try {
    fOut = new FileOutputStream(file);
    out.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    fOut.flush();
    fOut.close();
    b.recycle();
    out.recycle();               
} catch (Exception e) {}

また、あなたのをリサイクルすることを忘れないでくださいbitmaps:それはメモリを節約します。

新しく作成されたファイル文字列のパスを取得することもできます。newPath=file.getAbsolutePath();

于 2012-07-27T13:46:34.470 に答える
3

この方法を試してください:

public static Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight) {   
if(bitmapToScale == null)
    return null;
//get the original width and height
int width = bitmapToScale.getWidth();
int height = bitmapToScale.getHeight();
// create a matrix for the manipulation
Matrix matrix = new Matrix();

// resize the bit map
matrix.postScale(newWidth / width, newHeight / height);

// recreate the new Bitmap and set it back
return Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(), bitmapToScale.getHeight(), matrix, true);  
} 
于 2012-07-27T13:42:27.033 に答える
1

Bitmap.createScaledBitmap (ビットマップ src、int dstWidth、int dstHeight、ブール フィルター)を使用できます。

于 2012-07-27T13:44:15.750 に答える
0

ボタンを使用して幅を増減する代わりに、理想的にはマルチタッチを使用する必要があります。ここに素晴らしいライブラリがあります。ユーザーが画像を保存することを決定したら、画像変換マトリックスを永続的に (sqlite データベースに) 保存する必要があります。次にユーザーが画像を開いたときに、マトリックスを呼び出して画像に適用する必要があります。

私は実際にこれを以前にやったことがあります。

于 2012-07-27T13:47:52.047 に答える