我是通过从图库中获取图像从活动发送来自设备的图像,然后将其转换为字节数组和编码字符串。
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
//Log.d("gallery ---- > ", "" + data.getExtras().get("data"));
imageView.setImageURI(selectedImageUri);
imageView.setMaxHeight(200);
imageView.setMaxWidth(200);
imageView.setDrawingCacheEnabled(true);
BitmapFactory.Options options0 = new BitmapFactory.Options();
options0.inSampleSize = 2;
// options.inJustDecodeBounds = true;
options0.inScaled = false;
options0.inDither = false;
options0.inPreferredConfig = Bitmap.Config.ARGB_8888;
bmp = BitmapFactory.decodeFile(selectedImagePath);
ByteArrayOutputStream baos0 = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos0);
byte[] imageBytes0 = baos0.toByteArray();
Log.e("Byte Array:", imageBytes0.toString());
String encodedImage =Base64.encodeToString(imageBytes0,Base64.URL_SAFE);
JSONParser类
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONArray getJSONfromURL(String url) {
InputStream is = null;
String result = "";
JSONArray jArray = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpget = new HttpPost(url);
HttpResponse response = httpclient.execute(httpget);
// response =
HttpEntity entity = response.getEntity();
is = entity.getContent();
if(is!=null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}
jArray = new JSONArray(result);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jArray;
}
}
HttpPost httpget = new HttpPost(url); HttpResponse response = httpclient.execute(httpget);
Logcat跟踪
java.lang.IllegalArgumentException:索引161处的查询中的非法字符
图像字节数组编码的字符串在URL中大得多,可能是因为没有建立连接。
请建议我解决这个问题..
提前致谢