aFileChooser icon indicating copy to clipboard operation
aFileChooser copied to clipboard

Not Working for Google Drive Uri

Open ghost opened this issue 8 years ago • 4 comments
trafficstars

When user have selected file from Google drive, we cannot access original file path from that Uri. Working for Google Photos Uri. Not working when users have selected document/pdf file from Google Drive

ghost avatar Jul 30 '17 09:07 ghost

Any solution to this? or found any workaround on it?

amrutbidri avatar Jan 08 '18 10:01 amrutbidri

I couldn't found any. May be we need to manage by drive sdks.

I restricted user to choose from Drive though.

ghost avatar Jan 08 '18 10:01 ghost

I have created a pull request(#97) for this issue. Till then you can use codes in last 3 commits of this forked library: https://github.com/kk121/aFileChooser

kk121 avatar Jan 11 '18 10:01 kk121

I found a workaround for when file is not "local" (i.e. when FileUtils.getFile and FileUtils.getPath return null or FileUtils.isLocal is false), which is applicable for file selection from Drive #90, Google Photos #98, etc. The trick is to use a file descriptor instead of direct access, for example using something like this inside the usual onActivityResult():

try {

	final Uri uri = data.getData();
	final FileInputStream fileInputStream;
	final File file = FileUtils.getFile(this, uri);
	
	// Local file
	if (file != null) {
		fileInputStream = new FileInputStream(file);

	// Access to remote file (Google Photos, Drive, ...)
	} else {
		final ParcelFileDescriptor fileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
		if (fileDescriptor != null) {
			fileInputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
		} else {
			throw new IOException("Cannot open file descriptor from URI");
		}
	}

	// The following is just an example to test that the file is in fact readable
	final byte[] buffer = new byte[50];
	fileInputStream.read(buffer);
	fileInputStream.close();
	Toast.makeText(this, "Read: " + new String(buffer), Toast.LENGTH_LONG).show();

} catch (IOException e) {
	Toast.makeText(this, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
	e.printStackTrace();
}

lorenzos avatar Feb 19 '18 10:02 lorenzos