android - 如何将图片从 drawable 附加到 gmail?

标签 android android-intent bitmap gmail

我正在尝试将我的 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 保存到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 卡 中获取保存的图像并附加到电子邮件中,如下所示:

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

关于android - 如何将图片从 drawable 附加到 gmail?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31847086/

相关文章:

android - 如何在 Activity 过渡上执行淡入淡出动画?

android - 我如何获取用户选择的音频文件

java - 为什么单个静态变量在多次读取时有多个值?

java - 不幸的是 ,"App"已经停止,OnClickListener

java - 意图不起作用。第二个 Activity 启动错误

来自png文件的Android OpenGL ES 2.0纹理是黑色的

android - 加载图片出现OutOfMemoryException

c++ - 声明一个变量内联赋值C++

php - 通过mysql中的php从android存储以base64编码的图像

android - 接收位置更新