如何通过Messenger发送来自应用的图片?

我想通过Messenger从我的应用发送图像。我一直在寻找,并且找到了适用于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"));

}

回答:

在此花费大量时间后:

ImageView imageView = findViewById(R.id.image);

Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();

//save the image now:

saveImage(bitmap);

//share it

send();

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

}

}

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

}

}

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

以上是 如何通过Messenger发送来自应用的图片? 的全部内容, 来源链接: utcz.com/qa/421095.html

回到顶部