我正在开发一款使用相机拍照然后在另一项活动中展示的应用,我尝试过以下代码
public void onImageAvailable(ImageReader imageReader) {
Image image = imageReader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
Bitmap myBitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);
SetBitmap setBitmap = new SetBitmap();
setBitmap.setMbitmap(myBitmap);
image.close();
}
在此之后我在其他活动中得到我的位图
myBitmap = CamActivity.setBitmap.getMbitmap();
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);
SetBitmap是一个设置位图的类
当我点击它时,我有一个捕捉凸轮的按钮,它假设拍摄照片并开始另一个活动来显示图像但它崩溃然后 那么我现在能做什么呢?我是以错误的方式开始我的活动?还是问题是位图? 这是错误日志
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.camtest/com.example.android.camtest.DisplayActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap com.example.android.camtest.SetBitmap.getMbitmap()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap com.example.android.camtest.SetBitmap.getMbitmap()' on a null object reference
at com.example.android.camtest.DisplayActivity.onCreate(DisplayActivity.java:37)
at android.app.Activity.performCreate(Activity.java:6283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
java.lang.IllegalStateException: Handler (android.media.ImageReader$ListenerHandler) {b9cb033} sending message to a Handler on a dead thread
at android.os.MessageQueue.enqueueMessage(MessageQueue.java:325)
at android.os.Handler.enqueueMessage(Handler.java:631)
at android.os.Handler.sendMessageAtTime(Handler.java:600)
at android.os.Handler.sendMessageDelayed(Handler.java:570)
at android.os.Handler.sendEmptyMessageDelayed(Handler.java:534)
at android.os.Handler.sendEmptyMessage(Handler.java:519)
at android.media.ImageReader.postEventFromNative(ImageReader.java:511)
at android.hardware.camera2.legacy.LegacyCameraDevice.nativeProduceFrame(Native Method)
at android.hardware.camera2.legacy.LegacyCameraDevice.produceFrame(LegacyCameraDevice.java:516)
at android.hardware.camera2.legacy.RequestThreadManager$2.onPictureTaken(RequestThreadManager.java:224)
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1142)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.hardware.camera2.legacy.CameraDeviceUserShim$CameraLooper.run(CameraDeviceUserShim.java:144)
at java.lang.Thread.run(Thread.java:818)
这是我拍照的地方
private void takePic() {
if (cameraDevice == null)
return;
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraDevice.getId());
Size[] jpegSizes = null;
if (characteristics != null)
jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
.getOutputSizes(ImageFormat.JPEG);
int width = 700;
int height = 600;
if (jpegSizes != null && jpegSizes.length > 0) {
width = jpegSizes[0].getWidth();
height = jpegSizes[0].getHeight();
}
final ImageReader reader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);
List<Surface> outputSurface = new ArrayList<>(2);
outputSurface.add(reader.getSurface());
outputSurface.add(new Surface(textureView.getSurfaceTexture()));
final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureBuilder.addTarget(reader.getSurface());
captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
int rotation = getWindowManager().getDefaultDisplay().getRotation();
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));
String fileName = "IMG_" + new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date()) + ".jpg";
File captureDirectory = new File("/Download");
if (!captureDirectory.isDirectory()) captureDirectory.mkdirs();
final File mediaFile = new File(captureDirectory, fileName);
final Uri fileUri = FileProvider.getUriForFile(this,
"com.mydomain.fileprovider",
mediaFile);
ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader imageReader) {
Image mImage = imageReader.acquireLatestImage();
ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
try {
save(bytes);
} catch (IOException e) {
e.printStackTrace();
}
OutputStream output = null;
try {
output = getContentResolver().openOutputStream(fileUri);
output.write(bytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
mImage.close();
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void save(byte[] bytes) throws IOException {
OutputStream output = null;
try {
output = new FileOutputStream(mediaFile);
output.write(bytes);
} finally {
if (output != null)
output.close();
}
}
};
Intent displayIntent = new Intent(this, DisplayActivity.class);
displayIntent.putExtra("FILE_URI", fileUri);
Log.e("uri", fileUri + "");
startActivity(displayIntent);
reader.setOnImageAvailableListener(readerListener, mBackgroundHandler);
final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
Toast.makeText(CamActivity.this, "Saved ", Toast.LENGTH_SHORT).show();
}
};
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
这是展示活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
imageView = findViewById(R.id.imageView);
bundleExtras = getIntent().getExtras();
if(bundleExtras != null){
Uri savedBitmapUri = bundleExtras.getParcelable("FILE_URI");
imageView.setImageURI(savedBitmapUri);
Log.e("uri",savedBitmapUri+"");
} else {
Toast.makeText(this, "null pic", Toast.LENGTH_SHORT).show();
}
}
文件提供程序
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="myDownloads" path="Download" />
<root-path name="sdcard1" path="." /> </paths>
答案 0 :(得分:3)
将位图数据传递给另一个活动的方法是错误的。首先,您应该检查Android Activity Lifecycle。当你启动不同的Activity时,让我们从A调用它,A活动将被销毁,所有资源都将被销毁。在Android Activity LifeCycle文档中,他们提到了这一点:
onDestroy()回调会释放所有尚未存在的资源 由早期的回调版本发布,例如onStop()。
因此,您无法在活动销毁后访问变量。你应该做的是,你应该将位图保存到文件中,并通过intent extra传递文件URI,并从另一个活动的onCreate回调中获取URI。
您可以使用FileProvider保存文件,并通过意图传递提供程序URI,这是最好的“ Android种类 ”方法,或者您可以保存文件的路径共享首选项,并从另一个不稳定的活动中读取它,而不是最佳实践。
让我们使用FileProvider: 您可以查看Android Documentation about FileProvider来定义它。
// Assume that you added Captures path to your FileProvider's paths.
// Create temporary image file.
String fileName = "IMG_" + new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date()) + ".jpg";
File captureDirectory = new File(getFilesDir(), "Captures");
if( !captureDirectory.isDirectory() ) captureDirectory.mkdirs();
File mediaFile = new File(captureDirectory, fileName);
Uri fileUri = FileProvider.getUriForFile(this,
"com.mydomain.fileprovider",
mediaFile);
// Save the Bitmap.
public void onImageAvailable(ImageReader imageReader) {
Image mImage = imageReader.acquireLatestImage();
ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
OutputStream output = null;
try {
output = getContentResolver().openOutputStream(fileUri);
output.write(bytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
mImage.close();
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// Pass the file uri via intent.
Intent displayIntent = new Intent(this, DisplayActivity.class);
displayIntent.putExtra("FILE_URI", mediaFile);
startActivity(displayIntent);
// Read the uri from started activity and show the bitmap
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
...
bundleExtras = getIntent().getExtras();
if(bundleExtras != null){
Uri savedBitmapUri = bundleExtras.getParcelable("FILE_URI");
mImageView_Preview.setImageURI(savedBitmapUri);
}
else{
// Uri not inside the intent data.
}
...
}
您不应将所有字节作为位图加载到内存中。您可以将捕获的图像保存到文件中,并传递从应用程序的FileProvider创建的文件Uri,并使用一行代码显示文件Uri。
基本优化: - 您可以在缓存目录中创建文件 - 使用FileProvider permissions完成任务后,您可以删除该文件。 - 您可以像Google在android-Camera2Basic示例中所做的那样在后台线程上执行保存工作。 - 还有更多...