我想在相机捕获新图像或使用ContentObserver添加到图库的任何图像文件时将图像发送到服务器我的代码在服务中运行我在服务上注册了ContentObserver onCrate方法每件事都运行正常我正在尝试将他的图像压缩到基础64并且我想将这个转换后的imgae发送到服务器,但问题是当我在此行之后从相机捕获图像时
ByteArrayOutputStream stream = new ByteArrayOutputStream();
我的代码没有继续,所以我的图像没有被传输,因为那些代码行没有被执行 这是代码
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是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;
}}
请帮助我,我无法检测到没有错误但我的代码没有在流程中执行的问题 提前谢谢
这里是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
答案 0 :(得分:1)
无法在没有错误日志的情况下找出错误,但转换位图时似乎遇到了问题。
尝试将此图片转换为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;
}
}
工作正常;试着用它。我希望它可以帮助你。
如果你想使用你的代码,请添加日志输出,也许它会帮助我。