gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:28:51 25 4
gpt4 key购买 nike

我几天来一直在努力下定决心,但还没有成功。请善意地帮助我。我需要编写一个函数来接受 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();
}

}
}

最佳答案

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 的答案。

关于java - 接受 URL 作为参数并返回 Java 中的 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12922830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com