user180825
user180825

Reputation:

Attaching an image to an email

Writing an app which takes a picture from the camera and retrieves it as a bitmap. I then write this file to the disk and attempt to send the picture as an email, in the gmail app it says there is an attachment however when I receive the email no file is attached. Here are the two appropriate methods I am trying to get working.

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

    }
    };

Upvotes: 2

Views: 904

Answers (2)

user180825
user180825

Reputation:

First needed to access the SD Card, then find it and write to it and grab the URI of the file.

Here is the functioning code:

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

Upvotes: 1

jtt
jtt

Reputation: 13541

I had a colleague who had this problem, it would NEVER create the file, ensure that the files does exist when you are trying to send it and along with this make sure you have the permission's necessary to access the disk drive.

Upvotes: 0

Related Questions