我需要一些帮助。
我创建了一个片段,该片段拍摄照片并将其作为我发送的电子邮件中的附件传递。
片段有效,但有一些错误,请看一下:
我尝试了几乎所有的代码和示例来拍摄照片并设法将其放在imageview上,但是徒劳无功,因为我有一个三星手机和三星手机的工作方式不同。所以我从另一个人的教程中复制了onActivityResult方法。(如果有一个更简单的请成为我的客人)
当我尝试第二次发送电子邮件时,应用程序崩溃了。第一次按下发送按钮时,它会发送电子邮件,但是当我再次按下它时,它会崩溃。
出于某种奇怪的原因,该电子邮件仅适用于我在我的三星上安装的Gmail应用程序。我有另一个邮件客户端(默认)但当我选择它时,应用程序崩溃。
我使用了onsavedinstansestate,但是当再次旋转设备时,应用程序崩溃了。
我相信我的问题2和4是saveInstancestate的问题,因为如果每次刷新片段,一切都会被重置(imageviews等)。
在这里,我写下我的联系人片段,请好好看看
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();
}
}
});
}
}
答案 0 :(得分:0)
使用自定义相机,它可以为具有特殊功能的用户提供更具吸引力的体验。检查一下
http://developer.android.com/guide/topics/media/camera.html#custom-camera
对于教程
http://capycoding.blogspot.in/2012/06/custom-camera-application.html
并使用路径将保存的文件作为附件发送。