Android 如何让我的共享意向支持whatsapp和谷歌+;

Android 如何让我的共享意向支持whatsapp和谷歌+;,android,android-intent,Android,Android Intent,我正在使用此代码共享图像: File file = ImageLoader.getInstance().getDiskCache().get(imageUrl); if (file != null && file.exists()) { Uri uri = Uri.fromFile(file); Intent intent = new Intent(Intent.ACTION_SEND);

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

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表示“不支持这种格式”。但是,如果我想下载文件夹并通过Gallery应用程序共享,Google+和Whatsapp都会选择相同的图像。

你可以尝试将文件保存到外部缓存,这对我很有用。带有滑动的
示例

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 {

                }
            }
        });
Intent sendIntent=newintent();
sendIntent.setAction(Intent.ACTION\u SEND);
sendIntent.setType(“image/*”);
Glide.with(getContext())
.加载(“http://...url.here...")
.asBitmap()
.into(新的SimpleTarget(500500){
@凌驾
public void onResourceReady(位图资源、GlideAnimation){
试一试{
File File=新文件(getContext().getExternalCacheDir(),“File_to_share.png”);
文件.getParentFile().mkdirs();
FileOutputStream out=新的FileOutputStream(文件);
压缩(Bitmap.CompressFormat.PNG,90,out);
out.close();
sendIntent.putExtra(Intent.EXTRA_流,Uri.fromFile(文件));
sendIntent.addFlags(Intent.FLAG\授予\读取\ URI\权限);
getContext().startActivity(Intent.createChooser(sendIntent,“”);
}捕获(IOE异常){
Log.e(“Share”,e.getMessage(),e);
}最后{
}
}
});

如果您使用的是Universal Image Loader,我已申请保存图像,并在用户从共享返回后立即将其删除:

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();
         }
 }
要删除该文件,只需覆盖ActivityResult上的
,它将在共享后立即被删除

@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);
        }
     }

是否保存到外部缓存?用户共享后该文件会发生什么情况?它是一个缓存,因此在需要时,系统将删除它。与内部缓存相同。您可以添加一个方法来清理早于X的文件,并在生命周期中的某个地方调用它,如
onPause
。谢谢。实际上,当用户从共享中返回时,我会立即删除该图像