2011-09-29 13 views
6

私は現在、写真を撮るアプリを作成しようとしており、その写真を事前に決められたEメールアドレスに行くEメールに添付しています。Android:アプリケーションから電子メールの添付ファイルとして画像を送信するにはどうすればよいですか?

私は電子メールを使用しており、私はカメラが動作しています。私はカメラが添付ファイルとして追加するために取った画像を取得することはできません。私はイメージが添付されていない理由である場合、私は問題を出すことはありませんプレビューイメージの一種としてアプリでポップアップしている。

電子メールが送信されると、画像は作成されましたが、破損していて開けません。私はあたかも存在しない絵を描いているようです。私は添付ファイルを作成する部分に撮影した写真を結びつける場合があると思いますが、私は分かりません!もし誰かが私が非常に感謝してくれるのを助けることができたら!

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.OutputStream; 

import android.app.Activity; 
import android.content.ContentValues; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore.Images; 
import android.provider.MediaStore.Images.Media; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 

public class EmailActivity extends Activity { 
     Button send; 
     EditText address, subject, emailtext; 
     protected static final int CAMERA_PIC_REQUEST = 0; 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.email); 
     send=(Button) findViewById(R.id.emailsendbutton); 
     address=(EditText) findViewById(R.id.emailaddress); 
     subject=(EditText) findViewById(R.id.emailsubject); 
     emailtext=(EditText) findViewById(R.id.emailtext); 

     send.setOnClickListener(new OnClickListener() { 

         @Override 
         public void onClick(View v) { 
           // TODO Auto-generated method stub 

          if 
          (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) 
          { 

          } 

          File pngDir = new File(

            Environment.getExternalStorageDirectory(), 
            "Android/data/com.phstudios.jbrefurb/quote"); 

          if (!pngDir.exists()) 
           pngDir.mkdirs(); 

          File pngFile = new File(pngDir, "pic1.png"); 
          Uri pngUri = Uri.fromFile(pngFile); 


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

             emailIntent.setType("image/png"); 

             emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ "[email protected]"}); 

             emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject.getText()); 

             emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext.getText()); 

             emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, pngUri); 

             emailIntent.setType("image/png"); 


            EmailActivity.this.startActivity(Intent.createChooser(emailIntent, "Send mail...")); 

    } 
      }); 

Button camera = (Button) findViewById(R.id.button2); 
     camera.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
        startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 
; 

       }  
      }); 
     } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {   
     if (requestCode== 0 && resultCode == Activity.RESULT_OK){     
      Bitmap x = (Bitmap) data.getExtras().get("data");     
      ((ImageView)findViewById(R.id.imageView1)).setImageBitmap(x);     
      ContentValues values = new ContentValues(); 

      values.put(Images.Media.TITLE, "title");   
      values.put(Images.Media.BUCKET_ID, "test");   
      values.put(Images.Media.DESCRIPTION, "test Image taken");   
      values.put(Images.Media.MIME_TYPE, "image/png");   
      Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);   
      OutputStream outstream;     
      try {       
       outstream = getContentResolver().openOutputStream(uri);   
       x.compress(Bitmap.CompressFormat.JPEG, 70, outstream);   
       outstream.close();     
       } catch (FileNotFoundException e) {       
        //     
        }catch (IOException e){       
         //     
         }   
      } } 
    } 

私はちょうどそれらを一緒にリンクないよ簡単なことにその何かを望んでいる:ここで

は、電子メール、カメラと一緒に作成されている私のMainActivityです。

答えて

3
import android.os.Bundle; 
import android.app.Activity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.support.v4.app.NavUtils; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 


import android.content.ContentValues; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.Bitmap.CompressFormat; 
import android.net.Uri; 

import android.provider.MediaStore.Images; 
import android.provider.MediaStore.Images.Media; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 


public class MainActivity extends Activity { 
    Button send; 
    Bitmap thumbnail; 
    File pic; 
    EditText address, subject, emailtext; 
    protected static final int CAMERA_PIC_REQUEST = 0; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    send=(Button) findViewById(R.id.emailsendbutton); 
    address=(EditText) findViewById(R.id.emailaddress); 
    subject=(EditText) findViewById(R.id.emailsubject); 
    emailtext=(EditText) findViewById(R.id.emailtext); 










    Button camera = (Button) findViewById(R.id.button1); 
    camera.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0){ 
      Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); 

     } 
     }); 

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


} 

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

    } 
} 
+0

を試してみてください、私はちょうどそれを試してみましたが、それは私のために働いていた、それが役に立てば幸い!それが受け入れてください:) – Beast

+0

ありがとうございました!完璧に動作します! – PaulH

+0

ちょうど何かを発見しました。今撮影されていない電子メールは送信されません。写真が撮られていない場合でもメールは送信されますか? – PaulH

0

この

 Intent i = new Intent(Intent.ACTION_SEND); 
    i.setType("text/plain"); 
    i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"}); 
    i.putExtra(Intent.EXTRA_SUBJECT, " report"); 
    i.putExtra(Intent.EXTRA_TEXT , "PFA"); 
    i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(destinationFile));//pngFile 

     startActivity(Intent.createChooser(i, "Send mail...")); 
関連する問題