簡體   English   中英

如何通過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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM