在我的应用程序中,我有两个大小相同且工作正常的 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_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="30sp"
    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:adjustViewBounds="true"  
    android:maxWidth="100dp"  
    android:maxHeight="100dp"  
    android:scaleType="fitCenter"  
    android:contentDescription="@string/descForBackground"
    />
   <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:adjustViewBounds="true"  
    android:maxWidth="100dp"  
    android:maxHeight="100dp"  
    android:scaleType="fitCenter"
    android:contentDescription="@string/descForIcon"
     />
  <Button
    android:id="@+id/btnChangeImageForIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    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;
   }
}
}