[英]How can I get a result from an onActivityResult(int requestCode, int resultCode, Intent data)?
I'm working on an Android project which allow the user to take a picture using the camera. 我正在开发一个允许用户使用相机拍照的Android项目。
My method looks like this : 我的方法看起来像这样:
private void takePicture(){
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera, 1337);
}
And in my onActivityResult I take the picture and store it in my SdCard. 在我的onActivityResult中,我拍摄照片并将其存储在我的SdCard中。 I trying to modify my takePicture() function to return the path of the picture (String instead void).
我试图修改我的takePicture()函数来返回图片的路径(String而不是void)。
I already know that getPath() from java.io.File allow me to do that. 我已经知道java.io.File中的getPath()允许我这样做。
So my question is : how can I modify my function to return the path of the picture?
所以我的问题是:如何修改我的函数以返回图片的路径?
takePicture
won't be able to return a path, as it'll return before your intent has completed. takePicture
将无法返回路径,因为它将在您的意图完成之前返回。 You'll need to do something with the path inside onActivityResult
你需要对
onActivityResult
的路径做一些事情
Here is the code to get the path of captured and stored image in pictureCallback 以下是获取pictureCallback中捕获和存储图像路径的代码
in this code mFilePath
is the string which is holding the path of image... 在这段代码中,
mFilePath
是保存图像路径的字符串......
PictureCallback mPicture = new 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();
String mFilePath = pictureFile.getAbsolutePath();
} 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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.