2017-11-18 40 views
2

嘗試從Google相冊中選擇視頻時,我收到了null
它的作品,如果視頻也存儲在你的外部存儲器但它不工作,
如果它的唯一可用谷歌照片。我嘗試了很多解決方案,但不知道如何實現這一點。下面是我用來獲取URI的類。嘗試從Google相冊中選擇視頻時,我收到了空格

public class FilePath { 
    private static final DecimalFormat format = new DecimalFormat("#.##"); 
    private static final long MiB = 1024 * 1024; 
    private static final long KiB = 1024; 

    public static String getPath(final Context context, String string) { 
     return getPath(context, Uri.parse(string)); 
    } 

    public static String getPath(final Context context, final Uri uri) { 

     final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; 

     // DocumentProvider 
     if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { 
      // ExternalStorageProvider 
      if (isExternalStorageDocument(uri)) { 
       final String docId = DocumentsContract.getDocumentId(uri); 
       final String[] split = docId.split(":"); 
       final String type = split[0]; 

       if ("primary".equalsIgnoreCase(type)) { 
        return Environment.getExternalStorageDirectory() + "/" + split[1]; 
       } 

       // TODO handle non-primary volumes 
      } 
      // DownloadsProvider 
      else if (isDownloadsDocument(uri)) { 

       final String id = DocumentsContract.getDocumentId(uri); 
       final Uri contentUri = ContentUris.withAppendedId(
         Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); 

       return getDataColumn(context, contentUri, null, null); 
      } 
      // MediaProvider 
      else if (isMediaDocument(uri)) { 
       final String docId = DocumentsContract.getDocumentId(uri); 
       final String[] split = docId.split(":"); 
       final String type = split[0]; 

       Uri contentUri = null; 
       if ("image".equals(type)) { 
        contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
       } else if ("video".equals(type)) { 
        contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; 
       } else if ("audio".equals(type)) { 
        contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
       } 

       final String selection = "_id=?"; 
       final String[] selectionArgs = new String[] { 
         split[1] 
       }; 

       return getDataColumn(context, contentUri, selection, selectionArgs); 
      } 
     } 
     // MediaStore (and general) 
     else if ("content".equalsIgnoreCase(uri.getScheme())) { 
      return getDataColumn(context, uri, null, null); 
     } 
     // File 
     else if ("file".equalsIgnoreCase(uri.getScheme())) { 
      return uri.getPath(); 
     } 

     return null; 
    } 

    /** 
    * Get the value of the data column for this Uri. This is useful for 
    * MediaStore Uris, and other file-based ContentProviders. 
    * 
    * @param context The context. 
    * @param uri The Uri to query. 
    * @param selection (Optional) Filter used in the query. 
    * @param selectionArgs (Optional) Selection arguments used in the query. 
    * @return The value of the _data column, which is typically a file path. 
    */ 
    public static String getDataColumn(Context context, Uri uri, String selection, 
             String[] selectionArgs) { 

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

     try { 
      cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, 
        null); 
      if (cursor != null && cursor.moveToFirst()) { 
       final int column_index = cursor.getColumnIndexOrThrow(column); 
       return cursor.getString(column_index); 
      } 
     } finally { 
      if (cursor != null) 
       cursor.close(); 
     } 
     return null; 
    } 


    /** 
    * @param uri The Uri to check. 
    * @return Whether the Uri authority is ExternalStorageProvider. 
    */ 
    public static boolean isExternalStorageDocument(Uri uri) { 
     return "com.android.externalstorage.documents".equals(uri.getAuthority()); 
    } 

    /** 
    * @param uri The Uri to check. 
    * @return Whether the Uri authority is DownloadsProvider. 
    */ 
    public static boolean isDownloadsDocument(Uri uri) { 
     return "com.android.providers.downloads.documents".equals(uri.getAuthority()); 
    } 

    /** 
    * @param uri The Uri to check. 
    * @return Whether the Uri authority is MediaProvider. 
    */ 
    public static boolean isMediaDocument(Uri uri) { 
     return "com.android.providers.media.documents".equals(uri.getAuthority()); 
    } 

    public static String getFileSize(File file) { 

     if (!file.isFile()) { 
      throw new IllegalArgumentException("Expected a file"); 
     } 
     final double length = file.length(); 

     if (length > MiB) { 
      return format.format(length/MiB) + " MB"; 
     } 
     if (length > KiB) { 
      return format.format(length/KiB) + " KB"; 
     } 
     return format.format(length) + " B"; 
    } 
} 

在此先感謝。

+0

從外部或從內部和外部選擇視頻時遇到問題 –

+0

@Amjad,即時通訊面臨的問題,當我嘗試從谷歌照片應用程序中選擇一個視頻。 –

回答

0

您沒有權限閱讀Google照片向我發送文件的媒體,我想。您必須使用getContentResolver().openInputStream(uri)作爲InputStream打開,並在您的活動中顯示Google照片向您發送的照片。你的代碼是不可接受的。嘗試搜索這個主題。這是討論了很多次。

  Intent intent = getIntent(); 
      if (Intent.ACTION_SEND.equals(intent.getAction()){ 
       uri = intent.getParcelableExtra(Intent.EXTRA_STREAM); 
       if (uri != null) { 
        InputStream is = getContentResolver().openInputStream(uri); 
        long fileSize = is.available() 
        //Do some suff to show stream as a picture in the Activity 
       } 
      } 

此代碼對我的作品與去年穀歌照片應用中使用Android 7.0時,畫面存儲在手機的內部存儲。 如果Google Photos在雲中的某個位置保留了所需的文件,則必須在後臺線程中加載is的數據以避免Networking on main thread異常。

+0

感謝您的建議@isabsent,我試着下面提到的代碼,但得到'FileNotFoundException': InputStream inputStream = getContentResolver()。openInputStream(data.getData()); –

+0

您試圖查看的照片位於何處? – isabsent

+0

上述代碼僅適用於該圖片實際位於手機上或谷歌照片上的情況。在我的情況下,圖片並非物理位於設備上,它僅位於谷歌照片上。 'uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);'它返回null。 –

相關問題