Newbie Progie Newbie Progie - 1 month ago 9
Android Question

How to check if image is empty and will not crash the app

Hi when I'm clicking the post button on my android app but the image is empty the app crashes can you guys help me with this? Thanks. Here is my code. i guess the bitmap ones is the problem here but still not sure I tried like the

textUtils
on the editText but it didnt work. LOL

@Override
public void onClick(View v) {
try {
bitmap = ImageLoader.init().from(selectedPhoto).requestSize(512,512).getBitmap();
final String encodeImage = ImageBase64.encode(bitmap);
HashMap<String, String> post = new HashMap<String, String>();
post.put("image", encodeImage);
PostResponseAsyncTask taskUpload = new PostResponseAsyncTask(InsertActivity.this, post, new AsyncResponse() {
@Override
public void processFinish(String s) {
Log.d(TAG, s);
if (s.contains("upload_success")) {

} else {
Toast.makeText(getApplicationContext(), "Error Uploading", Toast.LENGTH_SHORT).show();
}
}
});
taskUpload.execute("http://carkila.esy.es/upload1.php");
taskUpload.setEachExceptionsHandler(new EachExceptionsHandler() {
@Override
public void handleIOException(IOException e) {
Toast.makeText(getApplicationContext(), "Cannot Connect to Server", Toast.LENGTH_SHORT).show();
}

@Override
public void handleMalformedURLException(MalformedURLException e) {
Toast.makeText(getApplicationContext(), "URL Error", Toast.LENGTH_SHORT).show();
}

@Override
public void handleProtocolException(ProtocolException e) {
Toast.makeText(getApplicationContext(), "Protocol Error", Toast.LENGTH_SHORT).show();
}

@Override
public void handleUnsupportedEncodingException(UnsupportedEncodingException e) {
Toast.makeText(getApplicationContext(), "Encoding Error", Toast.LENGTH_SHORT).show();
}
});
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(), "Something went wrong while choosing photos", Toast.LENGTH_SHORT).show();
}
HashMap postData = new HashMap();

postData.put("image", toString());
postData.put("txtCapacity", etCapacity.getText().toString());
postData.put("txtCarType", etCarType.getText().toString());
postData.put("txtCarModel", etCarModel.getText().toString());
postData.put("txtPrice",etPrice.getText().toString());
postData.put("txtPlateNumber", etPlateNumber.getText().toString());
postData.put("txtOwner", pref.getString("username","").toString());
postData.put("txtFuelType", spFuelType.getSelectedItem().toString());
postData.put("txtFrom",etFrom.getText().toString());
postData.put("txtTo", etTo.getText().toString());
postData.put("txtStatus","pending");
postData.put("txtVerification","pending");

PostResponseAsyncTask taskPost = new PostResponseAsyncTask(InsertActivity.this, postData, new AsyncResponse() {
@Override
public void processFinish(String s) {
Log.d(TAG, s);
if (s.contains("Insert Successfully")) {
Toast.makeText(InsertActivity.this, "Car will be verified for 2-3 days", Toast.LENGTH_LONG).show();
Intent in = new Intent(InsertActivity.this, OwnerTabs.class);
startActivity(in);
finish();
} else {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
}
});
taskPost.execute("http://carkila.esy.es/carkila/insert.php");
}


UPDATE

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == GALLERY_REQUEST) {
Uri uri = data.getData();
galleryPhoto.setPhotoUri(uri);
String photoPath = galleryPhoto.getPath();
selectedPhoto = photoPath;
try {
bitmap = ImageLoader.init().from(photoPath).requestSize(512,512).getBitmap();
ivImage.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(), "Something went wrong while opening gallery", Toast.LENGTH_SHORT).show();
}
}
}
}

Answer

Put a not null check before using bitmap. if (bitmap != null) { //use bitmap }.