2011-09-29 3 views
6

저는 현재 사진을 찍어서 미리 결정된 이메일 주소로가는 이메일에 그 사진을 첨부 할 앱을 만들고자합니다.Android : 애플리케이션에서 이미지를 이메일 첨부 파일로 보내는 방법은 무엇인가요?

전자 메일이 작동하고 카메라가 작동합니다. 카메라가 첨부 파일로 추가 한 사진을 얻는 것 같습니다. 이 이미지가 붙어있는 이유가 있다면 나는 밖으로 문제가없는 미리보기 이미지 종류로 응용 프로그램에서 팝업 이미지가 있습니다.

이메일을 보내면 사진이 생성되었지만 손상되어 열리지 않습니다. 마치 내가 존재하지 않는 그림을 만드는 것처럼. 나는 첨부 파일을 만드는 부분에 찍힌 사진을 묶는 경우가 될 것이라고 생각하지만 나는 잘 모른다! 아무도 도와 줄 수 없다면 나는 매우 감사 할 것입니다!

여기 내 이메일이 카메라와 함께 생성되는 MainActivity :

난 그냥 그들을 함께 연결 아니에요 간단한 것을 그 뭔가를 바라고 있어요
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){       
         //     
         }   
      } } 
    } 

.

답변

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

Thank you! 완벽하게 작동! – 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...")); 
관련 문제