从谷歌驱动器获取图像时,位图返回空

时间:2019-01-23 10:21:03

标签: android bitmap

我正在通过存储访问框架从Google驱动器中获取图像,并将其转换为位图。在运行在棉花糖上的银河A5上运行正常,但在运行Oreo位图的Redmi 6A上返回空。我的代码如下

 uri = resultData.getData();
 isImageFromGoogleDrive = "com.google.android.apps.docs.storage".equals(Objects.requireNonNull(uri).getAuthority());

  if(isImageFromGoogleDrive){
  InputStream inputStream;
  try {
     inputStream = getBaseContext().getContentResolver().openInputStream(uri);
     Bitmap bm = BitmapFactory.decodeStream(inputStream);

在Redmi 6A Oreo上bm的值为空

1 个答案:

答案 0 :(得分:0)

您可以这样做:

        if (resultCode == RESULT_OK && requestCode == SELECT_IMAGE_REQUEST_CODE) {
            Uri selectedImageUri = imageReturnedIntent.getData();
            try (InputStream inputStream = sender.getContentResolver().openInputStream(selectedImageUri )) {
               Files.copy(inputStream, "your_path_here", StandardCopyOption.REPLACE_EXISTING);
            } catch (IOException ex) {
               Toast.makeText(sender, "A problem has occurred while importing image", Toast.LENGTH_SHORT).show();
               Log.e(TAG, "error copying files:" + ex.getMessage());
               return;
            }
        }

您不能像从SDCARD上获取一张图像那样仅仅获取图像,因此您需要使用Streams将该文件复制到内存中(无论是否临时)。这段代码会将图像复制到路径,然后您可以使用它来做您想做的事情。