2017-09-04 3 views
0

私はアンドロイドでファイルチューザを実装したいと思います。私は以下のコードチューザを開くためのコードを実装しました。どのようにアンドロイドのfilechooserドロップボックスとGoogleドライブの指定されたURIから実際のパスを取得するには?

public static void showFileChooser(Activity activity, Fragment fragment) { 
     try { 

      File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "example"); 
      if (!imageStorageDir.exists()) { 
       imageStorageDir.mkdirs(); 
      } 
      File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg"); 
      mCapturedImageURI = Uri.fromFile(file); // save to the private variable 

      final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI); 
      captureIntent.putExtra("capturedimageuri", mCapturedImageURI.toString()); 

      // Intent for Audio Recording 
      final Intent audioRecordIntent = new Intent(); 
      audioRecordIntent.setAction(IAdoddleConstants.ACTION_AUDIO_RECORD); 

      final Intent videoRecordIntent = new Intent(); 
      videoRecordIntent.setAction(IAdoddleConstants.ACTION_VIDEO_RECORD); 

      // Use the GET_CONTENT intent from the utility class 
      Intent target = com.asite.adoddle.filechooser.FileUtils.createGetContentIntent(); 
      // Create the chooser Intent 


      if (activity != null) { 
       Intent intent = Intent.createChooser(
         target, activity.getString(R.string.chooser_title)); 

       intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { captureIntent,audioRecordIntent, videoRecordIntent}); 
       activity.startActivityForResult(intent, IMAGE_ANNOTATION_REQUEST_CODE); 
      } else { 
       Intent intent = Intent.createChooser(
         target, fragment.getString(R.string.chooser_title)); 

       intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { captureIntent,audioRecordIntent, videoRecordIntent}); 
       fragment.startActivityForResult(intent, IMAGE_ANNOTATION_REQUEST_CODE); 
      } 

     } catch (ActivityNotFoundException e) { 
      AdoddleLog.e(DEBUG_TAG, "Error:" + e); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
      CommonUtilities.showToast(activity, activity.getString(R.string.error_message), Toast.LENGTH_LONG); 
     } 
    } 

これで、次のように同じファイルを開きます。今

enter image description here

私は「ドキュメント」私は以下のように同じファイルを選択することができる場所から別のアプリケーションを取得することにクリックしてください。

enter image description here

ここでは、GoogleドライブやDropboxのようなアプリケーションを見ることができます。これで、このアプリケーションからファイルを選択します。 今すぐドロップボックスからファイルを選択すると、onActivityResult(int requestCode, int resultCode, Intent data)にあります。コンテンツ://com.dropbox.android.FileCache/filecache/2642b6eb-f9da-4775-b6c5-6cb4d6884018 uri from intent。今、このuriから実際の道を得るには? このファイルを自分のアクティビティに添付します。また、私はGoogleドライブのファイルの実際のパスを取得することができません。

+0

は、ファイルパスを使用しない、代わりに 'InputStream'使用 - あなたがたInputStream Iを使用して意味して詳細 – pskink

+0

@pskinkのための' ContentResolver'のドキュメントを参照してくださいそのファイルを自分のSDカードに書き込み、そのSDカードのファイルパスを使用します。 –

+0

なぜファイルパスが必要ですか? 'InputStream'を使うことはできませんか? (または 'ParcelFileDescriptor' /' FileDescriptor')? – pskink

答えて

-1

画像や動画をリアルタイムパスの使用を取得するには:

protected String getPhotoUriRealPath(Uri contentURI) { 
    String path = ""; 
    if (contentURI == null) { 
     return path; 
    } 
    Cursor cursor = getContentResolver().query(contentURI, null, null, null, null); 

    if (cursor == null) { 
     path = contentURI.getPath(); 
    } else { 
     cursor.moveToFirst(); 
     int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
     path = cursor.getString(idx); 

    } 
    if (cursor != null) { 
     cursor.close(); 
    } 
    return path; 
} 

protected String getVideoUriRealPath(Uri contentURI) { 
    String path = ""; 
    if (contentURI == null) { 
     return path; 
    } 
    Cursor cursor = getContentResolver().query(contentURI, null, null, null, null); 

    if (cursor == null) { 
     path = contentURI.getPath(); 
    } else { 
     cursor.moveToFirst(); 
     int idx = cursor.getColumnIndex(MediaStore.Video.VideoColumns.DATA); 
     path = cursor.getString(idx); 

    } 
    if (cursor != null) { 
     cursor.close(); 
    } 
    return path; 
} 
関連する問題