【问题标题】:How to send image from app via messenger?如何通过信使从应用程序发送图像?
【发布时间】:2018-10-29 01:23:06
【问题描述】:

我想通过信使从我的应用程序发送图像。我在查看 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"));
        }

【问题讨论】:

  • 错误是什么
  • 出了点问题!请稍后再试!或者它只是加载而没有任何反应。
  • logcat崩溃时检查一下..或者尝试抛出异常,以便我们知道问题所在然后解决它
  • 您在执行此操作的设备上是否安装了 messenger?
  • 是的,我有。我会尽快处理的,谢谢。

标签: java android facebook-messenger


【解决方案1】:

花了很多时间在这件事上:

检查是否已授予权限。然后:

第一步:在activity中创建你想要的图片的ImageView,然后转换成bitmap

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

现在,如果您不希望保存的图像在您的存储中,您可以在发送后删除它。检查其他链接以执行此操作。

【讨论】:

  • 它向我显示带有文本的 Toast 消息:pathname("file:////storage/emulated/0/saved_images/Image-test.jpg") 文件通过 ClipData.Item 暴露在应用程序之外.getUri()...
  • 您使用的是图像视图吗?在活动 xml 中添加它
  • 编辑:我通过插入“StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build());”解决了上述问题
  • 它适用于每个社交网络,除了信使......我认为有一些错误或类似的东西。谢谢阿拉哈斯亚
  • 它也适用于 Messenger。我已经测试过了。尝试重新安装 Messenger 并登录
【解决方案2】:

参考您的链接帖子,您可以修改分享意图。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-25
    • 2015-03-02
    • 2014-08-07
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 2017-11-30
    • 2014-12-25
    相关资源
    最近更新 更多