“我怎么能?”;更新;Android中的横幅?
我有一个应用程序,上面有一条横幅,上面有新闻,当我想放其他新闻时,我需要打开代码并更改resource.jpg和链接。有没有一种方法可以在不修改代码的情况下更改横幅和链接(或者至少是横幅)?Idk可能会把它上传到网页或类似的东西。“我怎么能?”;更新;Android中的横幅?,android,banner,Android,Banner,我有一个应用程序,上面有一条横幅,上面有新闻,当我想放其他新闻时,我需要打开代码并更改resource.jpg和链接。有没有一种方法可以在不修改代码的情况下更改横幅和链接(或者至少是横幅)?Idk可能会把它上传到网页或类似的东西。 谢谢我的建议是将banner.jpg上传到您的应用程序可以访问并动态加载的服务器。这将避免每次你想更改横幅时都必须更新你的应用程序,并使其更干净(没有过多的Google Play更新)。要实际加载图像,可以使用以下代码: ImageView image1 = (Ima
谢谢我的建议是将banner.jpg上传到您的应用程序可以访问并动态加载的服务器。这将避免每次你想更改横幅时都必须更新你的应用程序,并使其更干净(没有过多的Google Play更新)。要实际加载图像,可以使用以下代码:
ImageView image1 = (ImageView) findViewById(R.id.mybanner);
new Thread(new Runnable(){//create a new thread so we can do network operations
@Override
public void run() {//main thread function
try {//attempt to do network stuff
URL url = new URL("http://your-hosting-site.com/banner.jpg");//create aURL object with the path to your banner
HttpURLConnection con = (HttpURLConnection) url.openConnection();//create the connection object from the url
con.setReadTimeout(15000);
con.setConnectTimeout(15000);
con.setRequestMethod("GET");
con.setDoInput(true);
con.connect();//connect to the server
InputStream is = con.getInputStream();//get the stream so we can read the image
Drawable d = Drawable.createFromStream(is, "MyBanner");//create a drawable from the image
Bitmap bmp = ((BitmapDrawable) d).getBitmap();//create a bitmap from the drawable
final Drawable dS = new BitmapDrawable(Bitmap.createScaledBitmap(bmp, 192, 192, true));//scale it to whatever size you need
con.disconnect();//disconnect now that we're done
runOnUiThread(new Runnable(){//run UI update code on the main thread
@Override
public void run() {
image1.setImageDrawable(dS);//set the imageview to the banner we downloaded
}
});
} catch (MalformedURLException e) {//catch url error
e.printStackTrace();
} catch (IOException e) {//catch io error when downloading
e.printStackTrace();
}
}
}).start();//run the thread
将“”(第6行)更改为上载.jpg的位置,将R.id.mybanner(第1行)更改为ImageView的id,并将“mybanner”(第14行)更改为希望调用图像的任何内容
您可能希望将您的横幅保存到手机上,并仅在X天/小时后检查更新以保存数据,但这取决于您。是什么阻止您在运行时动态更改imageView?@amerite1,感谢您的回答。。我完全是新手,我不明白,因为每次我上传横幅到,例如imageshack.com,链接都会改变,所以我需要改变代码,对吗?或者我不明白Dinamicaly的意思:SI建议你获得一个免费的网络托管帐户,这样你就可以上传你的图片,并有一个展示你的应用程序的网站。这样,您可以上传文件,路径将保持不变。我个人成功地使用了000webhost,但你可以看看外面有什么。请注意,您不能仅将其服务用于文件托管;您将需要某种网站来遵守他们的tos