
我想通过信使从我的应用程序发送图像.我正在寻找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:在活动中创建您想要的图像的ImageVIEw,然后将其转换为位图
ImageVIEw imageVIEw = findVIEwByID(R.ID.image);Bitmap bitmap = ((BitmapDrawable) imageVIEw.getDrawable()).getBitmap();//save the image Now:saveImage(bitmap);//share itsend();第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(); }}现在发送后,如果您不想将其保存在存储中,则可以删除已保存的图像.检查其他链接即可.
总结以上是内存溢出为你收集整理的java – 如何通过messenger从app发送图像?全部内容,希望文章能够帮你解决java – 如何通过messenger从app发送图像?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)