如何通过 Instagram、Whatsapp 或任何社交应用程序从您自己的应用程序共享图像文件?

How to share the image file from your own app through Instagram, Whatsapp or maybe any social app?

我想用你自己的应用程序将图像分享给所有“Instagram”、“WhatsApp”或可能使用任何社交媒体应用程序?

当我得到whatsapp的解决方案时,我仍然无法得到Instagram的解决方案。

我的代码如下:

share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
            Bitmap bitmap = drawable.getBitmap();

            Intent share = new Intent(Intent.ACTION_SEND);
            share.setType("image/jpeg");
            share.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            ByteArrayOutputStream bytes = new ByteArrayOutputStream();

            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

            Random generator = new Random();
            int n = 10000;
            n = generator.nextInt(n);

            File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temp_"+n+".jpg");

            try {
                f.createNewFile();
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());
                fo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            share.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().getPath()));

            String savedFile = saveImageFile(bitmap, "myFolder");
            Uri imageUri =  Uri.parse(savedFile);

            share.putExtra(Intent.EXTRA_STREAM, imageUri);
            startActivity(Intent.createChooser(share, "Share Image"));
        }
    });
}

savedImages 方法:-

public static String saveImageFile(Bitmap image, String folder){

        String now = Long.toString(new Date().getTime());

        File imageFile = new File(Environment.getExternalStorageDirectory()+"/" + folder);
        if (!imageFile.exists()){
            File screenShotsFolder = new File(Environment.getExternalStorageDirectory()+"/"+ folder+ "/");
            screenShotsFolder.mkdirs();
        }

        File imageName = new File(new File(Environment.getExternalStorageDirectory() +"/"+ folder + "/"), now + ".jpg" );

        try{
            FileOutputStream outputStream = new FileOutputStream(imageName);
            image.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            outputStream.flush();
            outputStream.close();
        }catch (Throwable e){e.printStackTrace();}
        return imageName.toString();
    }

此代码适用于“WhatsApp”,但不适用于“Instagram

在 Instagram 中,我无法发送给特定的人,例如“9gag”。

使用,

  share.putExtra(Intent.EXTRA_STREAM, Uri.parse(f.getPath()));

而不是Environment.getExternalStorageDirectory().getPath())

但对于牛轧糖及更高版本,您应该使用 FileProvider 来获取文件 uri..

  picUri = FileProvider.getUriForFile(RegisterActivity.this , this.getApplicationContext().getPackageName() + ".provider", f);

这是完整的代码。将您的代码更改为以下内容

share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
            Bitmap bitmap = drawable.getBitmap();

            Intent share = new Intent(Intent.ACTION_SEND);
            share.setType("image/jpeg");

            ByteArrayOutputStream bytes = new ByteArrayOutputStream();

            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

            String savedFile = saveImageFile(bitmap, "myFolder");

            Uri imageUri =  Uri.parse(savedFile);
            share.putExtra(Intent.EXTRA_STREAM, imageUri);
            startActivity(Intent.createChooser(share, "Share Image"));

        }
    });

保存图片文件功能

public static String saveImageFile(Bitmap image, String folder){

    String now = Long.toString(new Date().getTime());

    File imageFile = new File(Environment.getExternalStorageDirectory() + folder);
    if (!imageFile.exists()){
        File screenShotsFolder = new File("/sdcard/Pictures/" + folder+ "/");
        screenShotsFolder.mkdirs();
    }

    File imageName = new File(new File("/sdcard/Pictures/" + folder + "/"), now + ".jpg" );

    try{
        FileOutputStream outputStream = new FileOutputStream(imageName);
        image.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        outputStream.flush();
        outputStream.close();
    }catch (Throwable e){e.printStackTrace();}
    return imageName.toString();
}

更喜欢这个 File share in Naught or above (API level 28 or above)