如何通过Messenger发送来自应用的图片? [英] How to send image from app via messenger?

查看:459
本文介绍了如何通过Messenger发送来自应用的图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to send image from my app via messenger. I was looking on Stack Overflow and I found answer which works for WhatsApp. When I tried to change "com.whatsapp" to "com.facebook.orca", it stops working. Here is my code:

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:在活动中创建您想要的图像的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();
    }
}

现在,发送后,如果不想在存储中保存图像,可以将其删除.检查其他链接可以做到这一点.

Now after sending you can delete the saved image if you don't want it in your storage. Check other link to do that.

这篇关于如何通过Messenger发送来自应用的图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆