【问题标题】:Android and FileProvider and getting patth from URIAndroid 和 FileProvider 以及从 URI 获取路径
【发布时间】:2014-05-05 13:50:17
【问题描述】:

我有一些令我困惑的问题。我似乎无法获得基于文件的 Uri 的正确 真实 路径。下面是带有最终结果变量值的代码:

  String shareFile_Path = "";
  String contentURI_Path_Real = "";          
  String contentURI_Path_Simple = "";          
  Uri myContentUri_Camera = null;          

  Bitmap myBitmap = SharedCode.sharedGetImageAsBitmapFromResource(thisActivityContext, R.drawable.replace__logo__app_256_256);           
  File mySharedFile_Camera = SharedCode.sharedSaveBitmapToInternalStoragePath(thisActivityContext, "shared", "camera.jpg", myBitmap);

  if ( (mySharedFile_Camera != null) && (mySharedFile_Camera.exists()) ) {

    shareFile_Path = mySharedFile_Camera.getAbsolutePath();

    try {
      myContentUri_Camera = FileProvider.getUriForFile(thisActivityContext, "com.example.app.fileprovider", mySharedFile_Camera);
    }
    catch (Exception e) {
      String debug_s = e.toString();
    }            
    if (myContentUri_Camera != null) {
      contentURI_Path_Simple = myContentUri_Camera.getPath(); 
      contentURI_Path_Real = SharedCode.sharedGetRealPathFromURI(thisActivityContext, myContentUri_Camera);
    }  
  }  

不同变量的最终结果:

  • sharedFile_Path = "/data/data/com.example.app/files/shared/camera.jpg"
  • contentURI_Path_Simple = "/providershared/camera.jpg"
  • contentURI_Path_Real = ""

可以看出,问题在于 contentURI_Path_Real 的值,这就是为什么 SharedCode.sharedGetRealPathFromURI 的完整代码在这里 - 我最初通过 SO 找到的内容:

  public static String sharedGetRealPathFromURI(Context context, Uri contentUri) {
    String res = "";
    if (contentUri != null) {      
      Cursor cursor = null;      
      try {
        cursor = context.getContentResolver().query(contentUri, null, null, null, null);
        if (cursor !== null) { 
          cursor.moveToFirst();
          int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
          if (idx > -1) {
            res = cursor.getString(idx);
          }  
        }  
      } 
      catch(Exception e){
      }         
      if (cursor != null) {
        cursor.close();
      }      
    }              
    return res;             
  }

在单步执行代码时,似乎这 if (idx > -1) 产生 false。

...

这是AndroidManifest.xml

的相关代码
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.app.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_filepaths" />
</provider>

这是 provider_filepaths.xml

的完整代码
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path path="shared/" name="providershared" />
</paths>

...

最后,当调用相机意图时,我需要一个有效的 myContentUri_Camera:

  Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  if ( (myContentUri_Camera != null) && (contentURI_Path_Real != "") ) {
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, myContentUri_Camera);
  }  
  startActivityForResult(cameraIntent, CAMERA_REQUEST); 

现在,如果 myContentUri_Camera 被传递,相机会在完成后使应用程序崩溃 - 在 onActivityResult 返回之前。我怀疑这是因为它没有“真实路径”,这就是我在这里询问路径问题的原因。

【问题讨论】:

    标签: android uri internal-storage android-fileprovider


    【解决方案1】:

    最后,当调用相机意图时,我需要一个有效的 myContentUri_Camera

    你已经有了。

    但是,您的安全性搞砸了。您的&lt;provider&gt; 未导出并且您没有在Intent 中使用FLAG_GRANT_URI_WRITE_PERMISSION。因此,相机应用程序无权使用您的Uri。我建议你add FLAG_GRANT_URI_WRITE_PERMISSION。

    【讨论】:

    • 我现在已经投票了,因为我可以看到我在那里有一个错误! :) 我仍然很好奇为什么 sharedGetRealPathFromURI 的代码没有返回预期的结果。 (如果相机旋转位图,我需要再次使用 Uri 读取 - 然后在将其显示在应用程序中之前对其进行修复。)我稍后会回来!
    • @Tom:“我仍然很好奇为什么 sharedGetRealPathFromURI 的代码没有返回预期的结果”——为什么会这样?它做出了各种假设,例如文件存在并被MediaStore 索引,但这里似乎都不是这种情况。没有人应该使用该代码 sn-p,尤其是在 Android 4.4+ 上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    相关资源
    最近更新 更多