I have this activity in which the user can either choose one image from Gallery or just take a picture and (along with other data) upload it to a website.
So far I've encountered 2 different problems:
1) If I try it with a picture from the gallery, I get an IOException with message /external/images/media/2305: open failed: ENOENT (No such file or directory) That happens when it comes to open the file stream.
2) If I try it by taking the picture, it goes ok, but the encoded data string is composed of "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" (really longer, but only A's) and I guess that's not a good sign. This is only a guess since I still cannot properly upload it to the website, but different pictures showing the same data string just smells funny.
The code here
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
//Uri selectedImage = imageUri;
loadImage(imageUri);
}
break;
case SELECT_PHOTO:
if(resultCode == Activity.RESULT_OK){
imageUri = data.getData();
loadImage(imageUri);
}
}
}
This is how I load the image (either pic taken or from the gallery) onto the ImageView. It works ok.
public void loadImage(Uri selectedImage){
mActivity.getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = mActivity.getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
ivPicture.setImageBitmap(bitmap);
ivPicture.setVisibility(View.VISIBLE);
mActivity.croutonInfo(selectedImage.toString());
} catch (Exception e) {
mActivity.croutonAlert("Failed to load");
e("Camera " + e.toString());
}
}
This is the method I use to mock the data upload. When I get the API it will have an asynctask to deal with the http transfer, so far it only puts the data into a logicless transfer object
public void uploadTapa() throws IOException{
mActivity.croutonInfo("subiendo tapa ");
d("uploadTapa new ");
TapaUploadParametros tup = new TapaUploadParametros();
d("uploadTapa bar: " + nombreBar);
tup.setBarNombre(etBarName.getText().toString());
d("uploadTapa tapa: " + nombreTapa);
tup.setNombre(etTapaName.getText().toString());
d("uploadTapa municipio: " + municipio);
tup.setLocalidad(municipio);
d("uploadTapa provincia: " + provincia);
tup.setProvincia(provincia);
d("uploadTapa tipologiaId: " + tipologiaId);
tup.setTipo(tipologiaId);
d("uploadTapa precioId: " + precioId);
tup.setPrecio(precioId);
String encodedImage = encodeImgForHTTP(imageUri);
d("uploadTapa encoded image: " + encodedImage);
tup.setPic(encodedImage);
d("uploadTapa direccionBar: " + direccionBar);
tup.setBarDireccion(direccionBar);
}
And this is the method to encode the image for http transfer. Images from gallery fail just after "before opening stream"
private String encodeImgForHTTP (Uri imageUri) throws IOException{
ContentResolver cr = mActivity.getContentResolver();
d("encodeImgForHTTP before opening stream ");
FileInputStream fis = new FileInputStream(imageUri.getPath());
d("encodeImgForHTTP after opening stream ");
// Get binary bytes for encode
byte[] imageBytes = new byte[fis.available()];
d("encodeImgForHTTP after getting byte array ");
// base 64 encode for text transmission (HTTP)
d("encodeImgForHTTP pre 64: " + imageBytes);
String data_string = Base64.encodeToString(imageBytes, Base64.URL_SAFE);
d("encodeImgForHTTP before returning the encoded data string " + data_string);
return data_string;
}
What am I doing wrong with the gallery images? Why does the encoding of different pictures look the same?