このハンドラーにメッセージを送信して (ここではいくつかのケースを省略しています)、ビットマップに対して何らかの操作を行い、UI にビットマップを設定します。ビットマップでの操作は時間がかかるのでスレッドに入れました。これが終わったら、メソッドrunOnUiThreadを使用して UI スレッドに UI を設定します。
通常、これはすべて正常に実行されますが、次の例外が発生する場合がありました:
非常にまれに発生しますが、数回発生しました。
private Handler mHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
int what = msg.what;
switch (what)
{
case VideoProtocal.COMPRESS_SUCCESS:
state = State.COMPRESSED;
recordBack.setVisibility(View.VISIBLE);
recordSend.setVisibility(View.VISIBLE);
playButton.setVisibility(View.VISIBLE);
new Thread(){
@Override
public void run()
{
rotationBitmap();
runOnUiThread(new Runnable() {
public void run() {
if( null != bitmap && !bitmap.isRecycled() )
{
Drawable drawable = new BitmapDrawable(bitmap);
fristFrame.setBackgroundDrawable(drawable);
}
}
});
}
}.start();
mFrameCount = 0;
getVideoSize();
videoTime = getVideoFileTime(mp4_path) / 1000;
timeView.setText(getString(R.string.video_count, videoTime));
deleteTempFile();
LogX.trace(TAG, "COMPRESS_SUCCESS");
dismissProgress();
break;
default:
break;
}
}
};
メソッド rotationBitmap() とそれが呼び出すいくつかのメソッドを以下に示します。
private void rotationBitmap()
{
int width = (int) (PIC_WIDTH * FusionField.currentDensity);
int height = (int) (PIC_HIGHT * FusionField.currentDensity);
Bitmap bmTemp = bitmap;
bitmap = CommonUtil.resizeImage(bitmap, width, height);
if (bmTemp != null && !bmTemp.isRecycled() && bmTemp != bitmap)
{
bmTemp.recycle();
bmTemp = null;
}
rotate(bitmap, rotationAngle());
CommonUtil.checkMysoftStage(FusionCode.videoThumbnail);
String videoName = CommonUtil.getUniqueName(".jpg", FusionCode.videoName);
jpg_path = FusionCode.videoThumbnail + "/" + videoName;
try
{
FileOutputStream fos = new FileOutputStream(jpg_path);
if (bitmap != null)
{
bitmap.compress(CompressFormat.JPEG, 50, fos);
}
fos.close();
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Bitmap rotate(Bitmap b, int degrees)
{
if (degrees != 0 && b != null)
{
Matrix m = new Matrix();
m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
try
{
bitmap = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
}
catch (OutOfMemoryError ex)
{
// TODO Auto-generated catch block
ex.printStackTrace();
}
}
return b;
}
コードから、問題は次のステートメントで発生する必要があります。
Drawable drawable = new BitmapDrawable(bitmap);
しかし、コード コンテキストによると、ビットマップは null またはリサイクルされるべきではありません。これは非常に奇妙です。
私はこの問題をほぼ 2 日間ぶらぶらしてきましたが、何が問題なのかわかりませんが、問題は存在します。誰か私にいくつかの提案をしてもらえますか? どんな反応でも大歓迎です!ありがとう!<br />
ところで、このようにRuntimeExceptionをしようとすると、この種の例外をキャッチできません。
try
{
bitmap.recycle();
Drawable drawable = new BitmapDrawable(bitmap);
fristFrame.setBackgroundDrawable(drawable);
}
catch (RuntimeException e)
{
e.printStackTrace();
}