java - 如何从数据库获取URL的图像以在ImageView中显示

标签 java android database imageview

我正在尝试制作一个用户个人资料页面。

我做了一些艰苦的工作,设法检索 json 对象并将用户数据存储在 SQL 数据库中,并将这些数据(字符串)显示到 TextView 中,例如姓名和电子邮件,效果很好。

我在同一数据库中也有用户照片的 URL,但我无法检索 URL 并像其他文本字符串一样显示在 ImageView 上。

有人可以告诉我如何调整这段代码来做到这一点吗?

请从 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();
        }

    }

}

最佳答案

您需要使用从 JSONObject 获取的 url 来填充 imageURL。 我认为 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();

关于java - 如何从数据库获取URL的图像以在ImageView中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13419801/

相关文章:

php - 搜索并打印多个查询的结果

java - 带有 Android : How to inject context when using MVP? 的 Dagger

java - 不同系统上的 SWT

java - ANDROID: If...Else 作为 Switch on String

java - 模式中限定词的使用

android - 如何在不堆叠行的情况下更改奇数行的 backgroundTint?

java - 应用引擎身份验证

java - 对插件进行单元测试 : cannot find symbol errors?

mysql - #1064 - 检查与您的 MariaDB 服务器版本对应的手册以获得正确的语法?

c# - 如何检查行是否存在?