【问题标题】:How to make my share intent support whatsapp and google+如何让我的分享意图支持 whatsapp 和 google+
【发布时间】:2017-02-22 18:38:05
【问题描述】:

我正在使用此代码共享图像:

File file = ImageLoader.getInstance().getDiskCache().get(imageUrl);
            if (file != null && file.exists()) {
                Uri uri = Uri.fromFile(file);
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.putExtra(Intent.EXTRA_TEXT, "Hello");
                intent.putExtra(Intent.EXTRA_STREAM, uri);
                intent.setType("image/*");
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(Intent.createChooser(intent, "Send"));
            } else {
                Toast.makeText(context, "Image cannot be shared", Toast.LENGTH_SHORT).show();
            }

我之前使用UIL加载图像,所以mageLoader.getInstance().getDiskCache().get(imageUrl);从磁盘缓存返回图像文件。

Gmail、环聊、消息、云端硬盘等可以抓取文件,但在 Google+ 上,当 Whatsapp 说“不支持这种格式”时,抓取不到文件。但是,如果我 save the file 到 Downloads 文件夹并通过 Gallery 应用程序共享,Google+ 和 Whatsapp 都会选择相同的图像。

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    您可以尝试将文件保存到外部缓存,它对我有用。以Glide 为例:

    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.setType("image/*");
    
    Glide.with(getContext())
            .load("http://...url.here...")
            .asBitmap()
            .into(new SimpleTarget<Bitmap>(500, 500) {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                    try {
                        File file =  new File(getContext().getExternalCacheDir(), "file_to_share.png");
                        file.getParentFile().mkdirs();
                        FileOutputStream out = new FileOutputStream(file);
                        resource.compress(Bitmap.CompressFormat.PNG, 90, out);
                        out.close();
    
                        sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                        sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        getContext().startActivity(Intent.createChooser(sendIntent, ""));
                    } catch (IOException e) {
                        Log.e("Share", e.getMessage(), e);
                    } finally {
    
                    }
                }
            });
    

    【讨论】:

    • 保存到外部缓存?用户共享后文件会发生什么变化?
    • 是缓存,需要的时候系统会删除。与内部缓存相同。您可以添加一个方法来清理早于 X 的文件并在生命周期中的某个位置调用它,例如 onPause
    • 谢谢。实际上,一旦用户从共享中返回,我就会删除图像
    【解决方案2】:

    如果您使用的是通用图像加载器,我申请了the accepted answer 来保存图像并在用户从共享返回后立即将其删除:

    private File saveImage(String imageUri, String fileName) {
            File file = new File(this.getExternalCacheDir(), fileName);
            InputStream sourceStream = null;
            File cachedImage = ImageLoader.getInstance().getDiskCache().get(imageUri);
            if (cachedImage != null && cachedImage.exists()) {
                Log.d(TAG, "Cache exists");
                try {
                    sourceStream = new FileInputStream(cachedImage);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            } else {
                Log.d(TAG, "Cache doesn't exist");
            }
    
            if (sourceStream != null) {
                Log.d(TAG, "SourceStream is not null");
                try {
                    OutputStream targetStram = new FileOutputStream(file);
                    try {
                        try {
                            IoUtils.copyStream(sourceStream, targetStram, null);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } finally {
                        try {
                            targetStram.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        sourceStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                Log.d(TAG, "SourceStream is null");
                Toast.makeText(this, "Image cannot be shared", Toast.LENGTH_SHORT).show();
            }
            return file;
        }
    
     private void shareImage(String imageUrl, String fileName) {
             if (isSDReadableWritable()) {
                 file = saveImage(imageUrl, fileName);
                 Uri uri = Uri.fromFile(file);
                 Intent intent = new Intent(Intent.ACTION_SEND);
                 intent.putExtra(Intent.EXTRA_TEXT, "Hello");
                 intent.putExtra(Intent.EXTRA_STREAM, uri);
                 intent.setType("image/*");
                 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                 startActivityForResult(Intent.createChooser(intent, "Send"), 20);
             } else {
                 Toast.makeText(this, "Storage cannot be accessed", Toast.LENGTH_SHORT).show();
             }
     }
    

    要删除文件只需覆盖onActivityResult,共享后会立即删除

    @Override
         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == 20 && file != null) {
                boolean isDelete = file.delete();
                Log.d(TAG, "isDelete is " + isDelete);
            }
         }
    

    【讨论】:

      猜你喜欢
      • 2018-09-04
      • 2016-10-16
      • 2021-11-14
      • 2016-07-04
      • 2015-03-20
      • 2014-04-23
      • 1970-01-01
      • 2014-07-18
      • 1970-01-01
      相关资源
      最近更新 更多