2017-07-10 11 views
3

でファイル選択者から正しいUriを取得してください。ここに問題があります。電話でファイルを取得し、それを自分の解析サーバーにアップロードする必要があります。私はドキュメント、ダウンロード、外部、メディアフォルダのファイルチューザを作成しましたが、アンドロイドファイルチューザーもGoogleDriveオプションを提案しています。だから私はテ・ウリを得ましたが、私はそのローカルコピーにアクセスする方法を見つけることができません。Googleドライブ

GoogleDrive SDKを使用してアクセスする必要がありますか?それとも、アンドロイドは賢くて、私にそのウリを扱う方法を教えてもらえませんか?

ファイル名の取得に成功しました。ここで

content://com.google.android.apps.docs.storage/document/ 

は、私のファイルチューとハンドラである:

public static void pick(final Controller controller) { 
     final Intent chooseFileIntent = new Intent(Intent.ACTION_GET_CONTENT); 
     chooseFileIntent.setType("application/pdf"); 
     chooseFileIntent.addCategory(Intent.CATEGORY_OPENABLE); 
     if (chooseFileIntent.resolveActivity(controller.getContext().getPackageManager()) != null) { 
      controller.startActivityForResult(chooseFileIntent, Configuration.Request.Code.Pdf.Pdf); 
     } 
    } 

    private static boolean isExternalStorageDocument(Uri uri) { 
     return "com.android.externalstorage.documents".equals(uri.getAuthority()); 
    } 

    private static boolean isDownloadsDocument(Uri uri) { 
     return "com.android.providers.downloads.documents".equals(uri.getAuthority()); 
    } 

    private static boolean isMediaDocument(Uri uri) { 
     return "com.android.providers.media.documents".equals(uri.getAuthority()); 
    } 

    private static boolean isGooglePhotosUri(Uri uri) { 
     return "com.google.android.apps.photos.content".equals(uri.getAuthority()); 
    } 

    private static boolean isGoogleDriveUri(Uri uri) { 
     return "com.google.android.apps.docs.storage".equals(uri.getAuthority()); 
    } 

    private 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 index = cursor.getColumnIndexOrThrow(column); 
       return cursor.getString(index); 
      } 
     } finally { 
      if (cursor != null) 
       cursor.close(); 
     } 
     return null; 
    } 

    private static String getPath(Context context, Uri uri) { 
     if (DocumentsContract.isDocumentUri(context, uri)) { 
      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]; 
       } 
      } else if (isGoogleDriveUri(uri)) { 
//    Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); 
//    if (cursor != null) { 
//     int fileNameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); 
//     cursor.moveToFirst(); 
//     Log.d("=== TAG ===", cursor.getString(fileNameIndex)); 
//     Log.d("=== TAG ===", uri.getPath()); 
//     cursor.close(); 
//    } 
      } 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); 
      } 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 ("content".equalsIgnoreCase(uri.getScheme())) { 
      if (isGooglePhotosUri(uri)) 
       return uri.getLastPathSegment(); 
      return getDataColumn(context, uri, null, null); 
     } 
     else if ("file".equalsIgnoreCase(uri.getScheme())) { 
      return uri.getPath(); 
     } 
     return null; 
    } 

    public static void upload(final Context context, final String name, final ParseObject dataSource, final String field, final Uri uri, final Handler handler) { 
     if (context != null && name != null && dataSource != null && field != null && uri != null) { 
      String path = getPath(context, uri); 
      if (path != null) { 
       final File file = new File(path); 
       dataSource.put(field, new ParseFile(file)); 
       dataSource.getParseFile(field).saveInBackground(new SaveCallback() { 
        @Override 
        public void done(ParseException e) { 
         if (e == null) { 
          if (handler != null) { 
           handler.success(); 
          } 
         } 
        } 
       }, new ProgressCallback() { 
        @Override 
        public void done(Integer percentDone) { 
         if (handler != null) { 
          handler.progress(percentDone); 
         } 
        } 
       }); 
      } 
     } 
    } 

はEDIT:

私はいくつかの試みを行ったが、ここでは、一時ファイル を削除すると、私のときに私は問題を抱えてコード:

public static void copyFile(final Context context, final Uri uri, final ParseObject dataSource, final String field, final String name, final Data.Source target, final Handler handler) { 
     new AsyncTask<Void, Void, Boolean>() { 
      @Override 
      protected Boolean doInBackground(Void... params) { 
       try { 
        InputStream inputStream = context.getContentResolver().openInputStream(uri); 
        if (inputStream != null) { 
         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
         byte[] bytes = new byte[1024]; 
         int length; 
         while ((length = inputStream.read(bytes)) != -1) { 
          byteArrayOutputStream.write(bytes, 0, length); 
         } 
         dataSource.put(field, new ParseFile(name, byteArrayOutputStream.toByteArray())); 
         byteArrayOutputStream.close(); 
         inputStream.close(); 
         return true; 
        } else { 
         return false; 
        } 
       } catch (IOException e) { 
        e.printStackTrace(); 
        return false; 
       } 
      } 

      @Override 
      protected void onPostExecute(final Boolean success) { 
       dataSource.getParseFile(field).saveInBackground(new SaveCallback() { 
        @Override 
        public void done(ParseException e) { 
         if (e == null) { 
          if (handler != null) { 
           handler.success(); 
          } 
         } 
        } 
       }, new ProgressCallback() { 
        @Override 
        public void done(Integer percentDone) { 
         if (handler != null) { 
          handler.progress(percentDone); 
         } 
        } 
       }); 
      } 
     }.execute(); 
    } 

最終Edi t:

ここで私のコードは正しいですが、一時ファイルが作成され、Parse自身によってキャッシュに置かれるので、その範囲外です。彼が助けることを願っています。

答えて

3

私はテ・ウリを得ましたが、私はその「ローカルコピー」にアクセスする方法を見つけることができません。

少なくとも1つはアクセスできる「ローカルコピー」はありません。

アンドロイドは賢くて、私にそのウリを処理する方法を教えてもらえませんか? Uriによって識別されるコンテンツにInputStreamを取得する

使用ContentResolveropenInputStream()。 "パースサーバー"と直接使用するか、それを使用して、管理するファイルに一時的な "ローカルコピー"を作成します。そのローカルコピーをアップロードし、完了したら削除します。ここで

私のファイルチューとハンドラである:

pick()は大丈夫です。 upload()は問題ありません。私はParseを使用していません。そのコードの残りの部分は、以前のジャンクからコピーされた、ジャンクです。それは多くの根拠がなく、信頼性のない仮定をしており、FileProviderを介して提供されているなど、任意のアプリからの値はUriの値では機能しません。

+0

ほとんどのUriで動作するソリューションがありますか?第3層のライブラリを使用する必要がありますか?私は実際にこのすべてを開発する時間がありません。 ありがとうございます! – Nek

+0

@Nek:「大部分のウリと協力する解決策はありますか?」 - 定義上、それは不可能です。 'content'スキームを持つ' Uri'は 'ContentProvider'が望むものを指すことができ、それはファイルである必要はありません。あなたがアクセスできるファイルシステムはもちろんのこと、ファイルである必要はありません。 'ContentResolver'と' openInputStream() 'を使ってください。 "私は実際にこのすべてを開発する時間がありません" - InputStreamからFileOutputStreamへデータをコピーするのは簡単です.Javaプログラミングの本で読めるようなものです。 – CommonsWare

+0

Okeyなので、openInputStreamとローカルの一時コピーを使用するファイルチューザを、最も一般的なものにする必要がありますか? – Nek

関連する問題