Android take photo and send it as an attachment to an email, imageview reset on rotation

拜拜、爱过 提交于 2020-01-06 08:11:24

问题


I need a little or much help here.

I created a fragment that takes a photo and passes it as an attachment on the email that I send.

The fragment works but it has some bugs, please take a look:

  1. I tried almost every code and example to take a photo and manage to put it on an imageview but in vain it does not work, because I have a samsung mobile and samsung mobiles work different. So I copied onActivityResult method from another guy's tutorial.(If there is a simpler please way be my guest)

  2. The application crashes when I try to send the email for a second time. The first time when I press the send button it sends the email, but when I press it again, it crashes.

  3. For a weird reason the email works only with the gmail application that I have installed on my samsung. I have another mail client (the default) but when I choose it, the app crashes.

  4. I use onsavedinstansestate but when rotate the device, again, the app crashes.

I believe that my questions 2 and 4 are a matter of the saveInstancestate because if on every refresh of the fragment, everything is reset (imageviews etc).

Here I write my contact fragment, kindly take a look

    public class contact extends Fragment {
    private String dataImported;
    private TextView txt;
    private ImageView imgThumbNail;
    private Bitmap bmap;
    String mCurrentPhotoPath;
    private static final int CAMERA_IMAGE_CAPTURE = 0;
    private static final int EMAIL_SEND = 1;
    private Uri uriThumbnailImage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    //Save the thumbnail
    if (uriThumbnailImage != null){
    imgThumbNail.buildDrawingCache();
    bmap = imgThumbNail.getDrawingCache();

    outState.putParcelable("savedImage", bmap);
    }

     }

     private void dispatchTakePictureIntent(){                  

    Intent TakePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(TakePictureIntent, CAMERA_IMAGE_CAPTURE);
     }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.e(getTag(), "onCreateView 3");
    txt = (TextView) getActivity().findViewById(R.id.txt_fragment3);
    imgThumbNail = (ImageView) getActivity().findViewById(R.id.imageThumbnail);

    if (container == null){
        return null;
    }
    return inflater.inflate(R.layout.fragment3_layout,
            container,false);
}


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    txt = (TextView) getActivity().findViewById(R.id.txt_fragment3);
    Log.e(getTag(), "onActivityCreated 3");


    if ((savedInstanceState != null) && (savedInstanceState.getParcelable("savedImage") != null))
    {
         bmap = (Bitmap) savedInstanceState.getParcelable("savedImage");
         System.out.println(bmap);
         imgThumbNail = (ImageView) getActivity().findViewById(R.id.imageThumbnail);
         imgThumbNail.setImageBitmap(bmap);
    }

    //create onClickListener for the email
    email();

    //create onClickListener for the photo
    takephoto();


    if (this.dataImported == null)
        txt.setText("Στείλτε μας την δικιά σας γλυκιά συνταγή!");
    else
        txt.setText(this.dataImported);
}


     private void takephoto() {
    ImageButton btnTakePhoto =(ImageButton) getActivity().findViewById(R.id.btn_takePhoto);
    btnTakePhoto.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            dispatchTakePictureIntent();
        }
    });

}


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAMERA_IMAGE_CAPTURE)
    {   
        // Describe the columns you'd like to have returned. Selecting from the Thumbnails
        String[] projection = {
            MediaStore.Images.Thumbnails._ID,   //The columns we want
            MediaStore.Images.Thumbnails.IMAGE_ID,
            MediaStore.Images.Thumbnails.KIND,
            MediaStore.Images.Thumbnails.DATA
        };
        String selection = MediaStore.Images.Thumbnails.KIND + "=" + MediaStore.Images.Thumbnails.MINI_KIND;

        String sort = MediaStore.Images.Thumbnails._ID + " DESC";

        @SuppressWarnings("deprecation")
        Cursor myCursor = getActivity().managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, selection, null, sort);

        long imageId = 01;
        long thumbnailImageId = 01;
        String thumbnailPath = "";

        try{
            myCursor.moveToFirst();
            imageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
            thumbnailImageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
            thumbnailPath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
        }finally{myCursor.close();}     

         //Create new Cursor to obtain the file Path for the large image

         String[] largeFileProjection = {
         MediaStore.Images.ImageColumns._ID,
         MediaStore.Images.ImageColumns.DATA
         };

         String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC";
         myCursor = getActivity().managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, largeFileProjection, null, null, largeFileSort);
        String largeImagePath = "";

        try{
             myCursor.moveToFirst();
            //This will actually give the file path location of the image.
             largeImagePath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
        }finally{myCursor.close();}
        // These are the two URI's you'll be interested in. They give a handle to the actual images
        Uri uriLargeImage = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(imageId));
        uriThumbnailImage = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, String.valueOf(thumbnailImageId));


        //I connect image to the imageView and show it on the screen
        imgThumbNail = (ImageView) getActivity().findViewById(R.id.imageThumbnail);
        imgThumbNail.setImageURI(uriThumbnailImage); 
    }//if

}


     private void email() {
    final EditText onomaSintagis = (EditText) getActivity().findViewById(R.id.txt_onomaSintagis_send);
    final EditText onomaPelati = (EditText) getActivity().findViewById(R.id.txt_CustomerName_send);
    final EditText sintagiPelati = (EditText) getActivity().findViewById(R.id.txt_Sintagi_send);

    ImageButton btnSendEmail =(ImageButton) getActivity().findViewById(R.id.btn_sendEmail);

    btnSendEmail.setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View v) {
             Log.i("Send email", "");

              String[] TO = {"info@urweb.eu"};
              String[] BCC = {"t.itzaris@gmail.com"};
              Intent emailIntent = new Intent(Intent.ACTION_SEND);
              emailIntent.setData(Uri.parse("mailto:"));
              emailIntent.setType("message/rfc822");                     
              emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
              emailIntent.putExtra(Intent.EXTRA_BCC, BCC);
              emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Αποστολή Συνταγής:"+onomaSintagis.getText()+" Από τον/την:"+onomaPelati.getText());
              emailIntent.putExtra(Intent.EXTRA_TEXT, sintagiPelati.getText());


              //I get the uriThumbnailImage(path of the photo) and i put it on the intent
              Uri uri = Uri.parse(uriThumbnailImage.toString());

              emailIntent.putExtra(Intent.EXTRA_STREAM, uri);


              try{
                  startActivityForResult(Intent.createChooser(emailIntent, "Send mail..."), EMAIL_SEND);
              }catch(android.content.ActivityNotFoundException ex){
                  Toast.makeText(getActivity(), "Sorry, There is no email application installed.", Toast.LENGTH_SHORT).show();
              }
        }
    });
}
}

回答1:


Use Custom camera, it can provide a more compelling experience for your users with special features. check this

http://developer.android.com/guide/topics/media/camera.html#custom-camera

For tutorial

http://capycoding.blogspot.in/2012/06/custom-camera-application.html

and send the saved file as an attachment using path.



来源:https://stackoverflow.com/questions/21546069/android-take-photo-and-send-it-as-an-attachment-to-an-email-imageview-reset-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!