简体   繁体   English

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

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

I wanted to share images with your own app to all " Instagram ", " WhatsApp " or maybe using any social media app? 我想使用自己的应用程序将图像共享到所有“ Instagram ”,“ WhatsApp ”,或者使用任何社交媒体应用程序?

As I got the solution for whatsapp, I still couldn't get the solution for Instagram. 当我获得whatsapp的解决方案时,我仍然无法获得Instagram的解决方案。

My code is as follows: 我的代码如下:

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 method:- 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();
    }

This code is working for " WhatsApp " but not for " Instagram " 此代码适用于“ WhatsApp ”,但不适用于“ Instagram

In Instagram, I am not able to send to particular person like " 9gag ". 在Instagram中,我无法发送给“ 9gag ”之类的特定人。

Use, 采用,

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

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

but for nougat and above you should use FileProvider to get the file uri.. 但对于牛轧糖及以上,则应使用FileProvider来获取文件uri。

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

Here is the complete code. 这是完整的代码。 Change your code to the following 将您的代码更改为以下内容

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"));

        }
    });

Saving Image file function 保存图像文件功能

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM