我的应用程序正常运行android 4.4.2。但是,当我安装其他版本的应用程序并尝试捕获图像时,应用程序将崩溃。
任何人都知道如何解决这个问题?
这是我的主要活动:
public class MainActivity extends AppCompatActivity {
private Button button;
private String encoded_string, image_name,getUnitCode,getEmpNo,ImageDate,ImageTime;
private Bitmap bitmap;
private File file;
private Uri file_uri;
EditText unitCode,empNo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check KJPhotoAudit Folder Available
checkMyDirectory();
}
public void CaptureImage(View view){
unitCode = (EditText)findViewById(R.id.unitCode);
getUnitCode = unitCode.getText().toString().toUpperCase();
if (TextUtils.isEmpty(getUnitCode)) {
unitCode.setError("Unit Code is Empty");
return;
}
empNo = (EditText)findViewById(R.id.empNo);
getEmpNo = empNo.getText().toString().toUpperCase();
if (TextUtils.isEmpty(getEmpNo)) {
empNo.setError("Employee Number is Empty");
return;
}
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
getFileUri();
i.putExtra(MediaStore.EXTRA_OUTPUT,file_uri);
startActivityForResult(i,10);
}
private void getFileUri(){
//set date to string
ImageDate = new SimpleDateFormat("yyyy-MM-dd").format( new Date());
ImageTime = new SimpleDateFormat("HH:mm:ss").format( new Date());
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format( new Date());
image_name = "MY_" +"JPEG_" + timeStamp + ".jpg" ;
file = new File(Environment.getExternalStoragePublicDirectory("/Myfolder") + File.separator + image_name);
file_uri = Uri.fromFile(file);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 10 && resultCode == RESULT_OK){
new Encode_image().execute();
}
}
private class Encode_image extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... voids){
bitmap = BitmapFactory.decodeFile(file_uri.getPath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,20,stream);
byte[] array = stream.toByteArray();
encoded_string = Base64.encodeToString(array,0);
bitmap.recycle();
return null;
}
@Override
protected void onPostExecute(Void aVoid){
makeRequest();
}
}
private void makeRequest() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(Request.Method.POST, "http:/www.myweb.com/connection.php", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println("response"+response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("error"+error);
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String,String> map = new HashMap<>();
map.put("encoded_string",encoded_string);
map.put("image_name",image_name);
map.put("unit_code",getUnitCode);
map.put("emp_no",getEmpNo);
map.put("image_date",ImageDate);
map.put("image_time",ImageTime);
return map;
}
};
System.out.println("req ="+request);
requestQueue.add(request);
ResetEmpNumber();
}
public void checkMyDirectory(){
File dir = new File(Environment.getExternalStorageDirectory() + "/Myfolder");
if(!dir.exists() && !dir.isDirectory()) {
File folder = new File(Environment.getExternalStorageDirectory().toString()+"/Myfoldert");
folder.mkdirs();
}
}
public void ResetEmpNumber(){
//Reset Employee Text Field
empNo.setText("");
}
}