将图像附加到电子邮件

时间:2011-02-08 01:28:46

标签: android

编写一个从相机拍摄照片并将其作为位图检索的应用程序。然后我将此文件写入磁盘并尝试将图片作为电子邮件发送,在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[]{"fakeaddress@hello.edu"});
        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"));

    }
    };

2 个答案:

答案 0 :(得分:1)

首先需要访问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[]{"fake@fake.edu"});
        i.putExtra(Intent.EXTRA_SUBJECT,"On The Job");
        //Log.d("URI@!@#!#!@##!", 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"));
    }
    };

答案 1 :(得分:0)

我有一个遇到此问题的同事,它永远不会创建该文件,确保在您尝试发送文件时确实存在这些文件,并确保您具有访问磁盘驱动器所需的权限。< / p>