找到你要的答案

Q:Load from Image from XML to ImageView

Q:从XML到ImageView加载图像

I want to load an image from an XML file tag <image> to the ImageView.

The layout file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:clickable="false">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

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

        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/jobtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:textSize="14sp"
            android:textStyle="bold"
            android:text="jobtitle">
        </TextView>

        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/jobinfo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="11sp"
            android:textStyle="normal"
            android:text="jobinfo"
            android:paddingLeft="10dp">
        </TextView>

    </LinearLayout>

</LinearLayout>

and the mainActivity:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".MainActivity" tools:ignore="MergeRootFrame" >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

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

        <ListView android:id="@+id/android:list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             />

    </LinearLayout>

</FrameLayout>

At this point i look up if the file already exists, if not, then download and parse it.

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    File file = new File(this.getFilesDir(), "Arbeitsauftrag_Bootsbau_Meier_vom_20150216.kx_job");
            if (file.exists()) {

                XMLParser parser = new XMLParser();
                Document doc = parser.getDomElement(readFromFile("Arbeitsauftrag_Bootsbau_Meier_vom_20150216.kx_job"));

                NodeList nodeListProject = doc.getElementsByTagName(KEY_PROJECT);
                NodeList nodeListTasks = doc.getElementsByTagName(KEY_TASK);

                //Schleife für Aufgaben
                for (int i = 0; i < nodeListTasks.getLength(); i++) {
                    HashMap<String, String> map = new HashMap<>();
                    Element e = (Element) nodeListTasks.item(i);

                    map.put(KEY_UUID_OBJ, parser.getValue(e, KEY_UUID_OBJ));
                    map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                    map.put(KEY_INFO, parser.getValue(e, KEY_INFO));
                    map.put(KEY_OBJECT, parser.getValue(e, KEY_OBJECT));
                    map.put(KEY_LOCATION, parser.getValue(e, KEY_LOCATION));
                    map.put(KEY_OBJECT_ID, parser.getValue(e, KEY_OBJECT_ID));
                    map.put(KEY_OBJECT_SNR, parser.getValue(e, KEY_OBJECT_SNR));
                    map.put(KEY_REGISTRATION_ID, parser.getValue(e, KEY_REGISTRATION_ID));
                    map.put(KEY_TASKIMAGE, parser.getValue(e, KEY_TASKIMAGE));
                    map.put(KEY_TASK_HEADLINE, parser.getValue(e, KEY_TASK_HEADLINE));
                    map.put(KEY_TASK_SUBJECT, parser.getValue(e, KEY_TASK_SUBJECT));
                    map.put(KEY_TASK_ACTION, parser.getValue(e, KEY_TASK_ACTION));
                    map.put(KEY_TASK_PRIORITY_COLOR, parser.getValue(e, KEY_TASK_PRIORITY_COLOR));
                    map.put(KEY_TASK_STATUS, parser.getValue(e, KEY_TASK_STATUS));
                    map.put(KEY_TASKIMAGE, parser.getValue(e,KEY_TASKIMAGE));

                    taskItems.add(map);
                }

                // Schleife für Auftragsinfos
                for (int i = 0; i < nodeListProject.getLength(); i++) {

                    // Neue HashMap erstellen
                    HashMap<String, String> map = new HashMap<>();
                    Element e = (Element) nodeListProject.item(i);

                    // Jedes Kind-Knoten zur HashMap
                    map.put(KEY_UUID, parser.getValue(e, KEY_UUID));
                    map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
                    map.put(KEY_JOBTITLE, parser.getValue(e, KEY_JOBTITLE));
                    map.put(KEY_JOBINFO, parser.getValue(e, KEY_JOBINFO));


                    //Hashmap zur ArrayList hinzufügen
                    projectItems.add(map);
                }

                ListAdapter adapter = new SimpleAdapter(ListViewActivity.this, projectItems,
                        R.layout.list_item_projects,
                        new String[]{KEY_JOBTITLE, KEY_JOBINFO},
                        new int[]{R.id.jobtitle, R.id.jobinfo});

                setListAdapter(adapter);
                //file.delete();


            } else {
                DownloadXMLFiles dlxmlf = new DownloadXMLFiles();
                dlxmlf.execute();
            }

This method is for decoding the base64 string:

 public static Bitmap decodeBase64(String input) {
        byte[] decodedByte = Base64.decode(input, Base64.DEFAULT);
        return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
    }

My ImageView is not at the MainActivity.

I'm looking how i can put the image from Base64 to Bitmap and set it as Image on my ImageView?

P.S. my ImageView isn't in the same Layout as my setContentView(R.layout.activity_main).

我想从一个XML文件标签& lt加载图像;图像>;到ImageView。

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:clickable="false">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

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

        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/jobtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:textSize="14sp"
            android:textStyle="bold"
            android:text="jobtitle">
        </TextView>

        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/jobinfo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="11sp"
            android:textStyle="normal"
            android:text="jobinfo"
            android:paddingLeft="10dp">
        </TextView>

    </LinearLayout>

</LinearLayout>

与主要活动:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".MainActivity" tools:ignore="MergeRootFrame" >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

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

        <ListView android:id="@+id/android:list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             />

    </LinearLayout>

</FrameLayout>

在这一点上,我看看文件是否已经存在,如果没有,然后下载并解析它。

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    File file = new File(this.getFilesDir(), "Arbeitsauftrag_Bootsbau_Meier_vom_20150216.kx_job");
            if (file.exists()) {

                XMLParser parser = new XMLParser();
                Document doc = parser.getDomElement(readFromFile("Arbeitsauftrag_Bootsbau_Meier_vom_20150216.kx_job"));

                NodeList nodeListProject = doc.getElementsByTagName(KEY_PROJECT);
                NodeList nodeListTasks = doc.getElementsByTagName(KEY_TASK);

                //Schleife für Aufgaben
                for (int i = 0; i < nodeListTasks.getLength(); i++) {
                    HashMap<String, String> map = new HashMap<>();
                    Element e = (Element) nodeListTasks.item(i);

                    map.put(KEY_UUID_OBJ, parser.getValue(e, KEY_UUID_OBJ));
                    map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                    map.put(KEY_INFO, parser.getValue(e, KEY_INFO));
                    map.put(KEY_OBJECT, parser.getValue(e, KEY_OBJECT));
                    map.put(KEY_LOCATION, parser.getValue(e, KEY_LOCATION));
                    map.put(KEY_OBJECT_ID, parser.getValue(e, KEY_OBJECT_ID));
                    map.put(KEY_OBJECT_SNR, parser.getValue(e, KEY_OBJECT_SNR));
                    map.put(KEY_REGISTRATION_ID, parser.getValue(e, KEY_REGISTRATION_ID));
                    map.put(KEY_TASKIMAGE, parser.getValue(e, KEY_TASKIMAGE));
                    map.put(KEY_TASK_HEADLINE, parser.getValue(e, KEY_TASK_HEADLINE));
                    map.put(KEY_TASK_SUBJECT, parser.getValue(e, KEY_TASK_SUBJECT));
                    map.put(KEY_TASK_ACTION, parser.getValue(e, KEY_TASK_ACTION));
                    map.put(KEY_TASK_PRIORITY_COLOR, parser.getValue(e, KEY_TASK_PRIORITY_COLOR));
                    map.put(KEY_TASK_STATUS, parser.getValue(e, KEY_TASK_STATUS));
                    map.put(KEY_TASKIMAGE, parser.getValue(e,KEY_TASKIMAGE));

                    taskItems.add(map);
                }

                // Schleife für Auftragsinfos
                for (int i = 0; i < nodeListProject.getLength(); i++) {

                    // Neue HashMap erstellen
                    HashMap<String, String> map = new HashMap<>();
                    Element e = (Element) nodeListProject.item(i);

                    // Jedes Kind-Knoten zur HashMap
                    map.put(KEY_UUID, parser.getValue(e, KEY_UUID));
                    map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
                    map.put(KEY_JOBTITLE, parser.getValue(e, KEY_JOBTITLE));
                    map.put(KEY_JOBINFO, parser.getValue(e, KEY_JOBINFO));


                    //Hashmap zur ArrayList hinzufügen
                    projectItems.add(map);
                }

                ListAdapter adapter = new SimpleAdapter(ListViewActivity.this, projectItems,
                        R.layout.list_item_projects,
                        new String[]{KEY_JOBTITLE, KEY_JOBINFO},
                        new int[]{R.id.jobtitle, R.id.jobinfo});

                setListAdapter(adapter);
                //file.delete();


            } else {
                DownloadXMLFiles dlxmlf = new DownloadXMLFiles();
                dlxmlf.execute();
            }

该方法用于解码base64字符串:

 public static Bitmap decodeBase64(String input) {
        byte[] decodedByte = Base64.decode(input, Base64.DEFAULT);
        return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
    }

我的ImageView是不是在主流。

我想我可以把图像从base64位图并将它设置为我的这个形象?

我不是一个ImageView布局作为我的setContentView(r.layout。activity_main)。

answer1: 回答1:

Create following method to convert from encoded Base64 string to Bitmap

Bitmap decodeBase64(String input) {
        try {
            byte[] decodedByte = Base64.decode(input, 0);
            return BitmapFactory.decodeByteArray(decodedByte, 0,
                    decodedByte.length);
        } catch (Exception e) {
            return null;
        }
    }

Now use it as follows-

Bitmap imageBitmap = decodeBase64(strEncodedBitmap);
imageView.setImageBitmap(imageBitmap);

创建如下方法从编码Base64字符串转换为位图

Bitmap decodeBase64(String input) {
        try {
            byte[] decodedByte = Base64.decode(input, 0);
            return BitmapFactory.decodeByteArray(decodedByte, 0,
                    decodedByte.length);
        } catch (Exception e) {
            return null;
        }
    }

现在使用它如下—

Bitmap imageBitmap = decodeBase64(strEncodedBitmap);
imageView.setImageBitmap(imageBitmap);
java  android  android-layout  android-imageview