Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get File from Google drive using Intent

I am trying to upload document from my app.

Everything working fine but when i choose file from drive.

data=Intent { act=android.intent.action.VIEW dat=content://com.google.android.apps.docs.storage.legacy/enc=ckpgt5KcEEF_JYniJQafRV_5pEnu_D5UAI1WF-Lu6h2Z_Vw4
     (has extras) }}

Can any body know how to handle this file.

I had already handle all files and images only facing problem with google drive files.

I am getting this content://com.google.android.apps.docs.storage.legacy/enc=ckpgt5KcEEF_JYniJQafRV_5pEnu_D5UAI1WF-Lu6h2Z_Vw4 in intent data Uri.

like image 845
sushildlh Avatar asked Dec 07 '18 13:12

sushildlh


People also ask

How do I download a file from Google Drive?

Files on Google drive can be shared between users, but the default access to the file is via a web browser graphical interface. However, sometimes it may be useful, or even necessary, to access and download a file from a command line, for example downloading the file with the wget utility.

What is Google Drive and how to use it?

Google Drive is an online storage space provided by Google. We can easily upload our files on the Internet and if it is not for our permission, other people can’t download the files we upload. Since the files are uploaded by ourselves, we can also download them at any time, just log in to our Google account.

How to share a test file with others on Google Drive?

First, we upload a test file we want to share with others on Google Drive. And then we need to set the sharing permission, right-click on the file you want to share and select Share. Then select the permission as Anyone on the internet can find and view.

How to share files on Google Drive with others using Wget?

For example, today we put the files that we want to share with others on Google Drive and we just need to write down a wget command as a shell script file, we can easily pass the lightweight shell script to others, and it was downloaded by others. First, we upload a test file we want to share with others on Google Drive.


1 Answers

Handle Uri received by Google-Drive files when selected through file chooser.

as stated earlier it receives Virtual File Uri.

I found this sample code simple and easy to understand. the given code sample worked for me .hope it works in your case.

1.So detect this Uri is received by google drive.

public static File getFileFromUri(final Context context, final Uri uri) throws Exception {

if (isGoogleDrive(uri)) // check if file selected from google drive
{ 
  return saveFileIntoExternalStorageByUri(context, uri);
}else 
    // do your other calculation for the other files and return that file
   return null;
}


public static boolean isGoogleDrive(Uri uri)
{
 return "com.google.android.apps.docs.storage.legacy".equals(uri.getAuthority());
}

2.if yes,the uri is stored to external path(here its root directory u can change it according to your need) and the file with that uri is created.

 public static File saveFileIntoExternalStorageByUri(Context context, Uri uri) throws 

Exception {
        InputStream inputStream = context.getContentResolver().openInputStream(uri);
        int originalSize = inputStream.available();

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        String fileName = getFileName(context, uri);
        File file = makeEmptyFileIntoExternalStorageWithTitle(fileName);
        bis = new BufferedInputStream(inputStream);
        bos = new BufferedOutputStream(new FileOutputStream(
                file, false));

        byte[] buf = new byte[originalSize];
        bis.read(buf);
        do {
            bos.write(buf);
        } while (bis.read(buf) != -1);

        bos.flush();
        bos.close();
        bis.close();

        return file;

    }

public static String getFileName(Context context, Uri uri) 
{
  String result = null;
  if (uri.getScheme().equals("content")) {
  Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
  try {
       if (cursor != null && cursor.moveToFirst()) {
        result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
       }
      } finally {
                cursor.close();
            }
        }
        if (result == null) {
            result = uri.getPath();
            int cut = result.lastIndexOf('/');
            if (cut != -1) {
                result = result.substring(cut + 1);
            }
        }
        return result;
}


public static File makeEmptyFileIntoExternalStorageWithTitle(String title) {
        String root =  Environment.getExternalStorageDirectory().getAbsolutePath();
        return new File(root, title);
    }

Note:Here the virtual file is retrieved from Intent getData() and used in context.getContentResolver().openInputStream(intent.getData()), this will return an InputStream. It's handle to get selected file from google drive.

for more info go through this link

like image 196
vikas Avatar answered Sep 23 '22 19:09

vikas