Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Not Working for Google Drive Uri #90

Open
ghost opened this issue Jul 30, 2017 · 4 comments
Open

Not Working for Google Drive Uri #90

ghost opened this issue Jul 30, 2017 · 4 comments

Comments

@ghost
Copy link

ghost commented Jul 30, 2017

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

@amrutbidri
Copy link

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

@ghost
Copy link
Author

ghost commented Jan 8, 2018

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

I restricted user to choose from Drive though.

@kk121
Copy link

kk121 commented Jan 11, 2018

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

@lorenzos
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants