55859

ImageView does not update properly

Question:

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; } } }

Answer1:

You are using the same resultCode SELECT_PICTUREso 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; }

Recommend

  • Remove xml tag during xstream parsing?
  • ActionBar with appcompat library v7 (ava.lang.IllegalStateException: You need to use a Theme.AppComp
  • Property file in java
  • How do you create a simple Automation Extender for Visual Studio with UITypeEditor
  • Typescript sourcemaps not loaded in chrome
  • Android Studio cannot find resources when building for device with version > 5.0
  • Gruntfile.js - Throwing error 'Recursive process.nextTick detected\"
  • Launching a new command window from Golang in Windows
  • Where to get the .java files of a netbeans project?
  • maven-dependency-plugin ignores outputDirectory configuration
  • draw a B+ tree in latex
  • Usage of local dart library
  • Error building image: Error building rlang - not available on R 3.5.0 (Uploading shiny)
  • Is it possible to make imports depend on the location of my Lua script instead of the current direct
  • Install different versions of nuget packages inside one solution file with two projects
  • JPA/EclipseLink Returning No Results
  • Calling java project from Mathematica
  • Grunt watch Running “watch” task Waiting
  • Returning this from a constructor function in JS
  • integration of function in modelica
  • How can I determine which routines MATLAB uses to solve a sparse matrix?
  • Basic defensive programming [duplicate]
  • Change device language on Android 6.0 (Android M)
  • Add Windows Feature from C#
  • TFS - how do I sum child task hours to parent
  • python: forcing relative imports to search from script file
  • Hide buttons on title bar in Java
  • Spring: No transaction manager has been configured
  • How to make R's read_csv2() recognise the text characters properly
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • Read text file that is not in the main package in a runnable jar
  • Roxygen error “Skipping invalid path”
  • How to create a file in java without a extension
  • OpenGL ES texture problem, 4 duplicate columns and horizontal lines (Android)
  • Cross-Platform Protobuf Serialization
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • How to format a variable of double type
  • coudnt use logback because of log4j
  • JaxB to read class hierarchy