将图像附加到电子邮件
问题描述:
编写从相机拍摄照片并将其作为位图检索的应用程序。然后我把这个文件写到磁盘上,然后尝试将图片作为电子邮件发送,在gmail应用中它说有一个附件,但是当我收到电子邮件时没有附加任何文件。以下是我尝试工作的两种适当方法。将图像附加到电子邮件
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.photoResultView);
image.setImageBitmap(thumbnail);
try {
pic = new File("pic");
FileOutputStream out = new FileOutputStream(pic);
thumbnail.compress(CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
catch (java.io.FileNotFoundException e) {
// that's OK, we probably haven't created it yet
}
catch (Throwable t) {
Toast
.makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG)
.show();
}
sendPictureButton.setVisibility(Button.VISIBLE);
}
}
private OnClickListener sendPictureButtonListener = new OnClickListener() {
@Override
public void onClick(View arg0){
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
i.putExtra(Intent.EXTRA_SUBJECT,"On The Job");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));
i.setType("image/png");
startActivity(Intent.createChooser(i,"Share you on the jobing"));
}
};
答
首先需要访问SD卡,然后找到它并写入并获取文件的URI。
下面是功能代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.photoResultView);
image.setImageBitmap(thumbnail);
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
pic = new File(root, "pic.png");
FileOutputStream out = new FileOutputStream(pic);
thumbnail.compress(CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
} catch (IOException e) {
Log.e("BROKEN", "Could not write file " + e.getMessage());
}
sendPictureButton.setVisibility(Button.VISIBLE);
}
}
private OnClickListener takePictureButtonListener = new OnClickListener() {
@Override
public void onClick(View arg0){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
};
private OnClickListener sendPictureButtonListener = new OnClickListener() {
@Override
public void onClick(View arg0){
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
i.putExtra(Intent.EXTRA_SUBJECT,"On The Job");
//Log.d("[email protected][email protected]#!#[email protected]##!", Uri.fromFile(pic).toString() + " " + pic.exists());
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));
i.setType("image/png");
startActivity(Intent.createChooser(i,"Share you on the jobing"));
}
};
答
我有一个同事谁了这个问题,它永远不会创建该文件,确保文件确实,当你试图把它存在与此相伴请确保您有权限的需要访问磁盘驱动器。
谢谢你使我在正确的方向 – NickTFried 2011-02-08 02:51:44