ImageView 没有正确更新

Posted

技术标签:

【中文标题】ImageView 没有正确更新【英文标题】:ImageView does not update properly 【发布时间】:2013-10-21 19:57:25 【问题描述】:

在我的应用程序中,我有两个尺寸相同且工作正常的 imageView。但是,当我将图片放入每个 imageView 中,然后尝试用不同的图片切换顶部的 imageView 时,底部的 imageView 最终会得到应该放在顶部 imageView 中的图片。我不确定这是为什么。这是我的 xml 文件:

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

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

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_
    android:layout_
    android:adjustViewBounds="true"  
    android:maxWidth="100dp"  
    android:maxHeight="100dp"  
    android:scaleType="fitCenter"  
    android:contentDescription="@string/descForBackground"

    />

   <Button
    android:id="@+id/btnChangeImage"
    android:layout_
    android:layout_
    android:text="@string/change_background" />

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

 <ImageView
    android:id="@+id/imageView2Icon"
    android:layout_
    android:layout_
    android:adjustViewBounds="true"  
    android:maxWidth="100dp"  
    android:maxHeight="100dp"  
    android:scaleType="fitCenter"
    android:contentDescription="@string/descForIcon"
     />

  <Button
    android:id="@+id/btnChangeImageForIcon"
    android:layout_
    android:layout_
    android:text="@string/change_icon" />

</LinearLayout>

然后这是我的 java 编码:

package com.example.awesomefilebuilderwidget;

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

import android.app.Activity;
import android.content.Context;
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;
Button btnChangeImageForIcon;
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 = getContentResolver().query(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
            image.setImageBitmap(bMap);

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


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

btnChangeImageForIcon = (Button) findViewById(R.id.btnChangeImageForIcon);
btnChangeImageForIcon.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 boolean saveImageToInternalStorage(Bitmap image) 
   try 
      FileOutputStream fos = this.openFileOutput("desiredFilename.png", Context.MODE_PRIVATE);
      image.compress(Bitmap.CompressFormat.PNG, 100, fos);
      fos.close();   
      return true;
    catch (Exception e) 
   return false;
   


我正在使用的更新代码:

package com.example.awesomefilebuilderwidget;

IMPORTS

public class Personalize extends Activity implements OnClickListener 
Button button;
ImageView image;
ImageView image2;
Button btnChangeImage;
Button btnChangeImageForIcon;
private static final int SELECT_PICTURE = 1;
private static final int SELECT_PICTURE_2 = 2;
private String  selectedImagePath;

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

Button btnChangeImage = (Button) findViewById(R.id.btnChangeImage);    
btnChangeImage.setOnClickListener(this);
Button btnChangeImageForIcon = (Button) findViewById(R.id.btnChangeImageForIcon); 
btnChangeImageForIcon.setOnClickListener(this);



@Override
public void onClick(View v) 
// 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 = getContentResolver().query(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);
    Bitmap b1 = getAndDecodeImage(selectedImagePath);
    if(b1 != null)
        image.setImageBitmap(b1);
               
 else if (requestCode == SELECT_PICTURE_2)

    Uri selectedImageUri = data.getData();
    selectedImagePath = getPath(selectedImageUri);
    Bitmap b2 = getAndDecodeImage(selectedImagePath);
    if(b2 != null)
        image2.setImageBitmap(b2);
    
    



private Bitmap getAndDecodeImage(String  selectedImagePath)
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
return bMap;

if (fileis != null) 

    fileis.close();

if (bufferedstream != null) 

    bufferedstream.close();

 catch (FileNotFoundException e)                  
e.printStackTrace();
 catch (IOException e)                    
e.printStackTrace();
 

return null;



public boolean saveImageToInternalStorage(Bitmap image) 
   try 
      FileOutputStream fos = this.openFileOutput("desiredFilename.png", Context.MODE_PRIVATE);
      image.compress(Bitmap.CompressFormat.PNG, 100, fos);
      fos.close();   
      return true;
    catch (Exception e) 
   return false;
   


【问题讨论】:

image = (ImageView) findViewById(R.id.imageView2Icon); ???为什么不使用两个不同的引用? 你能解释一下你的意思吗? 最初,您将 image 设置为引用顶部图像视图 image = (ImageView) findViewById(R.id.imageView1);。然后你改变的是下部图像视图。然后你改变图像。你为什么不使用两个参考,例如图像视图1,图像视图2。 所以我把第二张图片改成了image2 = (ImageView) findViewById(R.id.imageView2Icon); 是这个意思吗? (只是确保) 是的。当你想设置顶部图像视图时,image1.setImageBitmap()、image2.setImageBitmap() 设置底部一个。 imageTop 和 imageBottom 可能是更好的名字? 【参考方案1】:

您使用的是相同的 resultCode SELECT_PICTURE所以结果可以转到任何一个。 您应该为要启动的两个操作使用不同的结果代码。

编辑

帮助 cmets 中所说的代码:

@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);
        Bitmap b1 = getAndDecodeImage(selectedImagePath);
        if(b1 != null)
            image.setImageBitmap(b1);
                   
     else if (requestCode == SELECT_PICTURE_2)
    
        Uri selectedImageUri = data.getData();
        selectedImagePath = getPath(selectedImageUri);
        Bitmap b2 = getAndDecodeImage(selectedImagePath);
        if(b2 != null)
            image2.setImageBitmap(b2);
        
        
    


private Bitmap getAndDecodeImage(String  selectedImagePath)
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);

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

【讨论】:

随你所愿...例如在开头旁边的另一个:private static final int SELECT_PICTURE_2 = 2; 我将它和底部的实例更改为SELECT_PICTURE_2,但运行程序时仍然遇到同样的问题 尝试将两段代码放在onActivityResult 方法中,而不是使用这个SecondonActivityResult 请使用方便的方法进行所有图像解码,您正在重复很多代码...... “方便法”是什么意思?既然你这么说,我想知道如何简化这一点,因为我摆脱了SecondonActivityResult,但随后又为 SELECT_PICTURE_2 代码创建了另一个 if 语句

以上是关于ImageView 没有正确更新的主要内容,如果未能解决你的问题,请参考以下文章

XCode 5 ImageView 在 ios 6 中消失

ImageView 不更新 onActivityResult 的变化

如何正确设置像 Swift 中的 imageContacts 一样的圆形 imageView?

如何让 imageView 占用 CollectionView 单元格?

带圆角的 CardView 内的 ImageView 绘制不正确

创建带圆角的 ImageView [重复]