从android中的相机意图获取uri
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21388586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get uri from camera intent in android
提问by user782104
on click
点击
takePic.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent m_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageUri = getImageUri();
m_intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(m_intent, REQUEST_IMAGE_CAPTURE);
}
});
On Result:
结果:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE
&& resultCode == RESULT_OK) {
Log.d("test1",""+imageUri);
Intent shareIntent = new Intent(this, SharePicForm.class);
shareIntent.putExtra("photo",""+imageUri);
startActivity(shareIntent);
}
}
getImageUri()
getImageUri()
private Uri getImageUri(){
Uri m_imgUri = null;
File m_file;
try {
SimpleDateFormat m_sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
m_curentDateandTime = m_sdf.format(new Date());
m_imagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + m_curentDateandTime + ".jpg";
m_file = new File(m_imagePath);
m_imgUri = Uri.fromFile(m_file);
} catch (Exception p_e) {
}
return m_imgUri;
}
What I would like to achieve is very simple, call camera intent and get the uri of the result photo. But it seems there is an inconsistent of different device and on my device it isn't work at all. I tried to store the path on a public variable but when I retrieve it , it is null, is there formal and standard way to implement it and should be work on all device? Also, are there any way for not provide the custom path but get the default uri path from the camera intent ?
我想实现的很简单,调用相机意图并获取结果照片的uri。但似乎不同设备不一致,在我的设备上它根本不起作用。我试图将路径存储在一个公共变量上,但是当我检索它时,它为空,是否有正式和标准的方法来实现它并且应该在所有设备上工作?另外,有什么方法可以不提供自定义路径,而是从相机意图中获取默认的 uri 路径?
Thanks for helping
谢谢你的帮助
采纳答案by Alex Cohn
If your device does not respect cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT
, you still can use
如果您的设备不尊重cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT
,您仍然可以使用
Uri imageUri = data.getData();
in onActivityResult(int requestCode, int resultCode, Intent data)
. But in most cases, the problem is that RAM is limited, and the system destroys your activity to give enough memory to the camera app to fulfill your intent. Therefore, when the result is returned, the fields of your activity are not initialized, and you should follow Amit's suggestion and implement onSavedInstance()
and onRestoreInstanceState()
.
在onActivityResult(int requestCode, int resultCode, Intent data)
。但在大多数情况下,问题在于 RAM 是有限的,系统会破坏您的 Activity,以便为相机应用程序提供足够的内存来满足您的意图。因此,返回结果时,您的活动的字段未初始化,您应该遵循Amit的建议并实现onSavedInstance()
和onRestoreInstanceState()
。
回答by umair
First of all make sure that your directory is created......
首先确保您的目录已创建......
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ "/Folder/";
File newdir = new File(dir);
newdir.mkdirs();
on button click call this function.
在按钮上单击调用此函数。
private void capturarFoto() {
String file = dir+DateFormat.format("yyyy-MM-dd_hhmmss", new Date()).toString()+".jpg";
File newfile = new File(file);
try {
newfile.createNewFile();
} catch (IOException e) {}
Uri outputFileUri = Uri.fromFile(newfile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
Log.d("Demo Pic", "Picture is saved");
}
}
Make sure You add permission in manifest
确保您在清单中添加权限
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
回答by Shoaib Anwar
simply use this Uri imageUri = data.getData();
只需使用这个 Uri imageUri = data.getData();