在我的Android中,我授权用户与我的应用程序共享一个压缩文件(在大多数情况下,用户将只使用Dropbox进行共享)。
因此,我花了几个小时的时间试图弄清楚如何访问我的应用程序从Dropbox接收到的内容:// file。
我用的是这个代码:
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();
}
}
}那么,如何处理/处理内容://文件呢?
谢谢你的帮助!!
发布于 2018-08-12 14:13:30
但是如果我试图以正常方式访问该文件,它将无法工作。
那是因为它不是文件。类似地,Android: Access content:// file from intent不是一个文件。
那么,如何处理/处理内容://文件呢?
使用ContentResolver ( getContentResolver() on a Context)和openInputStream()获得Uri标识的内容的InputStream。
https://stackoverflow.com/questions/51809549
复制相似问题