ImageView的不正确更新 [英] ImageView does not update properly
问题描述
在我的应用程序,我有两个imageViews是相同的尺寸和两个正常工作。然而,当我把一个图片到每个ImageView的,然后尝试切换出前ImageView的用不同的画面,下方ImageView的结束得到了应该在顶部的ImageView去的图片。我不知道这是为什么。这里是我的xml文件:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直><的TextView
机器人:ID =@ + ID / personalizetextView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/自定义
机器人:TEXTSIZE =30sp
机器人:比重=中心
机器人:layout_marginTop =20dip/><的TextView
机器人:ID =@ + ID / personalizetextviewChangeBackground
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/ customizebackground
机器人:比重=中心/>< ImageView的
机器人:ID =@ + ID / imageView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:adjustViewBounds =真
安卓了maxWidth =100dp
安卓了maxHeight =100dp
机器人:scaleType =fitCenter
机器人:contentDescription =@字符串/ descForBackground /> <按钮
机器人:ID =@ + ID / btnChangeImage
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/ change_background/> <的TextView
机器人:ID =@ + ID / personalizetextviewChangeIcon
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/ change_icon
机器人:比重=中心/> < ImageView的
机器人:ID =@ + ID / imageView2Icon
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:adjustViewBounds =真
安卓了maxWidth =100dp
安卓了maxHeight =100dp
机器人:scaleType =fitCenter
机器人:contentDescription =@字符串/ descForIcon
/> <按钮
机器人:ID =@ + ID / btnChangeImageForIcon
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/ change_icon/>< / LinearLayout中>
然后这里是我的Java代码:
包com.example.awesomefilebuilderwidget;进口java.io.BufferedInputStream中;
进口java.io.FileInputStream中;
进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.database.Cursor;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.net.Uri;
进口android.os.Bundle;
进口android.provider.MediaStore;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.ImageView;公共类个性化延伸活动{
Button按钮;
ImageView的形象;
ImageView的IMAGE2;
按钮btnChangeImage;
按钮btnChangeImageForIcon;
私有静态最终诠释SELECT_PICTURE = 1;
私人字符串selectedImagePath;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.personalize);addListenerOnButton();}公共无效addListenerOnButton(){图像=(ImageView的)findViewById(R.id.imageView1);btnChangeImage =(按钮)findViewById(R.id.btnChangeImage);
btnChangeImage.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
意向意图=新的Intent();
intent.setType(图像/ *);
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(意向,SELECT_PICTURE);
}});
}公共字符串的getPath(URI URI){
的String [] =投影{MediaStore.Images.Media.DATA};
光标光标= getContentResolver()查询(URI,投影,NULL,NULL,NULL);
INT与Column_Index =光标
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
返回cursor.getString(Column_Index中);
}@覆盖
公共无效的onActivityResult(INT申请code,INT结果code,意图数据)
{
如果(结果code == RESULT_OK){
如果(要求code == SELECT_PICTURE)
{
乌里selectedImageUri = data.getData();
selectedImagePath =的getPath(selectedImageUri);
尝试{
的FileInputStream fileis =新的FileInputStream(selectedImagePath);
的BufferedInputStream bufferedstream =新的BufferedInputStream(fileis);
字节[] = bMapArray新的字节[bufferedstream.available()];
bufferedstream.read(bMapArray);
位图BMAP = BitmapFactory.de codeByteArray的(bMapArray,0,bMapArray.length);
//在这里,您可以将此/位图图像设置为按钮背景图片
image.setImageBitmap(BMAP); 如果(fileis!= NULL)
{
fileis.close();
}
如果(bufferedstream!= NULL)
{
bufferedstream.close();
}
}赶上(FileNotFoundException异常五){
e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
}
}
}图像=(ImageView的)findViewById(R.id.imageView2Icon);btnChangeImageForIcon =(按钮)findViewById(R.id.btnChangeImageForIcon);
btnChangeImageForIcon.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
意向意图=新的Intent();
intent.setType(图像/ *);
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(意向,SELECT_PICTURE);
}});
}公共布尔saveImageToInternalStorage(位图图像){
尝试{
FOS的FileOutputStream = this.openFileOutput(desiredFilename.png,Context.MODE_PRIVATE);
image.com preSS(Bitmap.Com pressFormat.PNG,100,FOS);
fos.close();
返回true;
}赶上(例外五){
返回false;
}
}
}
已更新code,我有工作:
包com.example.awesomefilebuilderwidget;进口公共类个性化扩展活动实现OnClickListener {
Button按钮;
ImageView的形象;
ImageView的IMAGE2;
按钮btnChangeImage;
按钮btnChangeImageForIcon;
私有静态最终诠释SELECT_PICTURE = 1;
私有静态最终诠释SELECT_PICTURE_2 = 2;
私人字符串selectedImagePath;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.personalize);按钮btnChangeImage =(按钮)findViewById(R.id.btnChangeImage);
btnChangeImage.setOnClickListener(本);
按钮btnChangeImageForIcon =(按钮)findViewById(R.id.btnChangeImageForIcon);
btnChangeImageForIcon.setOnClickListener(本);}@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
意向意图=新的Intent();
intent.setType(图像/ *);
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(意向,SELECT_PICTURE);
};公共字符串的getPath(URI URI){
的String [] =投影{MediaStore.Images.Media.DATA};
光标光标= getContentResolver()查询(URI,投影,NULL,NULL,NULL);
INT与Column_Index =光标
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
返回cursor.getString(Column_Index中);
}@覆盖
公共无效的onActivityResult(INT申请code,INT结果code,意图数据)
{
如果(结果code == RESULT_OK){
如果(要求code == SELECT_PICTURE)
{
乌里selectedImageUri = data.getData();
selectedImagePath =的getPath(selectedImageUri);
位图B1 = getAndDe $ C $的CImage(selectedImagePath);
如果(B1!= NULL){
image.setImageBitmap(B1);
}
}否则如果(要求code == SELECT_PICTURE_2)
{
乌里selectedImageUri = data.getData();
selectedImagePath =的getPath(selectedImageUri);
位图B2 = getAndDe $ C $的CImage(selectedImagePath);
如果(B2!= NULL){
image2.setImageBitmap(B2);
}
}
}
}私人位图getAndDe $ C $的CImage(字符串selectedImagePath){
尝试{
的FileInputStream fileis =新的FileInputStream(selectedImagePath);
的BufferedInputStream bufferedstream =新的BufferedInputStream(fileis);
字节[] = bMapArray新的字节[bufferedstream.available()];
bufferedstream.read(bMapArray);
位图BMAP = BitmapFactory.de codeByteArray的(bMapArray,0,bMapArray.length);
//在这里,您可以将此/位图图像设置为按钮背景图片
返回BMAP;如果(fileis!= NULL)
{
fileis.close();
}
如果(bufferedstream!= NULL)
{
bufferedstream.close();
}
}赶上(FileNotFoundException异常五){
e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
}返回null;
}
公共布尔saveImageToInternalStorage(位图图像){
尝试{
FOS的FileOutputStream = this.openFileOutput(desiredFilename.png,Context.MODE_PRIVATE);
image.com preSS(Bitmap.Com pressFormat.PNG,100,FOS);
fos.close();
返回true;
}赶上(例外五){
返回false;
}
}
}
您使用的是同样的结果code SELECT_PICTURE
这样的结果可以去任何一个。
您应该使用不同的结果codeS你正在启动两个动作。
修改
code,以帮助什么在评论中说:
@覆盖
公共无效的onActivityResult(INT申请code,INT结果code,意图数据)
{
如果(结果code == RESULT_OK){
如果(要求code == SELECT_PICTURE)
{
乌里selectedImageUri = data.getData();
selectedImagePath =的getPath(selectedImageUri);
位图B1 = getAndDe $ C $的CImage(selectedImagePath);
如果(B1!= NULL){
image.setImageBitmap(B1);
}
}否则如果(要求code == SELECT_PICTURE_2)
{
乌里selectedImageUri = data.getData();
selectedImagePath =的getPath(selectedImageUri);
位图B2 = getAndDe $ C $的CImage(selectedImagePath);
如果(B2!= NULL){
image2.setImageBitmap(B2);
}
}
}
}私人位图getAndDe $ C $的CImage(字符串selectedImagePath){
尝试{
的FileInputStream fileis =新的FileInputStream(selectedImagePath);
的BufferedInputStream bufferedstream =新的BufferedInputStream(fileis);
字节[] = bMapArray新的字节[bufferedstream.available()];
bufferedstream.read(bMapArray);
位图BMAP = BitmapFactory.de codeByteArray的(bMapArray,0,bMapArray.length); 如果(fileis!= NULL)
{
fileis.close();
}
如果(bufferedstream!= NULL)
{
bufferedstream.close();
}
返回BMAP;
}赶上(FileNotFoundException异常五){
e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
}
返回null;
}
In my app, I have two imageViews that are the same size and both work fine. However, when I put a picture into each imageView and then try to switch out the top imageView with a different picture, the bottom imageView ends up getting the picture that is supposed to go in the top imageView. I'm not sure why this is. Here is my xml file:
<?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>
And then here is my java coding:
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;
}
}
}
UPDATED CODE THAT I AM WORKING WITH:
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;
}
}
}
You are using the same resultCode SELECT_PICTURE
so the result can go to either one.
You should use different result codes for the two actions you are launching.
EDIT
Code to help what was said in comments:
@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;
}
这篇关于ImageView的不正确更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!