如何去code图像串从PHP服务器位图图像? [英] How to decode the image string to bitmap image from php server?
问题描述
在我的应用程序必须从图库中选择图像,并将其设置为资料图片。对于我都设有codeD图像作为字符串,并将其发送到服务器。正从服务器的响应后,我要脱code中的字符串图像为位图从服务器接收image.While响应它显示了以下错误
In my application i have to select an image from gallery and set it as an profile picture. For that i have encoded the image as string and send it to the server. After getting the response from server i want to decode the string image to bitmap image.While receiving response from the server it shows the following error
java.lang.IllegalArgumentException: bad base-64
at android.util.Base64.decode(Base64.java:161)
at android.util.Base64.decode(Base64.java:136)
at android.util.Base64.decode(Base64.java:118)
下面code我使用的base64方法将位图图像转换为字符串,
Below code i use the base64 method to convert the bitmap image to string,
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
profileimage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
profileimage.buildDrawingCache();
Bitmap bm = profileimage.getDrawingCache();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
// Resize the image
double width = bm.getWidth();
double height = bm.getHeight();
double ratio = 100 / width;
int newheight = (int) (ratio * height);
System.out.println("———-width" + width);
System.out.println("———-height" + height);
System.out.println("———-height" + newheight);
bm = Bitmap.createScaledBitmap(bm, 100, newheight, true);
// Here you can define .PNG as well
bm.compress(Bitmap.CompressFormat.JPEG, 95, bao);
byte[] ba = bao.toByteArray();
ba1 = Base64.encodeToString(ba, Base64.NO_WRAP);
System.out.println("uploading image now ——–" + ba1);
}
}
code服务器调用
Code for server call
public String makeServiceCall1(String url1, String a, final Context context) {
this.url = url1;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("profileimage", a));
try {
if (!isConnected(context)) {
return url;
}
if (isConnected(context) == false) {
Toast.makeText(context, "No Connection Available", 500).show();
}
URI uri = new URI(url.replace(" ", "%20"));
Log.d("uri", url + "");
HttpPost httppost = new HttpPost(uri);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpParams httpParameters = httppost.getParams();
int timeoutConnection = 100000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
int timeoutSocket = 100000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
int responseCode = response.getStatusLine().getStatusCode();
if (response != null) {
msg = EntityUtils.toString(entity);
}
Log.d("serverResponse", msg);
} catch (Exception e) {
e.printStackTrace();
}
return msg;
// TODO Auto-generated method stub
}
code的图像刺痛转换为位图图像,
Code for convert the image sting to bitmap image,
byte[] decodedString = Base64.decode(image, Base64.NO_WRAP);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,
decodedString.length);
profilepic.setImageBitmap(decodedByte);
请任何一个可以帮助我解决这个问题。
Please any one help me to solve the issue.
推荐答案
请恩$ C $使用此套餐C
Please Encode using this package
import org.kobjects.base64.Base64;
和也使用了这种
ba1 = Base64.encodeToString(ba, Base64.DEFAULT);
这篇关于如何去code图像串从PHP服务器位图图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!