2017-11-18 10 views
2

Google 포토에서 비디오를 선택하려고 할 때 null이 표시됩니다.
외부 메모리가 인 에도 저장되어 있지만 작동하지 않는 경우 동영상이
인 경우 작동합니다 (Google 사진에서만 사용 가능). 나는 많은 해결책을 시도했지만 이것을 성취 할 방법을 모른다. 아래는 URI를 얻기 위해 사용하는 클래스입니다.
Google 포토에서 비디오를 선택할 때 null이 나타납니다

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, Google 사진 앱에서 동영상을 선택하려고 할 때 문제가 발생합니다. –

답변

0

Google 포토에서 내가 생각하는 파일을 보내는 미디어를 읽을 권한이 없습니다. getContentResolver().openInputStream(uri)을 사용하여 InputStream으로 열어야하며 Google 포토에서 보내는 사진을 활동에 표시 할 수 있습니다. 귀하의 코드는 받아 들일 수 없습니다. SO를 검색하여이 주제를 찾으십시오. 그것은 많은 시간에 걸쳐 논의되었습니다.

  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 포토 앱에서 나에게 적합합니다. Google 포토가 클라우드의 원하는 위치에 파일을 보관하는 경우 예외를 피하기 위해 is의 데이터를 백그라운드 스레드에로드해야합니다.
+0

제안 해 주신 iisabsent, 아래에 언급 된 코드를 시도했지만'FileNotFoundException'이 발생했습니다 : InputStream inputStream = getContentResolver(). openInputStream (data.getData()); –

+0

보려는 사진의 물리적 위치는 어디입니까? – isabsent

+0

위의 코드는 사진이 휴대 전화 또는 Google 사진에 실제로있는 경우에만 작동합니다. 내 경우에는 그림이 물리적으로 장치에 위치하지 않으며 Google 사진에만 위치합니다. 'uri = intent.getParcelableExtra (Intent.EXTRA_STREAM);'null을 반환합니다. –