2017-03-10 3 views
2

Google 드라이브에서 PDF를 선택하고 '사본 보내기'를 통해 공유를 클릭하면 앱이 다운됩니다.드라이브에서 내 애플리케이션으로 파일을 공유 할 수 없습니다. - android

받은 의도 데이터에서 파일을 만들려고합니다. Uri가받은 콘텐츠는 다음과 같습니다. //com.google.android.apps.docs.storage.legacy/enc%3DwZUW7yjtovmYjBMzKXQVChubXRmP0Qum361CVLRoUegdADMl%0A.

이 uri에서 파일을 만들려고하면 null이 반환됩니다.

이미지, mp3, 비디오, pdf 등의 파일을 안드로이드에서 공유 할 수있는 방법이 있습니까? 내가받은 의도 된 데이터에서 파일을 만들려고 해요

File file = FileHelper.getFile(this, mUri); 

public static File getFile(Context context, Uri uri) { 
     if (uri != null) { 
      String path = getPath(context, uri); 
      if (path != null && isLocal(path)) { 
       return new File(path); 
      } 
     } 
     return null; 
    } 

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

     if (DEBUG) 
      Log.d(TAG + " File -", 
        "Authority: " + uri.getAuthority() + 
          ", Fragment: " + uri.getFragment() + 
          ", Port: " + uri.getPort() + 
          ", Query: " + uri.getQuery() + 
          ", Scheme: " + uri.getScheme() + 
          ", Host: " + uri.getHost() + 
          ", Segments: " + uri.getPathSegments().toString() 
      ); 

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

     // DocumentProvider 
     if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { 
      // LocalStorageProvider 
      if (isLocalStorageDocument(uri)) { 
       // The path is the id 
       return DocumentsContract.getDocumentId(uri); 
      } 
      // ExternalStorageProvider 
      else 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 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 null; 
    } 

답변

3

만들기 파일, 갤러리 등을 보관 작동합니다.

단계 # 1 : Uri

3 단계에 의해 확인 된 내용에 InputStream을 얻을 수있는 ContentResolveropenInputStream() 사용 :

단계 # 2이있는 모든 코드를 삭제합니다 : 당신이 제어 할 수있는 파일에 FileOutputStream 만들기 (예를 들어, getFilesDir() 또는 getCacheDir()에서)

단계 # 4 : 0123에 InputStream에서 콘텐츠를 복사

IOW의 경우 웹 사이트 URL과 마찬가지로 Uri을 처리하고 자신의 파일에 콘텐츠를 다운로드 한 다음 자신의 파일을 사용합니다.

+0

. 파일을 다운로드하면서 어떻게 파일을 만들 수 있습니까? 파일 확장명을 알 수 있습니까? 확장을 사용하지 않고 mime 유형이 원본과 다르며보기가 내 응용 프로그램에서 다릅니다. – APR

+0

@Uni : MIME 타입을 얻기 위해'ContentResolver'에서'getType()'을 호출하십시오. 'MimeTypeMap'을 사용하여 제안 된 파일 확장자를 얻으십시오. – CommonsWare

+0

일부 파일의 경우 확장 프로그램이 null을 반환합니다. .json과 같은 .php 파일 – APR

관련 문제