如何将可绘制图像附加到 Gmail?

2024-03-18

我正在尝试将 gridview 中的图像附加到 gmail 或 facebook,但是每当我尝试附加应用程序时就会崩溃,并且我收到以下带有空指针异常的错误,以下是我使用 gridview 图像选择的代码,有人可以帮忙吗?

public class Free_Cover_Activity extends AppCompatActivity
{
 GridView grid;
 int[] imageId = {
  R.drawable.discovercover,
  R.drawable.burpresswordfree,
  R.drawable.flyfree,
  R.drawable.cantmovefree,
  R.drawable.cantmovewordfree,
  R.drawable.chalkthisfree,
  R.drawable.fivehundredmetersfree,
  R.drawable.freeexercise,
  R.drawable.gym_smilie,
  R.drawable.hundredcalrairesfree,
  R.drawable.injuryfree,
  R.drawable.jumpropefree,
  R.drawable.nicesnathcfree,
  R.drawable.personglrecordfree,
  R.drawable.posefree,
  R.drawable.pushupfree,
  R.drawable.shoulder,
  R.drawable.timewordfree,
  R.drawable.unbrokernfree,
  R.drawable.weightbeltfree
 };

 private Bitmap mBitmap;
 private Intent email;

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.free_cover_gridview);
  android.support.v7.app.ActionBar abar =  getSupportActionBar();
  ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#D64365"));
  abar.setBackgroundDrawable(colorDrawable);
  abar.hide();

  CustomGridFreeCover adapter = 
               new CustomGridFreeCover(Free_Cover_Activity.this, imageId);
  grid=(GridView)findViewById(R.id.freecover_grid);
  grid.setAdapter(adapter);
  grid.setOnItemClickListener(new AdapterView.OnItemClickListener()
  {
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id)
   {
    try
    {
     Bitmap largeIcon = 
         BitmapFactory.decodeResource(getResources(), R.drawable.discovercover);

     /*
     replace "R.drawable.bubble_green" with the image resource 
     you want to share from drawable
     */
     ByteArrayOutputStream bytes = new ByteArrayOutputStream();
     largeIcon.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

     //you can create a new file name "test.jpg" in sdcard folder.
     File f = 
        new File(Environment.getExternalStorageDirectory() + File.separator + "test.jpg");
     f.createNewFile();

     //write the bytes in file
     FileOutputStream fo = new FileOutputStream(f);
     fo.write(bytes.toByteArray());

     //remember close de FileOutput
     fo.close();
    } catch (IOException e) {
     //TODO Auto-generated catch block
     e.printStackTrace();
    }

    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("image/jpeg");
    //set your subject
    shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hi"); 
    //set your message
    shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "How are you"); 

    String imagePath = Environment.getExternalStorageDirectory() 
           + File.separator + "test.jpg";
    File imageFileToShare = new File(imagePath);
    Uri uri = Uri.fromFile(imageFileToShare);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(shareIntent, "Share Image"));
   }
  });
 }

 public class CustomGridFreeCover extends BaseAdapter
 {
  private Context mContext;
  //private final String[] web;
  private final int[] Imageid;

  public CustomGridFreeCover(Context c,int[] Imageid )
  {
   mContext = c;
   this.Imageid = Imageid;
   //this.web = web;
  }

  @Override
  public int getCount()
  {
   //TODO Auto-generated method stub
   return Imageid.length;
  }

  @Override
  public Object getItem(int position) {
   //TODO Auto-generated method stub
   return null;
  }

  @Override
  public long getItemId(int position) {
   //TODO Auto-generated method stub
   return 0;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent)
  {
   //TODO Auto-generated method stub
   View grid;
   LayoutInflater inflater = (LayoutInflater) mContext
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null)
    {
     grid = new View(mContext);
     grid = inflater.inflate(R.layout.free_cover_griditem, null);
     //TextView textView = (TextView) grid.findViewById(R.id.grid_text);
     ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image_freecover);
     //textView.setText(web[position]);
     imageView.setImageResource(Imageid[position]);
    } else {
     grid = (View) convertView;
    }
   return grid;
  }
 }
}

这是您需要的工作代码:

首先保存图像Drawable to SD Card这是代码:

try{

Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.bubble_green);

            //replace "R.drawable.bubble_green" with the image resource you want to share from drawable 

            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            largeIcon.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

            // you can create a new file name "test.jpg" in sdcard folder.
            File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.jpg");

            f.createNewFile();

            // write the bytes in file
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());

            // remember close de FileOutput
            fo.close();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

然后获取保存的图像SD card并附加在电子邮件意图中,如下所示:

        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareIntent.setType("image/jpeg");

        shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hi"); //set your subject
        shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "How are you"); //set your message

        String imagePath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";

        File imageFileToShare = new File(imagePath);

        Uri uri = Uri.fromFile(imageFileToShare);

        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

        startActivity(Intent.createChooser(shareIntent, "Share Image"));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将可绘制图像附加到 Gmail? 的相关文章

随机推荐

  • IBM Worklight 6.0 - 构建问题

    我只是运行一个测试应用程序 我的构建失败并显示以下日志 我尝试过重新启动 更改工作区 创建测试应用程序 但没有成功 请告诉我 ipad build failed Cannot overwrite template file Users ms
  • 如何删除magento中产品图片的缓存url

    对于我的所有 magento 产品图像 我从缓存 url 获取图像 如何禁用它并使我的产品图像使用原始 url 我已在 public html dirname app code core Mage Catalog Helper image
  • Next.js 动态路由在部署时无法正常工作

    我使用 Next js 构建了一个网站 其中有以下文件夹结构 pages path index js for students path index js index js events js 在本地开发中一切都运行良好 动态路由使用get
  • 使用 Java 在 Selenium WebDriver 中聚焦元素的正确方法

    相当于什么selenium focus 对于网络驱动程序 element sendKeys or new Actions driver moveToElement element perform 我已经尝试过它们并且它们都有效 但是哪一个总
  • 为什么 std::function 不能接受推导类型作为其模板参数?

    include
  • Corda 企业节点上的 PostgreSQL 引发关系错误

    在 docker 容器中使用 PostgreSQL 运行 corda enterprise 我已按照文档中的说明进行操作并设置了数据库架构 在数据库启动时 我看到以下错误 任何人都可以帮助那里发生了什么事吗 2018 10 11 06 57
  • ASP.Net MVC5 和 StructureMap4 - 简化方法

    在整合的同时结构图 MVC5 https www nuget org packages StructureMap MVC5 到一个 ASP Net MVC5 Web 应用程序 意识到它使用 3 1 版本的 SM 而不是 4 然后尝试获取此
  • Swift:按下 UITabBarItem 时如何执行操作

    目前我有一个连接到表格视图控制器的选项卡栏控制器 当我按下标签栏项目时 我试图转到表格视图的顶部 我知道如何到达桌面视图的顶部 我只是不知道按下该项目时如何执行操作 你应该使用UITabBarDelegate用方法didSelectItem
  • 如何正确使用 codeigniter 发送电子邮件的方式

    嘿伙计 我正在尝试使用 codeigniter 邮件类函数发送电子邮件 但我发现 smtp 协议有问题 我使用gmail smtp协议 我在本地机器上运行这个 我正在使用 Xampp 1 7 4 包 并且我尝试过如下设置 function
  • .htaccess 将图像从旧文件夹重定向到新文件夹

    我刚刚从 Drupal Wordpress 迁移到完全用 WordPress 构建的网站 我有一组图像 其中文件不再存在 需要尝试将所有图像保留在一个文件夹中 如果可能 我需要发送对任何 gif png jpg 的请求http www do
  • Innosetup 添加多个 exe 文件并在主设置中执行

    我想知道如何添加额外的安装 exe 并使用 innosetup 与我的主 exe 一起执行额外的 exe 请帮助我 因为我在过去 3 天里一直在尝试这个 因为我是 innosetup 的新手 谢谢 最简单的方法是在 Run 部分调用附加的
  • CouchDB 文档更新处理程序(就地更新)

    http wiki apache org couchdb Document Update Handlers http wiki apache org couchdb Document Update Handlers CouchDB 0 10
  • 如何切换不同版本的gem安装?

    我在本地计算机上安装了三个版本的机架 rack 1 4 1 1 3 6 1 3 5 对于某些宝石 例如Cucumber 它需要较低版本rack被激活 我尝试过bundle但也没有什么好处 执行时 cucumber仍将使用激活的机架版本1 4
  • 反汇编Java字节码的Java程序[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我最近正在阅读有关字节码分析的内容 我需要以下查询的帮助 如果我想编写一个反汇编 Java 字节码 通过读取类文件 并打印操作码的 J
  • 如何在 MVC 4 中实现自定义 OpenID 依赖方

    我喜欢新的 MVC OpenID OAuth 登录功能 但我想知道如何添加新的登录按钮 例如我希望我的用户使用他们的 StackExchange 帐户或使用他们的 OpenID url 登录 就像在 stackoverflow 中一样htt
  • Fiddler 重新发行以及作曲家编辑和重新发行

    我在日常生活中使用 Fiddler 然而 对我来说最常用的功能 例如Reissue and Edit and Reissue from composer没有任何捷径 我不知道如何为此使用 fiddler 脚本 有人能指出这个问题的解决方案吗
  • 如何使用 Material ui Reactjs 禁用今天日期中的过去日期?

    我正在使用 React Material ui 创建日期范围选择器 我此功能背后的逻辑是选择所需日期 如果已选择所需日期 则禁用所选日期中的所有过去日期 如何实现这个react材质ui 这是我的代码 import React from re
  • struts 2将属性标签的值分配给隐藏字段

    我想将字段描述中的值分配给隐藏字段测试 但问题是 描述 包含单词序列 并且以下代码仅将第一个单词分配给 测试
  • 何时考虑 Solr

    我正在开发一个应用程序 需要通过搜索来做有趣的事情 包括全文搜索 命中突出显示 分面搜索等 该数据集可能有 3000 10000 条记录 每条记录有 20 30 个字段 并且全部存储在 MySQL 中 该网站的流量概况可能是中小型 所有这些
  • 如何将可绘制图像附加到 Gmail?

    我正在尝试将 gridview 中的图像附加到 gmail 或 facebook 但是每当我尝试附加应用程序时就会崩溃 并且我收到以下带有空指针异常的错误 以下是我使用 gridview 图像选择的代码 有人可以帮忙吗 public cla