Android 如何将JSON中的图像(字节数组)显示到imageView中。。。工厂返回空值
我已经和这个问题斗争了两天了。。。之前发布过,但没有一个答案是正确的 我的问题是:我正在尝试将字节[]转换为图像。字节[]来自JSON,格式如下: “4ocwue5hdqoacgaaa1jsersaaaaaafwaaibgaadbhwqrdsaaaalwsflzaaaajwaaaccbkgnighpagaeleqvr4xzpcrmk9ecwszcozfcofw7c5vcvw/CqXp…”这适用于另外100条生产线 用于显示图像的类:Android 如何将JSON中的图像(字节数组)显示到imageView中。。。工厂返回空值,android,json,android-listview,Android,Json,Android Listview,我已经和这个问题斗争了两天了。。。之前发布过,但没有一个答案是正确的 我的问题是:我正在尝试将字节[]转换为图像。字节[]来自JSON,格式如下: “4ocwue5hdqoacgaaa1jsersaaaaaafwaaibgaadbhwqrdsaaaalwsflzaaaajwaaaccbkgnighpagaeleqvr4xzpcrmk9ecwszcozfcofw7c5vcvw/CqXp…”这适用于另外100条生产线 用于显示图像的类: public class PlayersListActivit
public class PlayersListActivity extends Activity {
// URL to make request
private static String URL = "url goes here";
private static int userID;
ArrayList<HashMap<String, Object>> playersList;
ListView playerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
Bundle extras = getIntent().getExtras();
userID = extras.getInt("id");
initLayout();
}
private void initLayout() {
playerView = (ListView) findViewById(R.id.list);
playersList = new ArrayList<HashMap<String, Object>>();
playerView.setAdapter(new SimpleAdapter(PlayersListActivity.this, playersList, R.layout.activity_players_list, null, null));
playerView.setDividerHeight(0);
}
@Override
protected void onResume() {
if (!ConnectivityStatus.isNetworkAvailable(getApplicationContext())) {
Toast.makeText(PlayersListActivity.this, R.string.network_failed,
Toast.LENGTH_SHORT).show();
} else {
playerView.setAdapter(null);
new PlayersLoadTask().execute();
}
super.onResume();
}
ProgressDialog pDialog;
class PlayersLoadTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(PlayersListActivity.this);
pDialog.setMessage("Reading data");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
playersList.clear();
}
@Override
protected String doInBackground(String... arg0) {
try {
ArrayList<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
parameters.add(new BasicNameValuePair("request", "getPlayers"));
parameters.add(new BasicNameValuePair("clubid", Integer.toString(userID))); // TODO: you'll need to change this to the Id from the user
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(PlayersListActivity.URL);
httpPost.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
// if this is null the web service returned an empty page
if(httpEntity == null) // response is empty so exit out
return null;
String jsonString = EntityUtils.toString(httpEntity);
// again some simple validation around the returned string
if(jsonString != null && jsonString.length() != 0) // checking string returned from service to grab id
{
JSONArray jsonArray = new JSONArray(jsonString);
for(int i=0; i< jsonArray.length(); i++)
{
HashMap<String, Object> map = new HashMap<String, Object>();
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
int id = jsonObject.getInt("id");
String name = jsonObject.getString("name");
byte[] image = jsonObject.getString("image").getBytes();
String base64 = jsonObject.getString("image");
try {
BitmapFactory.Options op=new BitmapFactory.Options();
op.inSampleSize=8;
Bitmap yourSelectedImage = BitmapFactory.decodeByteArray(image,0,image.length);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, baos);
//this will convert image to byte[]
byte[] byteArrayImage = baos.toByteArray();
// this will convert byte[] to string
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
if (encodedImage != null) {
ImageView imgView = (ImageView) findViewById(R.id.image);
imgView.setImageBitmap(yourSelectedImage);
}
} catch (Exception e) {
Log.d("Exception", e.toString());
}
//map.put("id", id);
map.put("image", base64.toString());
map.put("name", name);
//Log.d("JSON OBJECTS:", jsonObject.toString());
//Log.d("WHATS IN MAP:", map.toString());
playersList.add(map);
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
Log.e("ERROR SOMEWHERE!!!! " , e.toString());
}
return null;
}
protected void onPostExecute(String file_url) {
if(playersList.size() == 0 ) {
Toast.makeText(PlayersListActivity.this, "No players in a list" , Toast.LENGTH_SHORT).show();
} else {
playerView.setAdapter(new PlayerListAdapter (PlayersListActivity.this, playersList, R.layout.activity_players_list, new String[] {"id", "image", "name"}, new int[] {R.id.player_list_id, R.id.image, R.id.name, }));
}
pDialog.dismiss();
}
}
我完全不知道为什么我的位图返回null。我试过很多不同的方法,但都不管用
这就是我急需帮助的原因!谢谢
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray , 0, byteArray .length);
iv.setImageBitmap(bitmap );
此代码将仅对字节数组中的图像进行解码。以供将来参考
JSONArray jsonArray = new JSONArray(jsonString);
for (int i = 0; i < jsonArray.length(); i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
int id = jsonObject.getInt("id");
String name = jsonObject.getString("name");
byte[] byteArray = Base64.decode(jsonObject.getString("image"), Base64.DEFAULT) ;
Bitmap bmp1 = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
//Bitmap bitmap = Bitmap.createScaledBitmap(bmp1, 120, 120, false);
map.put("id", id);
map.put("name", name);
map.put("image", bmp1);
playersList.add(map);
//Log.d("SHOW PLAYER LIST: " ,playersList.toString());
}
JSONArray-JSONArray=新的JSONArray(jsonString);
for(int i=0;i
Ok。但是我也尝试过使用字符串,但它不起作用。您是从中获取编码的base 64字符串还是它返回null?您的意思是:ByteArrayOutputStream=new ByteArrayOutputStream();byte[]byteArray=stream.toByteArray();字符串base64=base64.encodeToString(byteArray,0);它返回null.think so String base64=jsonObject.getString(“image”);此字符串为emptyString base64返回此4ocwue5hdqoacgaaa1jsersaaaafwaa…Ewq5CYOKAmg==如果图像较大,则会显示outofmemoryError
,因此Jakubee的答案是正确的!!!它对我不起作用,但仍将位图值返回为null@RajaPriyan对于(int i=0;iJSONArray jsonArray = new JSONArray(jsonString);
for (int i = 0; i < jsonArray.length(); i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
int id = jsonObject.getInt("id");
String name = jsonObject.getString("name");
byte[] byteArray = Base64.decode(jsonObject.getString("image"), Base64.DEFAULT) ;
Bitmap bmp1 = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
//Bitmap bitmap = Bitmap.createScaledBitmap(bmp1, 120, 120, false);
map.put("id", id);
map.put("name", name);
map.put("image", bmp1);
playersList.add(map);
//Log.d("SHOW PLAYER LIST: " ,playersList.toString());
}