指定された Base64 文字列を Android の対応する画像に正常に変換できます。アプリでこのシナリオをテストするために、ドローアブル フォルダーから 1 つの画像を取得し、この Web サイト ( Motobit.com)を使用して対応する Base64 文字列に変換しました。このウェブサイトで私が提供した画像は次のとおりです。
サイズは 23X25 ピクセル、サイズは 46.3KB です。Android で以下のコードを使用して、この画像の Base64 を次のように画像に変換しています。
byte[] decodedString = Base64.decode(tabData.getString("TabIconImageData"), Base64.DEFAULT);
BitmapFactory.Options options = new Options();
options.inJustDecodeBounds = true;
options.inSampleSize = calculateInSampleSize(options, 500, 500);
options.inJustDecodeBounds = false;
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length,options);
myImageView.setImageBitmap(decodedByte);
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// 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;
}
Base64 文字列は画像に正常に変換されていますが、元の画像のサイズのほぼ半分に見えます。元のサイズの画像と PNG 形式の画像が必要です。これを解決するために私を導いてください。