0
イメージビューからビットマップを取得して、それを拡大しようとしています。私はImageViewのが終了する前にそれを測定するため、インターネットを検索し、ビットマップに変換したようAndroid - ビューが測定されたときにビットマップを更新する
:「> 0でなければならない幅と高さjava.lang.IllegalArgumentExceptionが」
:しかし、私は以下のエラーを取得しています、私はビットマップを更新しようとします。誰も私がどのようにこれを修正することができますabouy助けることができます。私のコードは以下の通りです:
profileImage.buildDrawingCache();
Bitmap bm = profileImage.getDrawingCache();
int widthPixel = bm.getWidth();
int heightPixel = bm.getHeight();
int basePixel;
float widthRatio;
float heightRatio;
if (widthPixel < heightPixel) {
basePixel = widthPixel;
}
else {
basePixel = heightPixel;
}
if (basePixel > 180) {
widthRatio = 180/basePixel;
heightRatio = 180/basePixel;
}
else {
widthRatio = 1;
heightRatio = 1;
}
Bitmap bmResized = Bitmap.createScaledBitmap(bm,(int)(bm.getWidth()*widthRatio), (int)(bm.getHeight()*heightRatio), true);
// bm.recycle();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmResized.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byteArray1 = stream.toByteArray();
編集コード(今や "java.lang.IllegalArgumentExceptionが。:値がnullではないかもしれない" 取得エラー):
profileImage.buildDrawingCache();
bm = profileImage.getDrawingCache();
profileImage.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
public boolean onPreDraw() {
profileImage.getViewTreeObserver().removeOnPreDrawListener(this);
widthPixel = profileImage.getMeasuredWidth();
heightPixel = profileImage.getMeasuredHeight();
if (widthPixel < heightPixel) {
basePixel = widthPixel;
}
else {
basePixel = heightPixel;
}
if (basePixel > 180) {
widthRatio = 180/basePixel;
heightRatio = 180/basePixel;
}
else {
widthRatio = 1;
heightRatio = 1;
}
Bitmap bmResized = Bitmap.createScaledBitmap(bm,(int)(bm.getWidth()*widthRatio), (int)(bm.getHeight()*heightRatio), true);
// bm.recycle();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmResized.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byteArray1 = stream.toByteArray();
image1 = new ParseFile("profilePhoto.jpg", byteArray1, "image/jpg");
return true;
}
});
こんにちはライナー、私は「OnPreDrawListener」と、上記のように私のコードを編集し、今私は取得しています「java.lang.IllegalArgumentExceptionがを:値はnullではないかもしれません。」エラー。上のコードをチェックしてもよろしいですか?ありがとうございました。 – saner
アプリケーションがExceptionでクラッシュした場合は、その行番号を参照してください。これにより、エラーが発生しているコード内の行が指摘され、問題をデバッグすることができます。スローされるエラーは 'IllegalArgumentException'であり、' valueはnullではない可能性があります 'というメッセージが表示されるので、 'null'ビットマップのサイズを変更しようとしていると思います。 – Reinier
@sanerあなたのアプローチにはいくつかの問題があります。ビューの描画キャッシュを 'ビットマップ 'にレンダリングしようとしています。しかし、Viewはまだ描画されていないので、Viewの描画キャッシュは空であり、 'view.getDrawingCache()'は 'null'を返すようになっています。ビューを強制的に描画するか(SOの例がたくさんあります)、別の方法をとってください。一つの方法は、 'view.getDrawable()'を呼び出してから、 'Drawable'をビットマップに変換してから、サイズを変更することです。 – Reinier