接受URL作为参数并返回Java中的ImageView

时间:2012-10-16 20:24:45

标签: java android bitmap imageview

我试图让这个决心好几天但没有运气。请帮助我。 我需要编写一个函数来接受URL作为参数并返回ImageView。 有人可以告诉我如何修改此代码来做到这一点?它给了我imgURL.setImageBitmap(directory.getString(TAG_IMAGE));

下的红线
public class ViewProfileActivity extends Activity {

    ImageView imgURL;
    TextView txtName;

    String eid; 

    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();

    private static final String url_veiw_directory = "http://website.com/app/include/view_directory.php";

    private static final String TAG_SUCCESS = "success";
    private static final String TAG_DIRECTORY = "directory";
    private static final String TAG_ID = "eid";
    private static final String TAG_IMAGE = "image";
    private static final String TAG_NAME = "name";  

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_directory);

        Intent i = getIntent();

        eid = i.getStringExtra(TAG_ID);     


        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("Single Directory Details", json.toString());

                        success = json.getInt(TAG_SUCCESS);
                        if (success == 1) {                     

                            JSONArray directoryObj = json.getJSONArray(TAG_DIRECTORY);  
                            JSONObject directory = directoryObj.getJSONObject(0);

                            imgURL = (ImageView) findViewById(R.id.image);
                            txtName = (TextView) findViewById(R.id.name);                       


                            imgURL.setImageBitmap(directory.getString(TAG_IMAGE));
                            txtName.setText(directory.getString(TAG_NAME));                         


                        }else{

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });

            return null;
        }

        protected void onPostExecute(String file_url) {
            pDialog.dismiss();
        }

    }
}

2 个答案:

答案 0 :(得分:3)

http://developer.android.com/reference/android/widget/ImageView.html#setImageBitmap%28android.graphics.Bitmap%29

ImageView中没有setImageBitmap() - 方法,它接受字符串作为参数。你必须提供一个位图。

相反,您可以使用BitmapFactory创建位图,如下所示:

setImageBitmap(BitmapFactory.decodeFile(directory.getString(TAG_IMAGE)));

编辑:抱歉,忘了它是您要解码的网址。请参阅@MarchingHome答案。

答案 1 :(得分:2)

我想我看到了问题。 ImageView.setImageBitmap需要Bitmap个对象作为参数。 String不会做。 您应该尝试将JSONObject加载为Bitmap对象。

这可能是您正在寻找的答案: Loading a Bitmap from URL