gpt4 book ai didi

android - 从 url 获取图像并在 android 上设置为相对布局背景

转载 作者:行者123 更新时间:2023-11-29 14:50:29 25 4
gpt4 key购买 nike

基本上我想做的是,使用图像的url(下载与否无关紧要)来设置relativelayout的背景

我在谷歌上搜索了几个小时并尝试了很多建议,但我总是以错误和应用程序崩溃告终...

if((!modelsArrayList.get(position).isGroupHeader())&&       (modelsArrayList.get(position).isProfBox())) {
rowView = inflater.inflate(R.layout.prof_header, parent, false);
TextView titleView = (TextView) rowView.findViewById(R.id.header);
titleView.setText(modelsArrayList.get(position).getTitle());
RelativeLayout rLayout=(RelativeLayout)rowView.findViewById(R.id.ProfHead);
rLayout.setBackgroundResource("HTTP://URL.HERE");

我试过其他类似的 ( http://looksok.wordpress.com/2013/07/06/android-tutorial-download-image-from-the-internet-with-url/ ),但没有用

最佳答案

您提到的链接已经给出了答案!就用那个方法吧。应该是这样的。两件重要的事情!在 AndroidManifest.xml 中添加Internet Permission。在 UI Create 中应用严格模式。见下文

MainActivity.java

package com.example.imagedownload;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);

// About StrictMode Learn More at => http://stackoverflow.com/questions/8258725/strict-mode-in-android-2-2
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

Bitmap myImage = getBitmapFromURL("http://looksok.files.wordpress.com/2011/12/me.jpg");

RelativeLayout rLayout=(RelativeLayout)findViewById(R.id.relativeLayout);

//BitmapDrawable(obj) convert Bitmap object into drawable object.
Drawable dr = new BitmapDrawable(myImage);
rLayout.setBackgroundDrawable(dr);


return true;
}

public Bitmap getBitmapFromURL(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
>

</RelativeLayout>

关于android - 从 url 获取图像并在 android 上设置为相对布局背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19785099/

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