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:
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)
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.
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.
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 = {"[email protected]"};
String[] BCC = {"[email protected]"};
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();
}
}
});
}
}
See Question&Answers more detail:
os