如何安装图像从绘制到Gmail吗? [英] How to attach image from drawable to gmail?
本文介绍了如何安装图像从绘制到Gmail吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从我的GridView控件到Gmail或Facebook的重视形象,但每当我试图连接我的应用程序得到了碰撞,而我得到在与空指针异常错误,下面是我的code与GridView的图像选择,任何一个可以帮助?
公共类Free_Cover_Activity扩展AppCompatActivity
{
GridView的网格;
INT [] imageId = {
R.drawable.discovercover,
R.drawable.bur presswordfree,
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
};
私人位图mBitmap;
私人意图电子邮件;
@覆盖
保护无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.free_cover_gridview);
android.support.v7.app.ActionBar ABAR = getSupportActionBar();
ColorDrawable colorDrawable =新ColorDrawable(Color.parseColor(#D64365));
abar.setBackgroundDrawable(colorDrawable);
abar.hide();
CustomGridFreeCover适配器=
新CustomGridFreeCover(Free_Cover_Activity.this,imageId);
格=(GridView控件)findViewById(R.id.freecover_grid);
grid.setAdapter(适配器);
grid.setOnItemClickListener(新AdapterView.OnItemClickListener()
{
@覆盖
公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长ID)
{
尝试
{
位图largeIcon =
BitmapFactory.de codeResource(getResources(),R.drawable.discovercover);
/ *
与图像资源替代R.drawable.bubble_green
你想从绘制分享
* /
ByteArrayOutputStream字节=新ByteArrayOutputStream();
largeIcon.com preSS(Bitmap.Com pressFormat.JPEG,40个字节);
//你可以创建一个新的文件名test.jpg放在SD卡中的文件夹。
文件f =
新的文件(Environment.getExternalStorageDirectory()+文件分割符+test.jpg放在);
f.createNewFile();
//写在文件中的字节
FileOutputStream中FO =新的FileOutputStream(F);
fo.write(bytes.toByteArray());
//记得接近德FileOutput
fo.close();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
意图shareIntent =新的意图(android.content.Intent.ACTION_SEND);
shareIntent.setType(为image / jpeg);
//设置你的主题
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,你好);
//设置你的消息
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,你好吗);
字符串的ImagePath = Environment.getExternalStorageDirectory()
+文件分割符+test.jpg放在;
文件imageFileToShare =新的文件(的ImagePath);
开放的我们的uri = Uri.fromFile(imageFileToShare);
shareIntent.putExtra(Intent.EXTRA_STREAM,URI);
startActivity(Intent.createChooser(shareIntent,共享图片));
}
});
}
公共类CustomGridFreeCover扩展了BaseAdapter
{
私人语境mContext;
//私人最终的String []网络;
私人最终诠释[] Imageid;
公共CustomGridFreeCover(上下文C中,int [] Imageid)
{
mContext = C;
this.Imageid = Imageid;
//this.web =网络;
}
@覆盖
公众诠释getCount将()
{
// TODO自动生成方法存根
返回Imageid.length;
}
@覆盖
公共对象的getItem(INT位置){
// TODO自动生成方法存根
返回null;
}
@覆盖
众长getItemId(INT位置){
// TODO自动生成方法存根
返回0;
}
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup)
{
// TODO自动生成方法存根
查看网格;
LayoutInflater充气=(LayoutInflater)mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
如果(convertView == NULL)
{
格=新景(mContext);
格= 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 [位置]);
} 其他 {
格=(查看)convertView;
}
回电网;
}
}
}
解决方案
下面是你需要的工作code:
首先从绘制对象
保存图像 SD卡
这里是code:
尝试{
位图largeIcon = BitmapFactory.de codeResource(getResources(),R.drawable.bubble_green);
//你想从绘制共享图像资源替代R.drawable.bubble_green
ByteArrayOutputStream字节=新ByteArrayOutputStream();
largeIcon.com preSS(Bitmap.Com pressFormat.JPEG,40个字节);
//你可以创建一个新的文件名test.jpg放在SD卡中的文件夹。
文件F =新的文件(Environment.getExternalStorageDirectory()+文件分割符+test.jpg放在);
f.createNewFile();
//写在文件中的字节
FileOutputStream中FO =新的FileOutputStream(F);
fo.write(bytes.toByteArray());
//记得接近德FileOutput
fo.close();
}
赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
然后得到 SD卡保存的图像
并附加在电子邮件的意图是这样的:
意图shareIntent =新的意图(android.content.Intent.ACTION_SEND);
shareIntent.setType(为image / jpeg);
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,你好); //设置你的主题
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,你好吗); //设置你的消息
串的ImagePath = Environment.getExternalStorageDirectory()+文件分割符+test.jpg放在;
文件imageFileToShare =新的文件(的ImagePath);
开放的我们的uri = Uri.fromFile(imageFileToShare);
shareIntent.putExtra(Intent.EXTRA_STREAM,URI);
startActivity(Intent.createChooser(shareIntent,共享图片));
I am trying to attach image from my gridview to gmail or facebook,but whenever i tried to attach my app got crash,and i am getting following error with nullpointer exception,following is my code with gridview image selection,can any one help?
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;
}
}
}
解决方案
Here is the working code which you need:
Firstly save image from Drawable
to SD Card
here is the code:
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();
}
Then get the saved image from SD card
and attach in the email intent like this:
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"));
这篇关于如何安装图像从绘制到Gmail吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文