gpt4 book ai didi

java - android位图不显示

转载 作者:行者123 更新时间:2023-12-01 04:19:00 27 4
gpt4 key购买 nike

我需要从 Base64 图像中获取图像并将其设置为位图图像的背景。我看不到背景图像的变化。我使用异步任务也是为了避免主线程上的网络。这是代码:

public class MainActivity extends Activity {
ImageView templateImage;
String templateImageURL;
Bitmap bm;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
templateImage = (ImageView) findViewById(R.id.templateImage);
templateImageURL = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5OjcBCgoKDQwNGg8PGjclHyU3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3N//AABEIAH8AqQMBIgACEQEDEQH/xAAbAAADAQEBAQEAAAAAAAAAAAADBAUCAQYAB//EAEUQAAEDAgIFCAYIBAQHAAAAAAEAAgMEERIhBTFBUWETIkJScYGRoRQjMsHR4QYVYnKCkrHwQ1NjoiSj0uIzNDVEVFVz/8QAGQEAAwEBAQAAAAAAAAAAAAAAAQIEAwAF/8QAJhEAAgEEAQQCAgMAAAAAAAAAAAECAxESEwQhMUFRFGEisQUVQv/aAAwDAQACEQMRAD8A9VHUPtZrWDtRLSnVh7ipgqT1URtQdgKq3FOJQEcl/wDiNHemGscB7TCpPLO23C0KkjpBHcHAqHD0neBuiRgWyc6ykCqz1+a2Kjj5rt52Baa1u0uC2GQ7S5RWSuOq4CYjc89IrvkDKBTwQWye4LBsPZPfZLsa93TKMyKTY5yHyBtZvI+05yzgZfWPBEbDJb2nLvo7+tbtR+QdrBDCNq+dINx/Kvnxka3hYxFupy75B2s2wU7zzp2sO4tK7JQROP8AzkSUkmdvQXyOOQLV2/7F1jTqGJv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WCfkivpQNTYx4paShZrLGEfeN1VkYeslZIicy6x7F6MK7faJ58qSv1kIGhjFvVNz+0uHR8BGbQOxybLHj+IfBZc15FjIe4KiNW/+TCULPuJO0dB1T+ZAloIWtJwuHG4TroXdfy+aG6E9JybOXoXGPsk+i/ZJC+9Hb/KKfkgtqse5Y5LgENj8hwRTDVoN4eSA2ob+wiCdnHwU7oyH2IJhN9q6Gi97WKFy7DqHkuiePVZLpkHZEYaFsBKieNuxbbVt2DLsXPjyDtiPM4FHjNipzKxpzARG1wHdsSS4sjSNdFiJ2q6eilDRkNXavPs0iNgRm6SA2alPLiS9G8K6PW0FQMXOHisaRrGtPNDVAp9MRs51iT2LNbphkmZa5Zrju9rFeyON7haiucScmjsSclU52seSSk0kwkjknEdgQXVrSbe5WQ40V4IKld+xt8l+igueT0HeCEanFaxI7l8ai2vX2KmNO3ZE7nfydc77Lh2hDLt4N101OSG6oFr5eCdRl6M217OEoZ4rj6hv7CGalu8+Ca0hbo648FjDwC+5VpzzX3KM6qSUJMdSif/2Q==";
}

public class ImageDownload extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
bm = convertBitmap(templateImageURL);
return null;
}

@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (!templateImageURL.equals("")) {
templateImage.setImageBitmap(bm);
}
}

}

public Bitmap convertBitmap(String img) {
byte[] b = Base64.decode(img, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
}

这是 XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context=".MainActivity" >

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/templateImage" />

</RelativeLayout>

这是 list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.imageurlproject"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.imageurlproject.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Imageview 没有改变它的背景,我也没有看到任何异常。有人可以帮忙吗

最佳答案

试试这个:

public class ImageDownload extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
bm = convertBitmap(templateImageURL);
return bm;
}

@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (!templateImageURL.equals("")) {
templateImage.setImageBitmap(bm);
}
}

}

因为我认为位图应该返回而不是 null。

如果不起作用,请使用此方法将您的 Base64 转换为位图。

  public Bitmap StringToBitMap(String encodedString){
try{
byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
}catch(Exception e){
e.getMessage();
return null;
}
}

你应该在doInBackground中使用它,并更改相对变量。

关于java - android位图不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19161977/

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