我有一个问题/问题,我一直在寻找几个星期,仍然没有解决方案.
这个应用程序用于拍摄内部框架的照片.框架是PNG.
当我启动应用程序时,Everythong变得很棒,但当我点击按钮拍照时应用程序崩溃.有时候它会给出OutOfMemoryError,有时会给出ArrayIndexOutOfBoundsException,所以我几乎尝试了所有东西,但却没有.
我应该改变抽签的大小吗?或者我应该设置最终bmp的大小?
因为我是法国人,我很抱歉我的英语
这是代码
private class CapturePictureCallback implements PictureCallback {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap picBitmap = BitmapFactory.decodeByteArray(data, 10,
data.length);
final ViewFlipper flipper = (ViewFlipper) findViewById(R.id.main_flipper);
View flipperView = flipper.getCurrentView();
Bitmap flipperBitmap = Bitmap.createBitmap(flipperView.getWidth(), flipperView.getHeight(), Bitmap.Config.ARGB_4444);
Canvas flipperCanvas = new Canvas(flipperBitmap);
flipperView.draw(flipperCanvas);
Bitmap resizedFlipperBitmap = Bitmap.createScaledBitmap(flipperBitmap, 500, 300, true);
// Insert image on top
Bitmap overlaidBitmap = overlay(picBitmap, resizedFlipperBitmap);
picBitmap.recycle();
resizedFlipperBitmap.recycle();
// Create file
save(overlaidBitmap);
overlaidBitmap.recycle();
camera.startPreview();
}
private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap( 0,0, bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, new Matrix(), null);
return bmOverlay;
}
private void save(Bitmap bitmap) {
File picturesFolder = null;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ECLAIR_MR1) {
picturesFolder = new File(
Environment.getExternalStorageDirectory(), "Pictures");
} else {
picturesFolder = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
}
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
String dateString = sdf.format(date);
String fileName = "kissthepresident" + dateString + ".jpeg";
File pictureFile = new File(picturesFolder, fileName);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bos);
bitmap.recycle();
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ECLAIR_MR1) {
ContentValues values = new ContentValues(7);
values.put(Images.Media.TITLE, fileName);
values.put(Images.Media.DISPLAY_NAME, fileName);
values.put(Images.Media.DATE_TAKEN, dateString);
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(Images.Media.ORIENTATION, 0);
values.put(Images.Media.DATA,
pictureFile.toString());
values.put(Images.Media.SIZE, pictureFile.length());
Uri uri = Uri.fromFile(pictureFile);
getContentResolver().insert(uri, values);
} else {
MediaScannerConnection.scanFile(getApplicationContext(),
new String[] { pictureFile.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindDrawables(findViewById(R.id.main_layout));
System.gc();
}
private void unbindDrawables(View view) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof View) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}
这是catlog
FATAL EXCEPTION: main java.lang.ArrayIndexOutOfBoundsException
at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:403)
at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:418)
at com.kissthepresidents.Main$CapturePictureCallback.onPictureTaken(Main.java:255)
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:529)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
at dalvik.system.NativeStart.main(Native Method)