如何通过信使从应用程序发送图像?

How to send image from app via messenger?

我想通过 Messenger 从我的应用程序发送图片。我在查看 Stack Overflow 时发现 answer 适用于 WhatsApp。当我尝试将 "com.whatsapp" 更改为 "com.facebook.orca" 时,它停止工作。这是我的代码:

public void shareImageMessenger() {
            Bitmap adv = BitmapFactory.decodeResource(getResources(), R.drawable.koza);
            Intent share = new Intent(Intent.ACTION_SEND);
            share.setType("image/jpeg");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            adv.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            File f = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "temporary_file_1.jpg");
            try {
                f.createNewFile();
                new FileOutputStream(f).write(bytes.toByteArray());
            } catch (IOException e) {
                e.printStackTrace();
            }
            share.putExtra(Intent.EXTRA_STREAM,
                    Uri.parse( Environment.getExternalStorageDirectory()+ File.separator+"temporary_file_1.jpg"));
            share.setPackage("com.facebook.orca");
            startActivity(Intent.createChooser(share, "Share Image"));
        }

在这上面花了很多时间之后:

检查是否给了权限。那么:

第 1 步:在 activity 中创建您想要的图像的 ImageView,然后将其转换为无位图

ImageView imageView = findViewById(R.id.image);
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();

//save the image now:
saveImage(bitmap);
//share it
send();

第 2 步:将图像存储在内部文件夹中:

private static void saveImage(Bitmap finalBitmap) {

    String root = Environment.getExternalStorageDirectory().getAbsolutePath();
    File myDir = new File(root + "/saved_images");
    Log.i("Directory", "==" + myDir);
    myDir.mkdirs();

    String fname = "Image-test" + ".jpg";
    File file = new File(myDir, fname);
    if (file.exists()) file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

第 3 步:发送保存的图片:

public void send() {
    try {
        File myFile = new File("/storage/emulated/0/saved_images/Image-test.jpg");
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        String ext = myFile.getName().substring(myFile.getName().lastIndexOf(".") + 1);
        String type = mime.getMimeTypeFromExtension(ext);
        Intent sharingIntent = new Intent("android.intent.action.SEND");
        sharingIntent.setType(type);
        sharingIntent.putExtra("android.intent.extra.STREAM", Uri.fromFile(myFile));
        startActivity(Intent.createChooser(sharingIntent, "Share using"));
    } catch (Exception e) {
        Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

现在,如果您不想在存储中保存图片,可以在发送后将其删除。检查其他 link 来做到这一点。

引用您的链接 post,您可以修改共享意图。

 Intent share = new Intent(Intent.ACTION_SEND);
 share.setType("image/*");
 share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///assets/epic/adv.png"));
 this.startActivity(Intent.createChooser(share, "share_via"));

意图启动处理 Intent.ACTION_SEND 的应用程序。如果你想让特定的应用程序响应,请确保你知道包名称并且你需要设置包名称 share.setPackage("");