2017-12-04 38 views
0

Android 저장 파일 선택 및 앱 저장 공간 (이미지, 동영상, 문서)에서 파일을 선택하고 있습니다. 나는 "getPath"함수를 가지고있다. 나는 uri에서 경로를 얻고있다. 갤러리 이미지 나 다운로드 문서에는 아무런 문제가 없습니다. 하지만 Google 드라이브에서 파일을 선택하면 경로를 가져올 수 없습니다. 이것은 Google 드라이브의 uri입니다. "content : //com.google.android.apps.docs.storage/document/acc%3D25%3Bdoc%3D12" 도와 줄 수 있습니까?Google 드라이브 URI에서 경로 가져 오기

이것은 내 "getPath"기능입니다.

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

    // check here to KITKAT or new version 
    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]; 
      } 
     } 
     // 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); 
     } 
     else if(isGoogleDriveUri(uri)){ 
       //Get google drive path here 

     } 
    } 
    // MediaStore (and general) 
    else if ("content".equalsIgnoreCase(uri.getScheme())) { 

     // Return the remote address 
     if (isGooglePhotosUri(uri)) 
      return uri.getLastPathSegment(); 

     return getDataColumn(context, uri, null, null); 
    } 
    // File 
    else if ("file".equalsIgnoreCase(uri.getScheme())) { 
     return uri.getPath(); 
    } 

    return nopath; 
} 

public static String iStreamToString(InputStream is1) 
{ 
    BufferedReader rd = new BufferedReader(new InputStreamReader(is1), 4096); 
    String line; 
    StringBuilder sb = new StringBuilder(); 
    try { 
     while ((line = rd.readLine()) != null) { 
      sb.append(line); 
     } 
     rd.close(); 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    String contentOfMyInputStream = sb.toString(); 
    return contentOfMyInputStream; 
} 

답변

1

나는 갤러리의 이미지 나

당신은, 많은 장치에서 다운로드 문서 아무 문제가 없습니다.

하지만 구글 드라이브에서 파일을 선택하면 내가 경로

어떤 경로가 없습니다 그럴수. ACTION_GET_CONTENT은 사용자가 파일을 선택할 수 없도록합니다. 사용자는 콘텐츠를 선택할 수 있습니다. 그 내용은 일 수 있습니다 로컬 파일입니다. 그 내용 또한 수 있습니다 : Google 드라이브와 같은 클라우드 스토리지 서비스 on a file server on the network

  • 뭔가

    • 뭔가, 따라서 순간 장치에 an encrypted volume
    • 뭔가에
    • 뭔가 아니라고 는 파일이지만
    • 에 같은
    • 그래서 다른 앱 internal storage에서와 같이 직접 액세스 할 수있는 파일이 아닙니다

    두 가지 주요 옵션이 있습니다. 에만 개의 파일을 쓰려면 a third-party file chooser library을 사용하여 질문의 모든 코드를 교체하십시오.

    또는, 당신은 여전히 ​​ACTION_GET_CONTENT 또는 ACTION_OPEN_DOCUMENT을 사용하려는 경우, 당신은 당신이 onActivityResult()data.getData()에서 얻을 것을 Uri을 그것으로 두 가지 작업을 수행 할 수 있습니다

    • 첫째, DocumentFile를 얻을 수 DocumentFile.fromSingleUri()를 사용 그 객체를 가리키는 객체는 Uri입니다. DocumentFile에서 getName()으로 전화를 걸어 콘텐츠에 대한 '표시 이름'을 가져올 수 있습니다.이 표시는 사용자가 인식해야합니다. 그런 다음

    • , 당신은 파일의 바이트에서 얻을 수있는 FileInputStream을 사용하는 방법과 유사 콘텐츠 자체에서 얻을 수 ContentResolveropenInputStream()를 사용합니다.