简体   繁体   中英

Email Intent sends text file rather than image

This the code I am using to take picture, display it, then send it in an email:

private void takePicture() {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStorageDirectory(),
            getIntent().getStringExtra("counter"));
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);
    startActivityForResult(intent, 0);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case 0:
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImage = imageUri;
            getContentResolver().notifyChange(selectedImage, null);
            ContentResolver cr = getContentResolver();
            try {
                bitmap = android.provider.MediaStore.Images.Media
                        .getBitmap(cr, selectedImage);

                savePicture(getIntent().getStringExtra("counter"), bitmap,
                        getApplicationContext());
                setImage();
                Toast.makeText(this, selectedImage.toString(), Toast.LENGTH_LONG).show();
            } catch (Exception e) {

            }
        }
    }
}

How I send the email:

final Intent emailIntent = new Intent(
                    android.content.Intent.ACTION_SEND);


            String fileName = null;
            try {
                fileName = URLEncoder.encode(getIntent().getStringExtra("counter"), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            String path =  Environment.getExternalStorageDirectory()+"/"+fileName.trim().toString();    
             Uri uri = Uri.parse("file://"+path);

             emailIntent.setType("image/png");
            emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, uri);


                    /* Send it off to the Activity-Chooser */
            startActivity(Intent
                    .createChooser(emailIntent, "Send Email..."));

Notice, my file name should be getIntent().getStringExtra("counter");

When I do this, my email sends a text file rather than an image file. I have no clue why it does this...

I suspect this has happened to more than one person, but the problem was this:

I had to add +".jpg" to the end of the filename, and now it works. This may have been a simple solution but it surely was a distressful problem!

Hope this post still helps those who encounter this issue in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM