2014-02-21 7 views
1

갤러리에서 전자 메일을 보내는 데 문제가 있지만 정상적으로 작동하지만 캡처 된 이미지가 아닙니다. 나는 다른 코드를 시도해 보았다. 아니면 어떻게 작동하는지 설명하고 가르쳐주세요.캡처 한 이미지를 내 메일의 첨부 파일로 보내려고합니다.

이 앱은 사용자로부터 날짜를 가져 와서 전자 메일을 통해 미리 정의 된 전자 메일 주소로 보냅니다. 그러나 문제는 이미 캡처 된 갤러리에서 이미지를 가져 오는 두 가지 방법으로 이미지를 첨부 할 수 있다는 것입니다. 다른 옵션은 사용자가 이미지를 캡처 한 다음 업로드하여 첨부 파일로 전자 메일로 보내는 것입니다.

이 내 응용 프로그램에서 두 번째 활동

package com.example.medipostrx; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.database.Cursor; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.Toast; 

public class secondpage extends Activity{ 
    ImageView viewImage; 
    File pic; 
    Button b; 
    Button c; 

    String usernames; 
    String clnumber; 
    String clname; 
    String spnnr; 
    Uri imgui = null; 
    int h = 0; 


    @Override 

    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.secondpage); 
     Bundle data = getIntent().getExtras(); 
     usernames = data.getString("user"); 
     clnumber=data.getString("clnu"); 
     clname=data.getString("clnam"); 
     spnnr = data.getString("spn"); 
     b=(Button)findViewById(R.id.btntake); 
c= (Button) findViewById(R.id.btnsub); 
final ImageView y = (ImageView) findViewById(R.id.viewImage); 
c.setOnClickListener(new View.OnClickListener() { 

    @Override 

    public void onClick(View v) { 

     Intent i = new Intent(Intent.ACTION_SEND); 
     //i.setType("message/rfc822"); 
     i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"}); 
     //i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"}); 
     i.putExtra(Intent.EXTRA_SUBJECT, "Clinic Number: " + clnumber); 
     i.putExtra(Intent.EXTRA_TEXT , "Username: " + usernames + "\nClinic Number: " + clnumber + "\nClinic Name: " + clname + "\nBranch: " + spnnr); 

     //if(h==1){ 
     if(imgui != null){ 
      i.putExtra(Intent.EXTRA_STREAM, imgui); 
      i.setType("image/png"); 
     }else{ 
      i.setType("plain/text"); 
     } 
     //}//else{ 
      /*Drawable d =y.getBackground(); 
      BitmapDrawable bitDw = ((BitmapDrawable) d); 
      Bitmap bitmap = bitDw.getBitmap(); 
      File mFile = savebitmap(bitmap); 
      Uri u = null; 
       u = Uri.fromFile(mFile); 
       i.putExtra(Intent.EXTRA_STREAM, u); 
       i.setType("image/png");*/ 
      //i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic)); 
      //i.setType("image/png"); 
      //Toast.makeText(secondpage.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); 

     //} 
     try { 
      startActivity(Intent.createChooser(i, "Send mail...")); 
     } catch (android.content.ActivityNotFoundException ex) { 
      Toast.makeText(secondpage.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); 
     } 

    } 

}); 
     viewImage=(ImageView)findViewById(R.id.viewImage); 

     b.setOnClickListener(new View.OnClickListener() { 

      @Override 

      public void onClick(View v) { 

       selectImage(); 

      } 

     }); 

    } 
    private File savebitmap(Bitmap bmp) { 
      String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); 
      OutputStream outStream = null; 
     // String temp = null; 
     File file = new File(extStorageDirectory, "temp.png"); 
      if (file.exists()) { 
      file.delete(); 
      file = new File(extStorageDirectory, "temp.png"); 

      } 

      try { 
      outStream = new FileOutputStream(file); 
      bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
      outStream.flush(); 
      outStream.close(); 

      } catch (Exception e) { 
      e.printStackTrace(); 
      return null; 
      } 
      return file; 
     } 


    @Override 

    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds options to the action bar if it is present. 

     getMenuInflater().inflate(R.menu.main, menu); 

     return true; 

    } 



     private void selectImage() { 



     final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" }; 



     AlertDialog.Builder builder = new AlertDialog.Builder(secondpage.this); 

     builder.setTitle("Add Photo!"); 

     builder.setItems(options, new DialogInterface.OnClickListener() { 

      @Override 

      public void onClick(DialogInterface dialog, int item) { 

       if (options[item].equals("Take Photo")) 

       { 

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

        File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg"); 

        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 

        startActivityForResult(intent, 1); 


       } 

       else if (options[item].equals("Choose from Gallery")) 

       { 

        Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

        startActivityForResult(intent, 2); 



       } 

       else if (options[item].equals("Cancel")) { 

        dialog.dismiss(); 

       } 

      } 

     }); 

     builder.show(); 

    } 



    @Override 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     super.onActivityResult(requestCode, resultCode, data); 

     if (resultCode == RESULT_OK) { 

      if (requestCode == 1) { 
       h=1; 
       File f = new File(Environment.getExternalStorageDirectory().toString()); 

       for (File temp : f.listFiles()) { 

        if (temp.getName().equals("temp.jpg")) { 

         f = temp; 
         File photo = new File(Environment.getExternalStorageDirectory(), "temp.jpg"); 

         break; 

        } 

       } 

       try { 

        Bitmap bitmap; 

        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 



        bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), 

          bitmapOptions); 



        viewImage.setImageBitmap(bitmap); 




        String path = android.os.Environment 

          .getExternalStorageDirectory() 

          + File.separator 

          + "Phoenix" + File.separator + "default"; 

        f.delete(); 

        OutputStream outFile = null; 

        File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg"); 

        try { 

         outFile = new FileOutputStream(file); 

         bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile); 
//pic=file; 
         outFile.flush(); 

         outFile.close(); 


        } catch (FileNotFoundException e) { 

         e.printStackTrace(); 

        } catch (IOException e) { 

         e.printStackTrace(); 

        } catch (Exception e) { 

         e.printStackTrace(); 

        } 

       } catch (Exception e) { 

        e.printStackTrace(); 

       } 

      } else if (requestCode == 2) { 



       Uri selectedImage = data.getData(); 
imgui = selectedImage; 
       String[] filePath = { MediaStore.Images.Media.DATA }; 

       Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null); 

       c.moveToFirst(); 

       int columnIndex = c.getColumnIndex(filePath[0]); 

       String picturePath = c.getString(columnIndex); 

       c.close(); 

       Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath)); 

       Log.w("path of image from gallery......******************.........", picturePath+""); 

       viewImage.setImageBitmap(thumbnail); 

      } 

     } 

    } 


}` 

그리고 내 XML 파일입니다;

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:hint="@string/filenumber" > 

     <requestFocus /> 
    </EditText> 

    <Button 
     android:id="@+id/btntake" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/takepicutre" /> 
     <ImageView 

      android:id="@+id/viewImage" 

      android:layout_width="200dp" 

      android:layout_height="200dp" 
      android:layout_gravity="center" 

      android:src="@drawable/cameraa" /> 

     <Button 
      android:id="@+id/btnsub" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/email" /> 

</LinearLayout> 
+1

http://chintankhetiya.wordpress.com/2013/12/25/camera-preview-in-android/ 확인하고 이미지 경로를 사용하여 메일에 첨부하십시오. –

+0

https://www.javacodegeeks.com/2013/ 10/send-email-with-attachment-in-android.html –

답변

0
Intent iShare; 
iShare = new Intent(Intent.ACTION_SEND); 
iShare.setType("image/png"); 
iShare.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+fn.getPath())); 
iShare.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(new StringBuilder() 
     .append("<p><b>Path</b></p>").append("<small><p>"+sb+"</p></small>").toString())); 
startActivity(Intent.createChooser(iShare, "Share")); 

는 AFAIK 당신은 당신이 위의 코드를 시도 할 수 있습니다 이메일에 첨부 파일로 캡처 한 이미지를 보낼 수 있습니다.

+0

이 동생을 이해할 수없는 부분으로 설명해주세요 :) iShare.putExtra (Intent.EXTRA_STREAM, Uri.parse ("file : //" + fn.getPath())); iShare.putExtra (Intent.EXTRA_TEXT, Html.fromHtml (새의 StringBuilder() 으로 .Append ("

경로

")으로 .Append ("

"+ SB + "

")로 .toString())); –

+0

file : // blabla는 파일 경로를 그 위에두고 두 번째 명령문은 메일 본문에 내용을 추가하기 위해 코드의 일부분이므로 필요하지 않습니다. –

관련 문제