首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android:从意图访问内容:// file

Android:从意图访问内容:// file
EN

Stack Overflow用户
提问于 2018-08-12 14:04:03
回答 1查看 729关注 0票数 0

在我的Android中,我授权用户与我的应用程序共享一个压缩文件(在大多数情况下,用户将只使用Dropbox进行共享)。

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

我用的是这个代码:

代码语言:javascript
复制
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..“

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

代码语言:javascript
复制
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();
            }
        }
    }

那么,如何处理/处理内容://文件呢?

谢谢你的帮助!!

EN

回答 1

Stack Overflow用户

发布于 2018-08-12 14:13:30

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

那是因为它不是文件。类似地,Android: Access content:// file from intent不是一个文件。

那么,如何处理/处理内容://文件呢?

使用ContentResolver ( getContentResolver() on a Context)和openInputStream()获得Uri标识的内容的InputStream

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51809549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档