1

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/AHDT2IL4GNz5V3cB8Uq439pyGcG0pt4uAWUs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6MK7faJ58qSv1kIGhjFvVNz+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>

ここにマニフェストがあります

<?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 はその背景を変更しておらず、例外も見られません。誰か助けてくれませんか

4

1 に答える 1

0

これを試して:

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 で使用し、相対変数を変更する必要があります。

于 2013-10-03T16:07:36.837 に答える