无需Android中的Google Drive Api集成,即可获取具有Pick Chooser意图的Google Drive文件文档

无需Android中的Google Drive Api集成,即可获取具有Pick Chooser意图的Google Drive文件文档,android,android-intent,document,google-drive-android-api,Android,Android Intent,Document,Google Drive Android Api,我正在开发一个android应用程序,它在google drive中调用GetPickChooserContent来获取一些文件,它可以处理图像,但不能处理文档: 注意:我不是在谈论GoogleDriveAPI的实现 public static Intent getPickChooserIntent( @NonNull Context context, CharSequence title, boolean includeDocuments, bo

我正在开发一个android应用程序,它在google drive中调用GetPickChooserContent来获取一些文件,它可以处理图像,但不能处理文档:

注意:我不是在谈论GoogleDriveAPI的实现

public static Intent getPickChooserIntent(
      @NonNull Context context,
      CharSequence title,
      boolean includeDocuments,
      boolean includeCamera) {

    List<Intent> allIntents = new ArrayList<>();
    PackageManager packageManager = context.getPackageManager();

    // collect all camera intents if Camera permission is available
    if (!isExplicitCameraPermissionRequired(context) && includeCamera) {
      allIntents.addAll(getCameraIntents(context, packageManager));
    }

    List<Intent> galleryIntents =
        getGalleryIntents(packageManager, Intent.ACTION_GET_CONTENT, includeDocuments);
    if (galleryIntents.size() == 0) {
      // if no intents found for get-content try pick intent action (Huawei P9).
      galleryIntents = getGalleryIntents(packageManager, Intent.ACTION_PICK, includeDocuments);
    }
    allIntents.addAll(galleryIntents);

    Intent target;
    if (allIntents.isEmpty()) {
      target = new Intent();
    } else {
      target = allIntents.get(allIntents.size() - 1);
      allIntents.remove(allIntents.size() - 1);
    }

    // Create a chooser from the main  intent
    Intent chooserIntent = Intent.createChooser(target, title);

    // Add all other intents
    chooserIntent.putExtra(
        Intent.EXTRA_INITIAL_INTENTS, allIntents.toArray(new Parcelable[allIntents.size()]));

    return chooserIntent;
  }
在这之后,我只找到类似这样的URI:
content://com.google.android.apps.docs.storage.legacy/enc%3D8HXM7NIDpCn_Ax78iaO3nB9azNXlIu4AOk2poROCD8xP19KU%0A

这个URI对于图像和在ImageView中显示图像工作正常,如果当时我选择了PDF或其他文档,那么它就不工作了

有人有没有想过在没有google drive API集成的情况下从google drive获取意向数据

谢谢

我终于做到了

解决方案:

 @Override
    @SuppressLint("NewApi")
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {


             Uri mUri = CropImage.getPickImageResultUri(this, data);

             writeStreamToFile(getApplicationContext(),mUri,".pdf");

            }
        }
    }
方法:

   @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    void writeStreamToFile(Context mContext, Uri mUri,String ext) {
        if (mUri == null) {
            return;
        }

        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = mContext.getContentResolver().query(mUri, filePathColumn, null, null, null);
        String mCurrentPath = null;
        if (cursor != null && cursor.moveToFirst()) {
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            mCurrentPath = cursor.getString(columnIndex);
            cursor.close();
        }

        if (TextUtils.isEmpty(mCurrentPath)) {
            mCurrentPath = mUri.getPath();
        }

        File storageDir = mContext.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
        InputStream inputStream = null;
        try {
            inputStream = mContext.getContentResolver().openInputStream(mUri);

            File file = File.createTempFile("pptFiles", ext, storageDir);

            try (OutputStream output = new FileOutputStream(file)) {
                byte[] buffer = new byte[4 * 1024]; // or other buffer size
                int read;
                while ((read = inputStream.read(buffer)) != -1) {
                    output.write(buffer, 0, read);
                }
                output.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

你在用不起作用的uri做什么?我只想在我们的应用程序中下载该文件。然后显示该代码;PdfView:PdfView.fromUri(mUri);McRopimageri?
   @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    void writeStreamToFile(Context mContext, Uri mUri,String ext) {
        if (mUri == null) {
            return;
        }

        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = mContext.getContentResolver().query(mUri, filePathColumn, null, null, null);
        String mCurrentPath = null;
        if (cursor != null && cursor.moveToFirst()) {
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            mCurrentPath = cursor.getString(columnIndex);
            cursor.close();
        }

        if (TextUtils.isEmpty(mCurrentPath)) {
            mCurrentPath = mUri.getPath();
        }

        File storageDir = mContext.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
        InputStream inputStream = null;
        try {
            inputStream = mContext.getContentResolver().openInputStream(mUri);

            File file = File.createTempFile("pptFiles", ext, storageDir);

            try (OutputStream output = new FileOutputStream(file)) {
                byte[] buffer = new byte[4 * 1024]; // or other buffer size
                int read;
                while ((read = inputStream.read(buffer)) != -1) {
                    output.write(buffer, 0, read);
                }
                output.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }