如何将捕获的图像从自定义相机传递到另一个活动?



我目前正在尝试构建一个自定义相机应用程序,该应用程序将捕获图像,然后将其显示在另一个活动中(通过更新ImageView(。

目前,我可以通过相机预览成功捕获并保存在设备上的图像,甚至可以使用捕获的图像成功更新该活动中的ImageView - 但是,我将捕获的图像传递给第二个活动时遇到了更多困难。

我在网上看到很多使用ActivityResult和Intent组合的解决方案,但它们似乎都使用设备的本机相机应用程序,而不是像我这样的自定义相机。

我的相机活动类如下所示:

@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_activity);
swatch = (ImageView) findViewById(R.id.swatch);
preview = (FrameLayout) findViewById(R.id.camera_preview);
cameraButton = (Button) findViewById(R.id.captureButton);

cameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCamera.takePicture(null, null, mPicture);
}
});
}
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null) {
Log.d("Camera error",
"Error creating media file, check storage permissions: ");
return;
}
Log.i("Picture", pictureFile.toString());
try {
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
BitmapFactory.Options scalingOptions = new BitmapFactory.Options();
scalingOptions.inSampleSize = camera.getParameters().getPictureSize().width / imageView.getMeasuredWidth();
final Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length, scalingOptions);
} catch (FileNotFoundException e) {
Log.d("Camera error", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("Camera error", "Error accessing file: " + e.getMessage());
}
finish();
openColourDetectorActivity();
}
};
private static File getOutputMediaFile(int type) {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
"Medici");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.i("Medici", "failed to create directory");
return null;
}
}
@SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyymmddhhmmss")
.format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}

public void openColourDetectorActivity() {
Intent intent = new Intent(this, ColourDetectorActivity.class);
startActivity(intent);
}

当然,我正在尝试在该活动的onCreate方法的另一端接收它。

知道我该怎么做吗?

openColourDetectorActivity(pictureFile);
public void openColourDetectorActivity(File file) {
Intent intent = new Intent(this, ColourDetectorActivity.class);
intent.putExtra("FILE", file.getPath());
startActivity(intent);
}

in 颜色检测器活动 :

new File(getIntent().getStringExtra("FILE"))

相关内容

  • 没有找到相关文章

最新更新