gpt4 book ai didi

java - (Android 应用程序)尝试将 AlertDialog .setIcon() 设置为从 URL 下载的图像

转载 作者:行者123 更新时间:2023-12-01 21:16:31 28 4
gpt4 key购买 nike

目前正在制作一个带有位置屏幕的应用程序,当按下 map 标记时,会弹出一个包含一些信息的警报对话框。我想将 AlertDialog 的图标设置为与来自 URL 的该位置关联的图像。图片本地存储位置太多。

 public Drawable pGraphic = null;

public class LoadImageFromWeb extends AsyncTask<String, Void, Drawable> {
protected Drawable doInBackground(String... urls) {


try {
InputStream is = (InputStream) new URL(urls[0]).getContent();

Drawable d = Drawable.createFromStream(is, "src name");
BT_debugger.showIt(d.toString());
return d;
} catch (Exception e) {
BT_debugger.showIt(e.toString());
return null;
}

}
protected void onPostExecute(Drawable result) {
BT_debugger.showIt("Image Loaded");
pGraphic = result;


}
}
//handleMarkerClick..
public void handleMarkerClick(final int markerIndex) {

BT_debugger.showIt(fragmentName + ":handleMarkerClick");

//vars used in callout bubble...
BT_item tappedItem = null;
String latitude = "";
String longitude = "";
String title = "";
String subTitle = "";
String loadScreenWithItemId = "";
String loadScreenWithNickname = "";
String calloutTapChoice = "";
Drawable pinGraphic = null;

//if marker index == -1 then the user tapped the device's location icon...
if(markerIndex == -1){
BT_debugger.showIt(fragmentName + ":handleMarkerClick. Device location tapped");

//use the graphic for the device location...
pinGraphic = getResources().getDrawable(R.drawable.bt_screen_map_marker_device);

//latitude / longitude is current location or last saved location...
if(mapView.isMyLocationEnabled()){

Location deviceLoc = mapView.getMyLocation();
latitude = String.valueOf(deviceLoc.getLatitude());
longitude = String.valueOf(deviceLoc.getLongitude());

//title, subTitle...
title = pokegoaus_appDelegate.rootApp.getRootDevice().getDeviceModel();
subTitle = getString(R.string.mapUserLocationDescription) + "\nLatitude: " + latitude + "\nLongitude: " + longitude;

}else{

//latitude / longitude is last current saved...
latitude = goaus_appDelegate.rootApp.getRootDevice().getDeviceLatitude();
longitude = goaus_appDelegate.rootApp.getRootDevice().getDeviceLongitude();

//title, subTitle...
title = goaus_appDelegate.rootApp.getRootDevice().getDeviceModel();
subTitle = getString(R.string.mapUserLocationDescription) + "\nLatitude: " + latitude + "\nLongitude: " + longitude;

}//isMyLocationEnabled...

}else{

//get the BT_item at this index...
tappedItem = childItems.get(markerIndex);
latitude = BT_strings.getJsonPropertyValue(tappedItem.getJsonObject(), "latitude", "");
longitude = BT_strings.getJsonPropertyValue(tappedItem.getJsonObject(), "longitude", "");
title = BT_strings.getJsonPropertyValue(tappedItem.getJsonObject(), "title", "");
subTitle = BT_strings.getJsonPropertyValue(tappedItem.getJsonObject(), "subTitle", "");
loadScreenWithItemId = BT_strings.getJsonPropertyValue(tappedItem.getJsonObject(), "loadScreenWithItemId", "");
loadScreenWithNickname = BT_strings.getJsonPropertyValue(tappedItem.getJsonObject(), "loadScreenWithNickname", "");
calloutTapChoice = BT_strings.getJsonPropertyValue(tappedItem.getJsonObject(), "calloutTapChoice", "");


String pinColor = BT_strings.getJsonPropertyValue(tappedItem.getJsonObject(), "pinColor", "red");
String locationImage = BT_strings.getJsonPropertyValue(tappedItem.getJsonObject(), "locationImage", "");
new LoadImageFromWeb().execute("http://pheds.com.au/example1.png");


BT_debugger.showIt(fragmentName + ":handleMarkerClick. Tapped: \"" + title + "\"");

}//not the device's location...

//are we showing the details button on this locations pup-up bubble?
boolean showDetailsButton = false;
if(loadScreenWithItemId.length() > 1 || loadScreenWithNickname.length() > 1){
showDetailsButton = true;
}

//if we are showing directions we are not showing details bubble...
if(loadScreenWithItemId.equalsIgnoreCase("showDirections")){
showDetailsButton = false;
}

//if we did not find load screen from id or nickname, look for a load sceen object...
if(!showDetailsButton){
try{
if(tappedItem != null){
JSONObject obj = tappedItem.getJsonObject();
if(obj.has("loadScreenObject")){
showDetailsButton = true;
}
}
}catch(Exception e){
showDetailsButton = false;
}
}
pinGraphic = pGraphic;
final AlertDialog myAlert = new AlertDialog.Builder(this.getActivity()).create();
myAlert.setTitle(title);
myAlert.setMessage(subTitle);
myAlert.setIcon(pinGraphic);
myAlert.setCancelable(false);
myAlert.setButton2(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myAlert.dismiss();
} });

//do we show details button?
if(showDetailsButton){
myAlert.setButton(getString(R.string.details), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myAlert.dismiss();
showDetailsScreen(markerIndex);
} });
}

//do we show the driving directions button?
if(loadScreenWithItemId.equalsIgnoreCase("showDirections") || calloutTapChoice.equalsIgnoreCase("showDirections")){
myAlert.setButton(getString(R.string.mapDrivingDirections), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myAlert.dismiss();
showDirections(markerIndex);
} });
}

//show...
myAlert.show();

}

似乎正在下载图像。但我不确定如何处理下载的图像并在我的 AlertDialog 中使用它。由于 LoadImageFromWeb 是 AsyncTask 类,我不确定如何将结果用作 Drawable。

提前致谢

克里斯坦·霍尔斯

完美的应用程序

最佳答案

看来您没有等待 Drawable 被下载。您立即将其设置为 AlertDialog 的图标。

您最好使用 Glide用于从网络下载和显示图像。它比标准的 AsyncTask 更高效、更易于使用。以下是使用 Glide 的方法 -

Glide.with(this)
.load("http://pheds.com.au/example1.png")
.into(new SimpleTarget<GlideDrawable>(drawableWidth, drawableHeight) {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
myAlert.setIcon(resource);
}
});

编辑:导入以下内容 -

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

关于java - (Android 应用程序)尝试将 AlertDialog .setIcon() 设置为从 URL 下载的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39941946/

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