gpt4 book ai didi

android - 如何存储使用 camera.takePicture(null,null,mPicture) 拍摄的图像,以便在下一个 Activity 中显示?

转载 作者:行者123 更新时间:2023-11-29 16:46:39 27 4
gpt4 key购买 nike

我正在 android 中制作自定义 camera 应用程序。我使用

捕获 image

camera.takePicture(null,null,mPicture)”和“Camera.PictureCallBack()

我想存储从相机 拍摄的图像,以便我可以在下一个 Activity 中显示它。怎么做到的?

    Camera.PictureCallback mPicture = new Camera.PictureCallback() {

@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);

fos.close();
} catch (FileNotFoundException e) {

} catch (IOException e) {
}
}
};



private static File getOutputMediaFile() {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyCameraApp");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");

return mediaFile;
}

最佳答案

在你的下一个 Activity Intent 的额外部分中传递 uri

public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_PICTURE_USING_CAMERA) {
try {
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("uri",outputFileUri);
startActivity(i);
} catch (Exception ex) {
Log.e("Exception", ex.toString());
}
}
}
}

然后在您的第二个 Activity 中检索图像

Uri = getIntent().getExtras("uri");

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;

final Bitmap capturedimage = BitmapFactory.decodeFile(uri, options);
imgPreview.setImageBitmap(capturedimage );

关于android - 如何存储使用 camera.takePicture(null,null,mPicture) 拍摄的图像,以便在下一个 Activity 中显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47695936/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com