首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android中获取没有Google集成的选择器意图Google驱动器文件文档

在Android中获取没有Google集成的选择器意图Google驱动器文件文档
EN

Stack Overflow用户
提问于 2018-05-25 08:37:33
回答 1查看 610关注 0票数 0

我正在开发一个安卓应用程序,它在google驱动器中调用getPickChooserIntent来获取一些文件,它可以很好地处理图像,但不能处理文档:

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

代码语言:javascript
复制
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(上下文)获取所选图像的URI将返回正确的URI。

代码语言:javascript
复制
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();
  }

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

这个URI可以很好地在ImageView中显示图像和图像,如果当时我选择了PDF或其他文档,那么它就不能工作了。

任何一个人有任何想法从谷歌驱动器获得意图数据,而没有谷歌驱动API集成?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-29 10:51:23

终于我做到了!

解决方案:

代码语言:javascript
复制
 @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");

            }
        }
    }

方法:

代码语言:javascript
复制
   @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();
            }
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50524948

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档