Android : How to Display selected Listview Image(Imageview) to other activity

on Friday, July 4, 2014




I am having a simple listview with an ImageView and a textView.




Now whenever I click on my Listview I need to send the ImageView to another activity.




I have read many SO answers but couldn't find a better solution for this.




This is my Activity Code.



mMatrixCursor = new MatrixCursor(new String[] { "_id", "name", "photo",
"details" });

// Adapter to set data in the listview
mAdapter = new SimpleCursorAdapter(getBaseContext(),
R.layout.lv_layout, null, new String[] { "name", "photo",
"details" }, new int[] { R.id.tv_name, R.id.iv_photo,
R.id.tv_details }, 0);

// Getting reference to listview
ListView lstContacts = (ListView) findViewById(R.id.lst_contacts);

// Setting the adapter to listview
lstContacts.setAdapter(mAdapter);

//mAdapter.notifyDataSetChanged();

lstContacts.setDividerHeight(0);

// Creating an AsyncTask object to retrieve and load listview with
// contacts
ListViewContactsLoader listViewContactsLoader = new ListViewContactsLoader();

// Starting the AsyncTask process to retrieve and load listview with
// contacts
listViewContactsLoader.execute();
lstContacts.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Constants.position = position;
Constants.id = Constants.ids_list.get(position);

// bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//Uri contacid = ContentUris.withAppendedId(Contacts.CONTENT_URI, id);

ByteArrayOutputStream stream = new ByteArrayOutputStream();
//bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent intent = new Intent(MainActivity.this, detail.class);
intent.putExtra("bmp", byteArray); // for image
intent.putExtra("pos", position);


startActivity(intent);
// startActivity(intent);

MainActivity.this.finish();
}


});
private class ListViewContactsLoader extends AsyncTask<Void, Void, Cursor> {

@Override
protected Cursor doInBackground(Void... params) {
Uri contactsUri = ContactsContract.RawContacts.CONTENT_URI;

// Querying the table ContactsContract.Contacts to retrieve all the
// contacts
Cursor contactsCursor = getContentResolver().query(contactsUri,
null, null, null,
ContactsContract.Contacts.DISPLAY_NAME + " ASC ");

if (contactsCursor.moveToFirst()) {
do {
long contactId = contactsCursor.getLong(contactsCursor
.getColumnIndex("_ID"));

Uri dataUri = ContactsContract.Data.CONTENT_URI;

// Querying the table ContactsContract.Data to retrieve
// individual items like
// home phone, mobile phone, work email etc corresponding to
// each contact
Cursor dataCursor = getContentResolver().query(
dataUri,
null,
ContactsContract.Data.RAW_CONTACT_ID + "="
+ contactId, null, null);

Constants.ids_list.add(contactId);

String displayName = "";
String nickName = "";
String homePhone = "";
String mobilePhone = "";
String workPhone = "";
String photoPath = "" + R.drawable.blank;
byte[] photoByte = null;

String title = "";

if (dataCursor.moveToFirst()) {
// Getting Display Name
displayName = dataCursor
.getString(dataCursor
.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
do {

// Getting NickName
if (dataCursor
.getString(
dataCursor
.getColumnIndex("mimetype"))
.equals(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE))
nickName = dataCursor.getString(dataCursor
.getColumnIndex("data1"));

// Getting Phone numbers
if (dataCursor
.getString(
dataCursor
.getColumnIndex("mimetype"))
.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
switch (dataCursor.getInt(dataCursor
.getColumnIndex("data2"))) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
homePhone = dataCursor.getString(dataCursor
.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
mobilePhone = dataCursor
.getString(dataCursor
.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
workPhone = dataCursor.getString(dataCursor
.getColumnIndex("data1"));
break;
}
}

// Getting Photo
if (dataCursor
.getString(
dataCursor
.getColumnIndex("mimetype"))
.equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)) {
photoByte = dataCursor.getBlob(dataCursor
.getColumnIndex("data15"));

if (photoByte != null) {
bitmap = BitmapFactory.decodeByteArray(
photoByte, 0, photoByte.length);
// Getting Caching directory
File cacheDirectory = getBaseContext()
.getCacheDir();

// Temporary file to store the contact image
File tmpFile = new File(
cacheDirectory.getPath() + "/wpta_"
+ contactId + ".png");

// The FileOutputStream to the temporary
// file
try {
FileOutputStream fOutStream = new FileOutputStream(
tmpFile);

// Writing the bitmap to the temporary
// file as png file
bitmap.compress(
Bitmap.CompressFormat.PNG, 100,
fOutStream);

// Flush the FileOutputStream
fOutStream.flush();

// Close the FileOutputStream
fOutStream.close();

} catch (Exception e) {
e.printStackTrace();
}

photoPath = tmpFile.getPath();

}

}

} while (dataCursor.moveToNext());

String details = "";

// Concatenating various information to single string
if (homePhone != null && !homePhone.equals(""))
details = " " + homePhone + "\n";
if (mobilePhone != null && !mobilePhone.equals(""))
details += " " + mobilePhone + "\n";
if (workPhone != null && !workPhone.equals(""))
details += " " + workPhone + "\n";
if (nickName != null && !nickName.equals(""))
details += " " + nickName + "\n";

if (title != null && !title.equals(""))
details += " " + title + "\n";

// Adding id, display name, path to photo and other
// details to cursor
mMatrixCursor.addRow(new Object[] {
Long.toString(contactId), displayName,
photoPath, details });


}

} while (contactsCursor.moveToNext());

}
return mMatrixCursor;
}




In the similar way how can I send the image as it is in ArrayList as shown above..Can anyone tel me some light on this usage


0 comments:

Post a Comment