我有一个使用相机拍照的应用程序,然后转发到电子邮件地址。然而,在我拍摄照片后,活动结束,我发生了崩溃,如下图所示:
过去很容易跟踪这个错误,因为我一直在检查Logcat。崩溃很好地显示了红色文本和与之相关的文本。但是,不久前,这种行为消失了,我无法设法跟踪错误。我不知道我是否更改了某些内容,或者因为我将Firebase与该应用相关联...
任何人都可以给我任何关于如何在这种情况下看到错误的提示吗?如果有什么我可能错误地改变了这种行为?
发生崩溃时Logcat中显示的唯一内容是:
01-23 17:03:13.958 27525-6169/packagename V/FA: Connecting to remote service
01-23 17:03:13.988 27525-27525/packagename I/Adreno-EGL: <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: (CL3869936)
OpenGL ES Shader Compiler Version: 17.01.12.SPL
Build Date: 03/03/14 Mon
Local Branch: default
Remote Branch:
Local Patches:
Reconstruct Branch:
01-23 17:03:14.038 27525-6169/packagename V/FA: Activity resumed, time: 2962508140
01-23 17:03:14.128 27525-6169/packagename D/FA: Connected to remote service
01-23 17:03:14.128 27525-6169/packagename V/FA: Processing queued up service tasks: 1
01-23 17:03:19.174 27525-27525/packagename I/PersonaManager: getPersonaService() name persona_policy
01-23 17:03:19.184 27525-27525/packagename V/FA: onActivityCreated
01-23 17:03:19.334 27525-6169/packagename V/FA: Recording user engagement, ms: 5372
01-23 17:03:19.855 27525-6169/packagename V/FA: Activity paused, time: 2962513278
01-23 17:03:19.945 27525-6169/packagename V/FA: Activity resumed, time: 2962513501
01-23 17:03:19.955 27525-6169/packagename V/FA: Inactivity, disconnecting from the service
但它看起来不像会导致应用程序崩溃的错误。
编辑:根据要求,代码如下: 主Java文件:
import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Vibrator;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.karan.churi.PermissionManager.PermissionManager;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Civic extends AppCompatActivity {
static final int REQUEST_TAKE_PHOTO = 1;
private static final int ID_UL_MEU_PENTRU_PERMISIA_DE_WRITING = 1001;
private static final int ID_UL_MEU_PENTRU_PERMISIA_DE_CAMERA = 1000;
public String img1;
public String path;
public int[] store;
PermissionManager permissionManager;
ImageView img;
EditText editTextEmailValue;
EditText editTextNumeValue;
EditText message;
String mCurrentPhotoPath = null;
private Bitmap imgBitmap;
private int mDstWidth;
private int mDstHeight;
private Bitmap scaledBitmap;
private BitmapDrawable drawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setBackgroundDrawableResource(R.drawable.statuie_thirty_opac);
setContentView(R.layout.activity_civic);
/* CheckPermissionsCamera();*/
TextView info = findViewById(R.id.info);
img = findViewById(R.id.imageV);
Button btnImg = findViewById(R.id.camera);
Button btnSend = findViewById(R.id.sendCivic);
editTextEmailValue = findViewById(R.id.emailCivic);
editTextNumeValue = findViewById(R.id.numeCivic);
message = findViewById(R.id.descriereCivic);
mDstHeight = 720;
mDstWidth = 720;
//CHECK PERMISSIONS
permissionManager = new PermissionManager() {};
permissionManager.checkAndRequestPermissions(this);
//END CHECK PERMISSION
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
//Call back for Dialog!
public void Dialog(View view) {
if (check() == 0) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setMessage("Mulțumim pentru sesizare. Te vom contacta în cel mai scurt timp.")
.setTitle("Mesajul se trimite!")
.setCancelable(false)
.setPositiveButton("Închide", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
sendMail();
Toast.makeText(getApplicationContext(), "Photo saved in:" + mCurrentPhotoPath, Toast.LENGTH_SHORT).show();
message.setText("");
editTextEmailValue.setText("");
editTextNumeValue.setText("");
}
}).show();
}
}
//Conditii!
private int check() {
Vibrator vib = (Vibrator) getSystemService(VIBRATOR_SERVICE);
int counter = 0, leng, x = 300, y = 20;
if (emailVerif(editTextEmailValue.getText().toString()) == false || editTextEmailValue.getText().toString().equals("")) {
editTextEmailValue.setError("Nu ați respectat formatul de email : aaa@bbb.ccc");
editTextEmailValue.requestFocus();
counter++;
}
if (editTextNumeValue.getText().toString().equals("")) {
editTextNumeValue.setError("Obligatoriu!");
editTextNumeValue.requestFocus();
counter++;
}
if (message.getText().toString().equals("")) {
message.setError("Obligatoriu!");
message.requestFocus();
counter++;
}
if (message.getText().length() > x) {
leng = message.getText().length() - x;
message.setError("Ați depășit limita cu : " + leng + " caractere!");
message.requestFocus();
counter++;
} else if (message.getText().length() < y) {
leng = y - message.getText().length();
message.setError("Detaliați cu încă : " + leng + " caractere!");
message.requestFocus();
counter++;
}
if (mCurrentPhotoPath == null) {
Toast.makeText(getApplicationContext(), "Nu ați făcut poză!", Toast.LENGTH_LONG).show();
counter++;
}
if (counter != 0) {
vib.vibrate(500);
}
return (counter);
}
//Return true if email is good else return false
//AICI VERIFICAM DACA FORMATUL DE LA EMAIL ESTE RESPECTAT
protected boolean emailVerif(String email) {
String emailPattern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" +
"[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(emailPattern);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
//Send mail
private void sendMail() {
//Getting content for email
String email = editTextEmailValue.getText().toString().trim();
String nume = editTextNumeValue.getText().toString().trim();
String mesaj = message.getText().toString().trim();
String image = mCurrentPhotoPath.toString().trim();
//Creating SendMail object
SendMailCivic sm = new SendMailCivic(this, nume, email, mesaj, image);
//Executing sendmail to send email
sm.execute();
}
//Dialog for information!
public void infoCivic(View view) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setMessage("Instructions")
.setTitle("Informatii")
.setCancelable(false)
.setPositiveButton("Închide", null).show();
}
public void CheckPermissionsCamera(View view) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permisii insuficiente.", Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(this,
new String[] {
Manifest.permission.CAMERA
},
ID_UL_MEU_PENTRU_PERMISIA_DE_CAMERA
);
} else if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permisii insuficiente.", Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(this,
new String[] {
Manifest.permission.WRITE_EXTERNAL_STORAGE
},
ID_UL_MEU_PENTRU_PERMISIA_DE_WRITING
);
} else {
dispatchTakePictureIntent();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case ID_UL_MEU_PENTRU_PERMISIA_DE_WRITING:
{
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Ati acceptat permisiunea!", Toast.LENGTH_SHORT).show();
} else {
// daca grantResults e gol, a dat cancel
Toast.makeText(this, "Nu ati acceptat pemisiunea!", Toast.LENGTH_LONG).show();
}
}
case ID_UL_MEU_PENTRU_PERMISIA_DE_CAMERA:
{
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Ati acceptat permisiunea!", Toast.LENGTH_SHORT).show();
} else {
// daca grantResults e gol, a dat cancel
Toast.makeText(this, "Nu ati acceptat pemisiunea!", Toast.LENGTH_LONG).show();
}
}
}
}
}
&#13;
发送电子邮件的SendMailCivic.java:
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
//Class is extending AsyncTask because this class is going to perform a networking operation
public class SendMailCivic extends AsyncTask<Void, Void, Void> {
//Declaring Variables
private Context context;
private Session session;
private ProgressDialog progressDialog;
//Information to send email
private String email;
private String mesaj;
private String nume;
private String image;
//Class Constructor
public SendMailCivic(Context context, String nume, String email, String mesaj, String image) {
//Initializing variables
this.context = context;
this.nume = nume;
this.email = email;
this.mesaj = mesaj;
this.image = image;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(context, "Sending email", "Please wait...", false, false);
progressDialog.show();
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//Showing a success message
Toast.makeText(context, "Message Sent", Toast.LENGTH_LONG).show();
progressDialog.cancel();
}
@Override
protected Void doInBackground(Void... params) {
//Creating properties
Properties props = new Properties();
//Configuring properties for gmail
props.put("mail.smtp.host", "mail.server.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
//Creating a new session
session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
//Authenticating the password
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(Configurare_email.EMAIL, Configurare_email.PASSWORD);
}
});
try {
//Creating MimeMessage object
MimeMessage mm = new MimeMessage(session);
//Setting sender address
mm.setFrom(new InternetAddress(Configurare_email.EMAIL));
//Adding receiver
//TODO set email in InternetAddress
mm.addRecipient(Message.RecipientType.TO, new InternetAddress("receiveradress@server.com"));
//Adding subject
mm.setSubject("MailSubject");
Multipart multipart = new MimeMultipart();
BodyPart mesajPart = new MimeBodyPart();
mesajPart.setText("Name" + nume + "email" + email + "body" + mesaj);
multipart.addBodyPart(mesajPart);
BodyPart attach = new MimeBodyPart();
DataSource source = new FileDataSource(image); //image = path
attach.setDataHandler(new DataHandler(source));
attach.setFileName(image);
multipart.addBodyPart(attach);
mm.setContent(multipart);
//Sending email
Transport.send(mm);
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}
&#13;
路径在清单文件中给出:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
其中@ xml / file_paths包含:
<< ? xml version = "1.0"
encoding = "utf-8" ? >
<
paths >
<
external - path name = "my_images" / >
<
/paths>
&#13;
答案 0 :(得分:1)
我不能将此作为评论,请尝试使用像...这样的方法。
private Uri getFileUri(String filepath) throws FileNotFoundException {
Uri uri = Uri.parse(filepath);
if (uri.getScheme() == null) {
// No prefix, assuming that provided path is absolute path to file
File file = new File(filepath);
if (file.isDirectory()) {
throw new FileNotFoundException("IsDirectory Not File");
}
uri = Uri.parse("file://" + filepath);
}
return uri;
}
然后,通过执行new File(getFileUri(imagePath));
之类的操作而不是传入文件中的路径传递,getFileUri应该正确处理不正确的文件。然后,您可以将文件传递到FileDataSource而不是路径。