user1835337 user1835337 - 6 months ago 29
Android Question

Set image to the listview from intent

I have list view with image and text and I can call gallery intent onClick.

here ih my custom row:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="4px"
android:src="@drawable/ic_launcher" >
</ImageView>

<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label" >
</TextView>

</LinearLayout>


and main code:

public class MyActivity extends ListActivity {

private ListView lv;
private ArrayList<String> itemArray;
private ArrayAdapter<String> itemAdapter;

//new gallery intent privates
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;

public void onCreate(Bundle icicle) {
super.onCreate(icicle);

itemArray = new ArrayList<String>();
itemArray.clear();

itemAdapter = new ArrayAdapter<String>(this, R.layout.row, R.id.label, itemArray);
setListAdapter(itemAdapter);

}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Toast.makeText(this, selectedImagePath, Toast.LENGTH_SHORT).show();
}
}
}

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

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();

// select a file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}

protected void addItemList() {
// TODO Auto-generated method stub
itemArray.add(0,"step");
itemAdapter.notifyDataSetChanged();
}


// menu options
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add("add");
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
addItemList();
Toast.makeText(this, "test", Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
}

}


How can I set selected image to the clicked list item imageView? Can't find any solution how to connect them/
(I know how to load images if I know their dirrectory (I don't need it here), but I don't know how to set them after an intent)

VVB VVB
Answer

Paste below code in your onActivityResult method :

        if (requestCode == PICK_FROM_GALLERY) {
            if (resultCode != RESULT_CANCELED) {
                Bundle extras = data.getExtras();
                if (extras != null) {
                photo = extras.getParcelable("data");
                photo.setImageBitmap(photo);                
            }
         }
      }