获取选择选择器意图谷歌驱动器文件文件没有谷歌驱动器Api集成Android

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取选择选择器意图谷歌驱动器文件文件没有谷歌驱动器Api集成Android相关的知识,希望对你有一定的参考价值。

我正在开发一个android应用程序,它在谷歌驱动器中调用getPickChooserIntent来获取一些文件,它可以正常使用图像而不是文档:

注意:我不是在谈论谷歌驱动API实现。

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

从getPickImageChooserIntent(Context)获取所选图像的URI将返回正确的URI。

public static Uri getPickImageResultUri(@NonNull Context context, @Nullable Intent data) {
    boolean isCamera = true;
    if (data != null && data.getData() != null) {
      String action = data.getAction();
      isCamera = action != null && action.equals(MediaStore.ACTION_IMAGE_CAPTURE);
    }
    return isCamera || data.getData() == null ? getCaptureImageOutputUri(context) : data.getData();
  }

之后,我只找到像:content://com.google.android.apps.docs.storage.legacy/enc%3D8HXM7NIDpCn_Ax78iaO3nB9azNXlIu4AOk2poROCD8xP19KU%0A的URI

此URI适用于ImageView中的图像和显示图像,如果那时我选择PDF或其他文档,那么它不起作用。

任何人都有任何想法从谷歌驱动器获取意图数据没有谷歌驱动器API集成?

谢谢

答案

最后我做到了!

解:

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

以上是关于获取选择选择器意图谷歌驱动器文件文件没有谷歌驱动器Api集成Android的主要内容,如果未能解决你的问题,请参考以下文章

我是不是需要为以下谷歌驱动器范围提交验证请求才能从选择器下载文件?

如何在android中从谷歌驱动器中选择文件?

谷歌应用程序脚本,谷歌选择器团队驱动器

iCloud 文档选择器:应用文件夹在导入时被禁用

URL启动器与谷歌地图位置选择器冲突

谷歌驱动API的Android如何登录并从硬编码我的硬盘帐户注销无需用户交互?