开发者

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) {

            }
        }
    }
开发者_C百科}

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜