Java Android:从intent访问content://文件

Java Android:从intent访问content://文件,java,android,react-native,android-intent,android-contentprovider,Java,Android,React Native,Android Intent,Android Contentprovider,在我的Android应用程序中,我授权用户与我的应用程序共享zip文件(在大多数情况下,用户仅使用Dropbox进行共享) 因此,我花了几个小时试图弄清楚如何访问我的应用程序从Dropbox接收到的内容://文件 我正在使用以下代码: Intent intent = MainActivity.this.getIntent(); Uri dataUri = intent.getData(); Bundle bundle = new Bundle(); if(dataUri != null){

在我的Android应用程序中,我授权用户与我的应用程序共享zip文件(在大多数情况下,用户仅使用Dropbox进行共享)

因此,我花了几个小时试图弄清楚如何访问我的应用程序从Dropbox接收到的内容://文件

我正在使用以下代码:

Intent intent = MainActivity.this.getIntent();
Uri dataUri = intent.getData();
Bundle bundle = new Bundle();
if(dataUri != null){
  String dataUriString = dataUri.toString();
  File rootDataDir = MainActivity.this.getFilesDir();
  if(unzipFile(dataUriString, rootDataDir.toString())){
     //true
  }else{
     //false
  }
  bundle.putString("receive_data", dataUri.toString());
}
return bundle;
在dataUriString中,我们有如下内容:

"content://com.dropbox.android.FileCache/filecache/...contenID.."

但是,如果我尝试以正常方式访问该文件,它将无法工作:

public boolean unzipFile(String inputPath, String outputPath) {
        try{
            byte[] buffer = new byte[1024];
            File file = new File(inputPath);
            ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
            ZipEntry zipEntry = zis.getNextEntry();
            while(zipEntry != null){
                String fileName = zipEntry.getName();
                File newFile = new File(outputPath+ "/import/" + fileName);
                try (FileOutputStream fos = new FileOutputStream(newFile, false)){
                    int len;
                    while ((len = zis.read(buffer)) > 0) {
                        fos.write(buffer, 0, len);
                    }
                }
                zipEntry = zis.getNextEntry();
            }
            zis.closeEntry();
            zis.close();
            return true;
        }catch(IOException err){
            return false;
        }
    }


    private String getRealPathFromURI(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } catch (Exception e) {
            Log.e("ReactNative", "getRealPathFromURI Exception : " + e.toString());
            return "";
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
那么,我必须如何工作/处理content://文件呢

谢谢你的帮助

但是,如果我尝试以正常方式访问该文件,它将无法工作

这是因为它不是一个文件。同样,也不是一个文件

那么,我必须如何工作/处理content://文件呢


使用
ContentResolver
(在
Context
上的
getContentResolver()
)和
openInputStream()
Uri
标识的内容上获得
InputStream
,我看到您多次回答这个问题。我还没有找到一个关于如何实现这一点的例子。你能举个例子吗?假设我有一个
MediaPlayer
,我需要向它传递一个路径/uri,当从Dropbox中选择文件时,我将如何执行该操作?@HB.:“当从Dropbox中选择文件时,我将如何执行该操作?”--我不知道您使用的是什么API,当用户选择某个内容时,您将得到什么结果。如果您从Dropbox获得
内容://
Uri
,您可能可以直接使用它。否则,在您控制的某个文件上打开一个
FileOutputStream
(例如,在
getCacheDir()
中)。使用Dropbox中的内容获取Dropbox内容上的
InputStream
。将字节从
输入流
复制到
文件输出流
。然后,您可以使用您的文件副本处理需要文件的内容。“如果您从Dropbox获得
内容://
Uri
,您可能可以直接使用它”您能详细说明一下吗?我的
Uri
类似于这个问题
content://com.dropbox.android.FileCache/filecache/
@HB.:尝试将其传递到
setDataSource()
:您能检查一下我当前所做的是否正确吗?我添加了一些评论来解释我所做的事情——目前视频正在按预期播放,我只是想确定我所做的是否正确-