2017-05-28 44 views
0

我想通過打開文件來獲取文件路徑,通過startActivityForResult來選擇,其意圖是Intent.ACTION_GET_CONTENT和setType(*/*),但是當我選擇「Nexus 5X」項目時,返回的uri是「com.android.externalstorage.documents」,如何處理這種類型的uri。 有一些代碼。如何通過uri得到一個文件路徑,其權限是「com.android.externalstorage.documents」

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
intent.addCategory(Intent.CATEGORY_OPENABLE); 
intent.putExtra(Intent.ACTION_DEVICE_STORAGE_OK, true); 
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); 
intent.setType("*/*"); 
startActivityForResult(intent, FILE_ADD_ACTION_REQUEST_CODE); 

screenshot

+0

這不是完整的「返回URI」。 – greenapps

+0

其中一個完整的uri是「內容://com.android.externalstorage.documents/document/home%3Ademo_decry.mp4」 – sohnyi

回答

0

如何處理這種類型的URI

使用ContentResolveropenInputStream()獲得由Uri標識內容的InputStream

0

您也可以嘗試使用這個,如果你使用>奇巧

public static String getRealPathFromURI_API19(Context context, Android.Net.Uri uri) 
    { 
     String filePath = ""; 

     // ExternalStorageProvider 
     if (isExternalStorageDocument(uri)) 
     { 
      String docId = DocumentsContract.GetDocumentId(uri); 
      String[] split = docId.Split(':'); 
      String type = split[0]; 

      if ("primary".Equals(type, StringComparison.OrdinalIgnoreCase)) 
      { 
       return Android.OS.Environment.ExternalStorageDirectory + "/" + split[1]; 
      } 
      else 
      { 

       if (Build.VERSION.SdkInt > BuildVersionCodes.Kitkat) 
       { 
        //getExternalMediaDirs() added in API 21 
        File[] extenal = context.GetExternalMediaDirs(); 
        if (extenal.Length > 1) 
        { 
         filePath = extenal[1].AbsolutePath; 
         filePath = filePath.Substring(0, filePath.IndexOf("Android")) + split[1]; 
         //File f = new File(extenal[1].Parent + File.Separator + "ddddddd.amit"); 
         //var created= f.CreateNewFile(); 
        } 
       } 
       else 
       { 
        filePath = "/storage/" + type + "/" + split[1]; 
       } 
       return filePath; 
      } 

     } 
     else if (isDownloadsDocument(uri)) 
     { 
      // DownloadsProvider 
      String id = DocumentsContract.GetDocumentId(uri); 
      //final Uri contentUri = ContentUris.withAppendedId(
      // Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); 

      //Cursor cursor = null; 
      String column = "_data"; 
      String[] projection = { column }; 

      var data = GetDataColumn(context, uri, column, projection); 
      return data; 
     } 
     else if (DocumentsContract.IsDocumentUri(context, uri)) 
     { 
      // MediaProvider 
      String wholeID = DocumentsContract.GetDocumentId(uri); 

      // Split at colon, use second item in the array 
      String[] ids = wholeID.Split(':'); 
      String id; 
      String type; 
      if (ids.Length > 1) 
      { 
       id = ids[1]; 
       type = ids[0]; 
      } 
      else 
      { 
       id = ids[0]; 
       type = ids[0]; 
      } 

      Android.Net.Uri contentUri = null; 
      if ("image".Equals(type, StringComparison.OrdinalIgnoreCase)) 
      { 
       contentUri = MediaStore.Images.Media.ExternalContentUri; 
      } 
      else if ("video".Equals(type, StringComparison.OrdinalIgnoreCase)) 
      { 
       contentUri = MediaStore.Video.Media.ExternalContentUri; 
      } 
      else if ("audio".Equals(type, StringComparison.OrdinalIgnoreCase)) 
      { 
       contentUri = MediaStore.Audio.Media.ExternalContentUri; 
      } 

      String selection = "_id=?"; 
      String[] selectionArgs = new String[] { id }; 
      String column = "_data"; 
      String[] projection = { column }; 

      var data = GetDataColumn(context, uri, column, projection); 
      return data; 
     } 
     else 
     { 
      //commented------------------------------ 
      //String[] proj = { MediaStore.Audio.Media.DATA }; 
      //Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null); 
      //if (cursor != null) 
      //{ 
      // int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA); 
      // if (cursor.moveToFirst()) 
      //  filePath = cursor.getString(column_index); 
      // cursor.close(); 
      //} 


      return filePath; 
     } 
     return null; 
    } 
+0

僅僅回答代碼的答案並不鼓勵,因爲它們不會爲將來的讀者提供很多信息,請爲您提供一些解釋已經寫了 – WhatsThePoint