I am trying to insert an image in ListView
and when I click on it to display the image in a new image view (Bigger View).
I don't have an Android phone to execute my application.
This is my ImageViewer class.
public class ImageViewer extends ListActivity {
Uri uri;
Cursor c;
ListView l;
ArrayList<String> al1 = new ArrayList<String>();
ArrayList<String> al2 = new ArrayList<String>();
int data, dname;
String dt, disnam, imgid;
int id;
String type;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String project[] = {MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID};
c = getContentResolver().query(uri, project, null, null, null);
data = c.getColumnIndex(MediaStore.Images.Media.DATA);
dname = c.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME);
id = c.getColumnIndex(MediaStore.Images.Media._ID);
if (c.moveToFirst()) {
do {
dt = c.getString(data);
disnam = c.getString(dname);
imgid = c.getString(id);
al1.add(dt);
al2.add(disnam);
} while (c.moveToNext());
}
l = getListView();
l.setAdapter(new MyAdapter());
l.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(
AdapterView<?> arg0,
View arg1, int arg2, long arg3
) {
String filename = al1.get(arg2);
Intent intent = new Intent(ImageViewer.this, Viewer.class);
intent.putExtra("image2view", filename);
intent.putExtra("type", type);
intent.putExtra("id", id);
startActivity(intent);
}
});
}
public class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int pos, View convertview, ViewGroup vg) {
// TODO Auto-generated method stub
View row = convertview;
if (row == null) {
LayoutInflater inf = getLayoutInflater();
row = inf.inflate(R.layout.image, vg, false);
}
TextView filename = (TextView) row.findViewById(R.id.ImageName);
filename.setText(al2.get(pos) + "\n");
ImageView thumbs = (ImageView) row.findViewById(R.id.Thumb);
thumbs.setImageResource(id);
thumbs.setScaleType(ImageView.ScaleType.CENTER_CROP);
thumbs.setPadding(8, 8, 28, 8);
return row;
}
}
}
This is my Viewer class, which has to display the selected image in full screen.
public class Viewer extends Activity{
ImageView iv;
TextView tv;
public void onCreate(Bundle b1){
super.onCreate(b1);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.imageview);
System.gc();
Bundle b2=getIntent().getExtras();
//String file=b2.getString("image2view");
int id=b2.getInt("id");
iv=(ImageView)findViewById(R.id.imageView101);
iv.setImageResource(id);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.help_title);
ImageView iv = (ImageView) findViewById(R.id.header);
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Intent();
setResult(RESULT_OK);
finish();
}
});
}
}
The xml file used for ImageViewer
class is image.xml.
<RelativeLayout
android:id="@+id/RelativeLayout02"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/mainbkgnd"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/Thumb"
android:layout_width="50dp"
android:layout_height="70dp"
android:src="@drawable/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_toRightOf="@+id/Thumb"
android:layout_height="wrap_content"
android:id="@+id/ImageName"
android:text="Image Name"
android:textColor="#000000">
</TextView>
</RelativeLayout>
The xml code for the Viewer class is imageview.xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/itextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:visibility="gone" />
<ImageView
android:id="@+id/imageView101"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.62"
android:src="@drawable/ic_launcher" />
</LinearLayout>
I searched but I couldn't find suitable code.
I don't believe that this id
:
id=c.getColumnIndex(MediaStore.Images.Media._ID);
Is the appropriate resource id
for:
iv.setImageResource(id);
You should be able to do this in Viewer's onCreate() instead:
String filename = b2.getString("filename");
if(filename != null)
iv.setImageUri(Uri.fromFile(new File(filename)));