如何从数据库URL的图像中的ImageView
显示我试图让用户的个人资料页面。如何从数据库URL的图像中的ImageView
我已经做了一些艰苦的工作,并设法检索SQL数据库JSON对象并存储用户数据,并显示这些数据(串)转换成的TextView如姓名和电子邮件,工作正常。
我也有在同一个数据库中的URL给用户的照片,但我不能检索有关的ImageView就像其他文本字符串的URL和显示..
可能有人好心告诉我如何调整这个代码来做到这一点?
请从Java新手考虑这一点。谢谢! ^^
public class DirectoryDetailMeActivity extends Activity {
String eid; //user ID
JSONParser jsonParser = new JSONParser();
private static final String url_veiw_directory = "http://www.myweb.com/android/include/directory_detail_me.php";
private static final String TAG_ID = "eid";
private static final String TAG_IMG = "photo";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.directory_detail_me);
new GetDirectoryDetails().execute();
}
class GetDirectoryDetails extends AsyncTask<String, String, String> {
protected String doInBackground(String... params) {
runOnUiThread(new Runnable() {
public void run() {
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", eid));
JSONObject json = jsonParser.makeHttpRequest(url_veiw_directory, "GET", params);
Log.d("my profile", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
JSONArray directoryObj = json.getJSONArray(TAG_DIRECTORY);
JSONObject directory = directoryObj.getJSONObject(0);
// It works fine if I give the URL manually.
String imageURL = "";
ImageView imagePhoto = (ImageView) findViewById(R.id.photo);
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(imageURL, imagePhoto);
TextView txtName = (TextView) findViewById(R.id.name);
TextView txtEmail = (TextView) findViewById(R.id.email);
txtName.setText(directory.getString(TAG_NAME));
txtEmail.setText(directory.getString(TAG_EMAIL));
}else{
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
您需要填写imageURL从JSONObject获取URL。 我认为JSONObject有图像数组。
先试试这个。
String imageURL = directory.getString(TAG_IMG);
//then
ImageView imagePhoto = (ImageView) findViewById(R.id.photo);
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(imageURL, imagePhoto);
,如果它不一样,如果他们两人的无用功
JSONArray imageObj = directory.getJSONArray(TAG_IMG);
JSONObject img = imageObj.getJSONObject(0);
String imageURL = img.getString("NewTAG");
//then
ImageView imagePhoto = (ImageView) findViewById(R.id.photo);
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(imageURL, imagePhoto);
。 在ImageLoder类改变这一行
InputStream is = (InputStream) new URL(url).getContent();
这个
InputStream is = (InputStream) new URL(url).openConnection().getInputStream();
@Bans,谢谢你帮助我。不幸的是,两者都不起作用。它显示空白内容,包括名称和电子邮件档案.. – user1781367
尝试调试模式,并看看你的目录json对象。也许这个领域是空的 –
如果我把一个图像的网址,它工作正常.. – user1781367
不应该IMAGEURL = directory.getString(TAG_IMG)的地方?这是问题,还是imageLoader?你的意思是 – Zoleas
? String imageURL = directory.getString(TAG_IMG); – user1781367
当你做String imageURL = directory.getString(TAG_IMG);你的字符串是空的?如果是这样,你可以尝试登录你的json字符串吗? – Zoleas