简体   繁体   English

图库中的图像未设置为 imageview

[英]Image from Gallery is not being set to imageview

I have a listview and a button.我有一个列表视图和一个按钮。 When the button is clicked, it leads to the gallery where the user can choose a picture.单击该按钮时,它会转到用户可以选择图片的图库。 When I test my app and go to the gallery, when I select a picture, the imageview isn't updated with that picture.当我测试我的应用程序并转到图库时,当我选择一张图片时,imageview 不会更新为该图片。 Here is my imageview and button:这是我的图像视图和按钮:

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

<Button
    android:id="@+id/btnChangeImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/change_background" />

And my coding:还有我的编码:

package com.example.awesomefilebuilderwidget;

IMPORTS

public class Personalize extends Activity{
Button button;
ImageView image;
Button btnChangeImage;
private static final int SELECT_PICTURE = 1;
private String  selectedImagePath;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.personalize);

    addListenerOnButton();

}

public void addListenerOnButton() {

    image = (ImageView) findViewById(R.id.imageView1);
    image = (ImageView) findViewById(R.id.imageView2Icon);

    btnChangeImage = (Button) findViewById(R.id.btnChangeImage);
    btnChangeImage.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivityForResult(intent, SELECT_PICTURE);
        }

        public void onActivityResult(int requestCode, int resultCode, Intent data)
            {
                if (resultCode == RESULT_OK) {
                    if (requestCode == SELECT_PICTURE)
                    {
                        Uri selectedImageUri = data.getData();
                        selectedImagePath = getPath(selectedImageUri);
                        try {
                            FileInputStream fileis=new FileInputStream(selectedImagePath);
                            BufferedInputStream bufferedstream=new BufferedInputStream(fileis);
                            byte[] bMapArray= new byte[bufferedstream.available()];
                            bufferedstream.read(bMapArray);
                            Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
                            //Here you can set this /Bitmap image to the button background image

                            if (fileis != null) 
                            {
                                fileis.close();
                            }
                            if (bufferedstream != null) 
                            {
                                bufferedstream.close();
                            }
                        } catch (FileNotFoundException e) {                 
                            e.printStackTrace();
                        } catch (IOException e) {                   
                            e.printStackTrace();
                        }               
                    }
                }
            }


        public String getPath(Uri uri) {
                String[] projection = { MediaStore.Images.Media.DATA };
                Cursor cursor = managedQuery(uri, projection, null, null, null);
                int column_index = cursor
                        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            }

    });


    } 
}

ADDED: Full code for xml:添加: xml 的完整代码:

    <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <TextView
    android:id="@+id/personalizetextView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/customize" 
    android:textSize="30dip"
    android:gravity="center"
    android:layout_marginTop="20dip"/>

  <TextView 
    android:id="@+id/personalizetextviewChangeBackground"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/customizebackground"
    android:gravity="center" />

  <ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/pattern1"
    />

  <Button
    android:id="@+id/btnChangeImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/change_background" />

  <TextView 
    android:id="@+id/personalizetextviewChangeIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/change_icon"
    android:gravity="center" />

<ImageView
    android:id="@+id/imageView2Icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/pattern2" />

 <Button
    android:id="@+id/btnChangeImageForIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/change_icon" />

 </LinearLayout>

Full code for Personalize.java: Personalize.java 的完整代码:

package com.example.awesomefilebuilderwidget;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class Personalize extends Activity{
Button button;
ImageView image;
ImageView image2;
Button btnChangeImage;
private static final int SELECT_PICTURE = 1;
private String  selectedImagePath;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.personalize);

    addListenerOnButton();

}

public void addListenerOnButton() {

    image = (ImageView) findViewById(R.id.imageView1);

    btnChangeImage = (Button) findViewById(R.id.btnChangeImage);
    btnChangeImage.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivityForResult(intent, SELECT_PICTURE);
        }

    });


    } 

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE)
        {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            try {
                FileInputStream fileis=new FileInputStream(selectedImagePath);
                BufferedInputStream bufferedstream=new BufferedInputStream(fileis);
                byte[] bMapArray= new byte[bufferedstream.available()];
                bufferedstream.read(bMapArray);
                Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
                //Here you can set this /Bitmap image to the button background image

                if (fileis != null) 
                {
                    fileis.close();
                }
                if (bufferedstream != null) 
                {
                    bufferedstream.close();
                }
            } catch (FileNotFoundException e) {                 
                e.printStackTrace();
            } catch (IOException e) {                   
                e.printStackTrace();
            }               
        }
    }
  }
}

I will add the other coding to update the icon since I can just carry over the same coding and modify the names of resources as needed我将添加其他编码来更新图标,因为我可以保留相同的编码并根据需要修改资源名称

It looks like onActivityResult() and getPath() are both inside your OnClickListener .看起来onActivityResult()getPath()都在您的OnClickListener Try moving them out to be methods of Personalize instead.尝试将它们移出成为Personalize方法。 You'll also need to add @Override to onActivityResult() .您还需要将@Override添加到onActivityResult()

I don't see the line where you set the ImageView with the selected "picture".我没有看到您使用所选“图片”设置 ImageView 的行。

After this line:在这一行之后:

Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);

Add this:添加这个:

image.setImageBitmap(bMap);

Try setting the image like this:尝试像这样设置图像:

     Uri selectedImageUri = data.getData();
     selectedImagePath = getPath(selectedImageUri);
     Bitmap bmp = BitmapFactory.decodeFile(selectedImagePath);
     image.setImageBitmap(bmp);

Instead of do:而不是做:

        Uri selectedImageUri = data.getData();
        selectedImagePath = getPath(selectedImageUri);
        try {
            FileInputStream fileis=new FileInputStream(selectedImagePath);
            BufferedInputStream bufferedstream=new BufferedInputStream(fileis);
            byte[] bMapArray= new byte[bufferedstream.available()];
            bufferedstream.read(bMapArray);
            Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
            //Here you can set this /Bitmap image to the button background image

            if (fileis != null) 
            {
                fileis.close();
            }
            if (bufferedstream != null) 
            {
                bufferedstream.close();
            }
        } catch (FileNotFoundException e) {                 
            e.printStackTrace();
        } catch (IOException e) {                   
            e.printStackTrace();
        } 

Let me know if this didn't solve the problem.如果这不能解决问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM