从Android中的相机意图获取uri [英] Get uri from camera intent in android

查看:99
本文介绍了从Android中的相机意图获取uri的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击

 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);
            }
        });

关于结果:

 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;
    }

我想要实现的非常简单,调用相机意图并获得结果照片的uri.但是似乎不同的设备存在不一致,并且在我的设备上根本不起作用.我试图将路径存储在一个公共变量上,但是当我检索它时,它为null,是否存在正式和标准的方式来实现它,并且应该在所有设备上都可以使用?另外,有什么方法可以不提供自定义路径,而是从相机意图中获取默认的uri路径?

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 ?

感谢您的帮助

推荐答案

如果您的设备不遵守cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,您仍然可以使用

If your device does not respect cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, you still can use

Uri imageUri = data.getData();

onActivityResult(int requestCode, int resultCode, Intent data)中的

.但是在大多数情况下,问题在于RAM有限,并且系统破坏了您的活动,无法为相机应用程序提供足够的内存来满足您的意图.因此,返回结果时,您的活动字段未初始化,您应遵循 Amit 的操作建议并实施onSavedInstance()onRestoreInstanceState().

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().

这篇关于从Android中的相机意图获取uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆