不支持文件格式 - 在WhatsApp上发送照片时


        //Share image to all
        share = (Button)findViewById(R.id.facebook_app_id);
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri imageUri = Uri.parse("android.resource://" + getPackageName() +"/drawable/"+imageRes);
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("image/*");
                intent.putExtra(Intent.EXTRA_STREAM, imageUri);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                startActivity(Intent.createChooser(intent , "Share"));

            }
        });

我正在尝试构建一个照片共享应用程序。Facebook,Messenger,Skype完美工作,但 WhatsApp和Viber 显示错误(The file format is not supported(

尝试将共享意图中的内容类型设置为 image/png 。如上所述,这可能对所有应用程序都可能不起作用。

原因是您无法直接从应用程序内部存储中共享URI(当然,应用程序的资源将始终在内部存储中(

有两种实现这一目标的方法。

将图像复制到外部存储,然后从那里共享。看到这个

写一个内容提供商以共享图像。为此,请参考内部存储中创建并共享文件

这是一个示例如何做到这一点:

public void shareImageWhatsApp() {
    Bitmap adv = BitmapFactory.decodeResource(getResources(), R.drawable.adv);
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpeg");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    adv.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    File f = new File(Environment.getExternalStorageDirectory()
            + File.separator + "temporary_file.jpg");
    try {
        f.createNewFile();
        new FileOutputStream(f).write(bytes.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
    share.putExtra(Intent.EXTRA_STREAM,
            Uri.parse( Environment.getExternalStorageDirectory()+ File.separator+"temporary_file.jpg"));
    if(isPackageInstalled("com.whatsapp",this)){
          share.setPackage("com.whatsapp"); 
          startActivity(Intent.createChooser(share, "Share Image"));
    }else{
        Toast.makeText(getApplicationContext(), "Please Install Whatsapp", Toast.LENGTH_LONG).show();
    }
}
private boolean isPackageInstalled(String packagename, Context context) {
    PackageManager pm = context.getPackageManager();
    try {
        pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (NameNotFoundException e) {
        return false;
    }
}

我不确定,但是您可以检查带有扩展的成像,例如.jpeg或png例如android.resource://" getpackagename(( "/drawable/" " imageres.jpg"

尝试以下:

 Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_round);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("image/*");
    String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, "ic_launcher_round", null);
    Uri imageUri = Uri.parse(path);
    intent.putExtra(Intent.EXTRA_STREAM, imageUri);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(intent , "Share"));

在执行此代码之前,请确保您必须授予External_Storage和TARGET和TARGET并编译SDK高于Lolipop,则必须请求许可

最新更新