【问题标题】:retrieve the path of a saved image causes crash检索保存图像的路径导致崩溃
【发布时间】:2012-03-23 17:08:50
【问题描述】:

是否有一种标准方法来检索相机拍摄的保存图像的路径,因为我找到了不同的方法来显示路径,但它们会导致应用程序崩溃。 由于我是 android 编程新手,我只需要一些指导。 请检查代码:

Java 代码:

@Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File imagesFolder = new File(Environment.getExternalStorageDirectory(), "My Images");
            imagesFolder.mkdirs();
            File image = new File(imagesFolder, "img01");
            uriSavedImage = Uri.fromFile(image);
            File f = new File (uriSavedImage.getPath());// <---------
            //imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
            imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, f.getPath()); // <---------
            startActivityForResult(imageIntent,CAMERA_REQUEST_CODE);
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if ((requestCode == CAMERA_REQUEST_CODE) && (resultCode == RESULT_OK)) {

            Toast.makeText(getApplicationContext(),// <---------
                    " "+data.getData(), 
                    Toast.LENGTH_SHORT).show();

【问题讨论】:

  • 你能把堆栈跟踪粘贴到这里吗?

标签: android android-camera mediastore


【解决方案1】:

它崩溃是因为您将 getParent() 传递给 convertImageUriToFile 方法(您可以在堆栈跟踪中看到 NullPointerException)。根据android docs getParent() 如果活动不是嵌入式活动则返回null。你的很可能不是。

请改用File f = convertImageUriToFile(uriSavedImage, this);

对于另一个错误,imageUri 为空,请参阅aswer 以获得非常可能的解释和解决方案。

Morover,您的 imageUri 是“f​​ile://”类型的 uri,而不是“content://”类型。要从中获取路径,请使用

new File(uri.getPath());

而不是您的 convertImageUriToFIle() 方法。我认为该方法仅适用于“content://”类型的 uris。

其实,你不需要从你自己制作的uri中获取文件路径,你在行中有那个路径:

File image = new File(imagesFolder, "img01");

【讨论】:

  • Toast.makeText(getApplicationContext(), " "+convertImageUriToFile(uriSavedImage, this).getAbsolutePath(), Toast.LENGTH_SHORT).show(); 我尝试显示上述路径,但应用程序仍然崩溃
  • 如果您需要帮助,请提供堆栈跟踪。
  • 你的 imageUri 不是为空吗?查询执行期间出现 nullPointerException。检查您的 imageUri 是否不为空且格式正确(以 content:// 开头)
  • 我给 imageuri 赋值,但它是空的
  • 请再次检查代码我修改了onActivityResult()方法
猜你喜欢
  • 2021-03-23
  • 2017-12-21
  • 1970-01-01
  • 2012-06-28
  • 2015-11-17
  • 2011-07-05
  • 2012-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多