Reputation:
I want to send image to the server as soon as the camera captures new image or any image file added to the gallery using the ContentObserver my code is running in service I registered the ContentObserver on the service onCrate method every thing is working fine i am trying to compress he image to base 64 andI want to send this converted imgae to the server but the problem is when i capture image from camera after this line
ByteArrayOutputStream stream = new ByteArrayOutputStream();
my code does't proceed so my image is not transferred because those line of code was not executed here is the code
public class GallreyObserver extends ContentObserver{
public GallreyObserver(Context c,Handler handler) {
super(handler);
this.c=c;
}
@Override
public void onChange(boolean selfChange) {
// TODO Auto-generated method stub
super.onChange(selfChange);
GallreyLogsDetail(c);
Log.d("onChanged", "Change in logs");
}
private void GallreyLogsDetail(Context con)
{
Cursor cur = con.getContentResolver().query(images, projection, null, null, MediaStore.Images.Media.DATE_TAKEN+ " DESC");
gallreyData= new ArrayList<GallreyLogsInfo>();
int datacolumn = cur.getColumnIndex(MediaStore.Images.Media.DATA);
while(cur.moveToNext())
{
// Get the field values
String imagePath=cur.getString(datacolumn);
GallreyLogsInfo logs=new GallreyLogsInfo();
logs.setImgPath(imagePath);
gallreyData.add(logs);
new rest().execute(new String[] {SERVICE_URL});
}
AsyncTask is the inner class of GallreyObserver
private class rest extends AsyncTask <String, Void, String>
{
@Override
protected String doInBackground(String... urIs) {
String response="";
Log.d("do", "doInBackground");
for(String urI: urIs)
{
HttpPost httppost= new HttpPost(urI);
// httppost.setHeader("content-type", "MULTIPART_FORM_DATA");
httppost.setHeader("content-type", "multipart/form-data");
httppost.setHeader("ENCTYPE", "multipart/form-data");
HttpParams httpParameters = new BasicHttpParams();
// httpParameters.setLongParameter("id", 1L);
int timeOutConnection=3000;
HttpConnectionParams.setConnectionTimeout(httpParameters,timeOutConnection);
int timeoutsocket=5000;
HttpConnectionParams.setSoTimeout(httpParameters,timeoutsocket);
DefaultHttpClient client = new DefaultHttpClient();
client.setParams(httpParameters);
for(int i=0;i< gallreyData.size();i++)
{
Log.d("list", "forr loop");
GallreyLogsInfo gl =gallreyData.get(i);
String gimagePath=gl.getImgPath();
Log.i("Listing Images","\n data:" +"" + gimagePath+ ");
Log.d("bitmap", "before bitmap");
Bitmap bitmap = BitmapFactory.decodeFile(gimagePath);
Log.d("ByteArrayOutputStream", "before ByteArrayOutputStream");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Log.d("compress", "before compress");
boolean s;
s= bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
Log.d("s", ""+s);
Log.d("recycle", "before recycle");
bitmap.recycle();
bitmap = null;
Log.d("byte", "before byte");
byte[] byteArray=stream.toByteArray();
// Byte code array to Base64 String
String sendImg=Base64.encodeToString(byteArray,Base64.DEFAULT);
Log.d("send",""+ ""+sendImg);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
Log.d("NameValuePair", "online");
nameValuePairs.add(new BasicNameValuePair("image path", gimagePath));
Log.d("going", "server");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse execute =client.execute(httppost);
Log.d("server", "sent");
}
return null;
}}
please help me i am unable to detect the problem there was no errors but my code was not executing in the flow thanks in advance
here is the logcat
05-28 12:18:24.941: D/do(25504): doInBackground
05-28 12:18:24.941: D/onChanged(25504): Change in logs
05-28 12:18:25.187: D/onChanged(25504): Change in logs
05-28 12:18:25.652: D/list(25504): forr loop
05-28 12:18:25.675: I/Listing Images(25504): data:/storage/sdcard0/DCIM/Camera/IMG_20140528_121821.jpg
05-28 12:18:25.675: D/bitmap(25504): before bitmap
05-28 12:18:25.847: D/dalvikvm(25504): GC_FOR_ALLOC freed 77K, 3% free 8034K/8259K, paused 42ms, total 47ms
05-28 12:18:26.859: I/dalvikvm-heap(25504): Grow heap (frag case) to 26.643MB for 19660816-byte allocation
05-28 12:18:26.929: D/dalvikvm(25504): GC_CONCURRENT freed 3K, 2% free 27230K/27527K, paused 11ms+4ms, total 67ms
Upvotes: 0
Views: 3419
Reputation: 369
Can't figure out the error without the error log, but it seems that you have a problem when converting the bitmap.
Try this class that converts image to base64
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import android.text.TextUtils;
import android.util.Base64;
public class ConvertImageToBase64 {
public static String convert(String path){
InputStream inputStream = null;
try {
inputStream = new FileInputStream(path);
byte[] bytes;
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
while ((bytesRead = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
bytes = output.toByteArray();
String encodedString = Base64.encodeToString(bytes, Base64.DEFAULT);
return TextUtils.htmlEncode(encodedString);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
finally{
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
return null;
}
}
It works fine; try to use it. I hope it helps you.
If you want to use your code please add the log output, and maybe it will help me on that.
Upvotes: 1