【问题标题】:Android share image Intent: WhatsApp - The file format is not supportedAndroid 共享图像意图:WhatsApp - 不支持文件格式
【发布时间】:2016-10-16 10:50:49
【问题描述】:

我在将图像从我的应用程序共享到 WhatsApp 时遇到问题。

int imageId = getResources().getIdentifier("image_name", "drawable", getPackageName());

Uri imageUri = Uri.parse("android.resource://com.companyname.packagename/drawable/"+ imageId);

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");                       
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));

此代码适用于 Facebook Messenger 或内置 Messenger 的 Android。 但它不适用于 WhatsApp。我收到此错误消息:

“不支持文件格式!”

我已经通过使用@CommonsWare 解决方案解决了这个问题: https://github.com/commonsguy/cwac-provider

【问题讨论】:

  • 许多应用程序在android.resource Uri 值方面存在问题,因为它们不常见、出乎意料,因此经常未经测试。
  • 试试shareIntent.setType("image/*");
  • @CommonsWare 感谢您的回答。有没有其他方法可以在不使用android.resource 的情况下获取我的图像文件的路径?
  • 你可以将这个drawable移动到res/raw/assets/,然后使用我的StreamProvider来使用content:Uri:github.com/commonsguy/cwac-provider或者,将图像复制到文件上internal storage,然后use FileProvider to serve it using a content: Uri
  • @CommonsWare 谢谢 :) 我会试试你的 CWAC-Provider。

标签: android image android-intent share whatsapp


【解决方案1】:

即使在 Android 11 上,这对我也有效。它基于以下 Android 文档:https://developer.android.com/training/data-storage/shared/media。在我的用例中,我需要共享通过将布局转换为位图而生成的图像。我必须先将图像保存到共享媒体存储中,但我相信私有存储也应该可以工作。

public void shareImage(Bitmap bitmap) {
    Uri contentUri;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        contentUri = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
    } else {
        contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    }
    
    ContentResolver contentResolver = getApplicationContext().getContentResolver();
    ContentValues newImageDetails = new ContentValues();
    newImageDetails.put(MediaStore.Images.Media.DISPLAY_NAME, "filename");
    Uri imageContentUri = contentResolver.insert(contentUri, newImageDetails);

    try (ParcelFileDescriptor fileDescriptor =
                 contentResolver.openFileDescriptor(imageContentUri, "w", null)) {
        FileDescriptor fd = fileDescriptor.getFileDescriptor();
        OutputStream outputStream = new FileOutputStream(fd);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, bufferedOutputStream);
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
    } catch (IOException e) {
        Log.e(TAG, "Error saving bitmap", e);
    }

    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_STREAM, imageContentUri);
    sendIntent.putExtra(Intent.EXTRA_TEXT, "some text here");
    sendIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    sendIntent.setType("image/*");
    Intent shareIntent = Intent.createChooser(sendIntent, "Share with");
    startActivity(shareIntent);
}

【讨论】:

    【解决方案2】:

    替换shareIntent.setType("image/jpeg");

    有了这个shareIntent.setType("image/*");,它将选择所有类型的图像,也许这对你有用,因为它对我来说很好。

    【讨论】:

      【解决方案3】:
      Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
      shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
      shareIntent.setType("image/*");
      //set your message
      shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, msgText); 
      
      String imagePath = Environment.getExternalStorageDirectory() + File.separator + "image_name.jpg";
      
      File imageFileToShare = new File(imagePath);
      
      Uri uri = Uri.fromFile(imageFileToShare);
      
      shareIntent.putExtra(Intent.EXTRA_STREAM, uri);`
      
      try { // should you to check Whatsapp is installed or not
           startActivity(shareIntent)
      }
      catch (android.content.ActivityNotFoundException exception) {
             showMessage("Whatsapp have not been installed")
      }
      

      【讨论】:

      • 不适用于 Android 11(API 级别 30)。如何解决 Android R 不支持文件格式的问题。
      • @SaurabhGaddelpalliwar 你找到解决办法了吗?
      • 是的,我找到了解决方案
      • 您必须使用以下代码在 Android 11 上保存图像,filesDir = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), ConstantString.IMAGE_FILE_SHARE_PATH_FULL);并使用此生成的路径在 WhatsApp 上共享图像。
      • 将此用于 Android 11 , String mediaPath = MediaStore.Images.Media.insertImage(mActivity.getContentResolver(), result, newFile.getName(), null); imageInternalUri = Uri.parse(mediaPath);
      【解决方案4】:
      shareIntent.setType("image/jpeg"); 
      

      替换为shareIntent.setType("image/*"); //it support all type of files.

      【讨论】:

      • 对于所有类型的文件,您需要 shareIntent.setType(" * / * ")。星号之间没有空格
      【解决方案5】:
      mIntent.setType("image/png");
      

      用它替换这个。它可能会起作用。

      【讨论】:

      • 这不起作用。如果我使用 png 图像并将代码更改为 shareIntent.setType("image/png"); ,则会发生相同的错误。
      猜你喜欢
      • 2021-11-18
      • 2016-12-09
      • 1970-01-01
      • 2018-01-26
      • 2020-06-16
      • 2015-03-20
      • 2012-05-05
      • 1970-01-01
      • 2014-09-05
      相关资源
      最近更新 更多