私は、画像を選択しsd card
て画像ビューに表示する必要があるアプリケーションに取り組んでいます。ここで、ユーザーがボタンをクリックして幅を増減し、SD カードに保存するようにします。
画像を選んでUIに表示しました。しかし、サイズを変更する方法が見つかりません。誰かがそれを達成する方法を教えてください。
ちょうど昨日、私はこれをやった
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();
この方法を試してください:
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);
}
ボタンを使用して幅を増減する代わりに、理想的にはマルチタッチを使用する必要があります。ここに素晴らしいライブラリがあります。ユーザーが画像を保存することを決定したら、画像変換マトリックスを永続的に (sqlite データベースに) 保存する必要があります。次にユーザーが画像を開いたときに、マトリックスを呼び出して画像に適用する必要があります。
私は実際にこれを以前にやったことがあります。