Android: Access content:// file from intent

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Android: Access content:// file from intent



In my Android App I authorise the user to share a zip-file with my application (in most cases the user will only use Dropbox for sharing).



So I tried for hours to figure out how to access the content:// file my App is receiving from Dropbox.



I'm using this code:


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;



In dataUriString we have something like:



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



But if I try to access the file as a normal it won't work:


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();





So how do I have to work/handle content:// files?



Thanks for any help!!




1 Answer
1



But if I try to access the file as a normal it won't work



That is because it is not a file. Similarly, https://stackoverflow.com/questions/51809549/android-access-content-file-from-intent is not a file.


https://stackoverflow.com/questions/51809549/android-access-content-file-from-intent



So how do I have to work/handle content:// files?



Use ContentResolver (from getContentResolver() on a Context) and openInputStream() to get an InputStream on the content identified by the Uri.


ContentResolver


getContentResolver()


Context


openInputStream()


InputStream


Uri






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard