-3

I download several jpeg files and I show them in the items on a ListView. Then, when the user taps on an item of the listview, I want to show again the corresponding jpeg file plus some additional info, on a new screen, in a new activity. I use an intent to start the new activity, but I don't know how to pass the jpeg file. Is it possible to pass it as an extra to the intent?

Here's the whole activity that contains the intent to start the second activity, after I tried to implement the method shown below by @geet :

public class DisplayActivity extends Activity {
private MySQLiteHelper dbHelper; 
private SQLiteDatabase db;
private ListView lv_custom;
ArrayList<Content> contents = new ArrayList<Content>();
Content content0 = new Content();
Cursor c;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display);
    lv_custom = (ListView) findViewById(android.R.id.list);

    dbHelper = new MySQLiteHelper(this); 
    db = dbHelper.getWritableDatabase();
    LoadImgAsyncTask l = new LoadImgAsyncTask(contents);
    l.execute();

}

private class Content {
    private Drawable img;
    private String phoneNr;
    private String address;
}

private class ViewHolder{
    ImageView image;
    TextView textViewPhone;
    TextView textViewAddress;
}

private class CustomListViewAdapter extends BaseAdapter{        
    private int layoutResource;
    private ArrayList<Content> mArrayList;
    private LayoutInflater inflater;

    public CustomListViewAdapter(Context mContext, int layoutResource, ArrayList<Content> mArrayList) {
        this.layoutResource = layoutResource;
        this.mArrayList = mArrayList;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        convertView = inflater.inflate(layoutResource, null);
        viewHolder = new ViewHolder();

        viewHolder.image = (ImageView) convertView.findViewById(R.id.imageView1);
        viewHolder.textViewPhone = (TextView) convertView.findViewById(R.id.phoneNr);
        viewHolder.textViewAddress = (TextView) convertView.findViewById(R.id.address);

        viewHolder.image.setImageDrawable(mArrayList.get(position).img);
        viewHolder.textViewPhone.setText(mArrayList.get(position).phoneNr);
        viewHolder.textViewAddress.setText(mArrayList.get(position).address);

        return convertView;
    }   
}

private Drawable loadImageFromWeb(String url)
   {
  try
  {
   InputStream is = (InputStream) new URL(url).getContent();
   Drawable d = Drawable.createFromStream(is, "src name");
   return d;
  }catch (Exception e) {
   System.out.println("Exc="+e);
   return null;
  }}

private class LoadImgAsyncTask extends AsyncTask<Void, Void, ArrayList<Content>>{
    private ProgressDialog progressDialog;
    private ArrayList<Content> cnt;
    LoadImgAsyncTask(ArrayList<Content> cnt){
        this.cnt = cnt;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(DisplayActivity.this);
        progressDialog.setMessage("Downloading logos. Please wait some more...");
        progressDialog.setIndeterminate(true);
        progressDialog.show();
    }
    @Override
    protected ArrayList<Content> doInBackground(Void ...voids ) {
        String[] selectionArguments = {};
        ////////c = db.rawQuery("SELECT * FROM " + MySQLiteHelper.TABLE_STORES + " GROUP BY " + MySQLiteHelper.COLUMN_NAME + " HAVING COUNT(" + MySQLiteHelper.COLUMN_NAME +") < 2", selectionArguments);
        c = db.rawQuery("SELECT * FROM " + MySQLiteHelper.TABLE_STORES, selectionArguments);
        if (c.getCount() != 0) {
        try {
            while (c.moveToNext()) {
                content0 = new Content();
                //add a new store information to the ArrayList
                content0.img = loadImageFromWeb(c.getString(6));
                content0.phoneNr = c.getString(7);
                content0.address = c.getString(1);
                contents.add(content0);
            } 
        } catch (Exception e) {
            Log.v("Exception e ",e.fillInStackTrace().toString());
        }}
        else {
            Log.v("Cursor ", 0 + "");
            Toast.makeText(getApplicationContext(), 
                    "Unfortunately the connection is bad. Try again later.",
                    Toast.LENGTH_SHORT).show();
        }
        db.close();
        return contents;
    }
    @Override
    protected void onPostExecute(ArrayList<Content> cnt) {
        super.onPostExecute(cnt);   
        progressDialog.hide();
        progressDialog.dismiss();

        CustomListViewAdapter clva = new CustomListViewAdapter(DisplayActivity.this, R.layout.one_row, cnt);
        lv_custom.setAdapter(clva);

        lv_custom.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                c.moveToPosition(position);
                String adr = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_ADDRESS));
                String city = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_CITY));
                String name = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_NAME));
                String store_id = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_STORE_ID));
                String phone = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_PHONE));
                String zip = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_ZIP));
                String state = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_STATE));
                Double latitude = c.getDouble(c.getColumnIndex(MySQLiteHelper.COLUMN_LAT));
                Double longitude = c.getDouble(c.getColumnIndex(MySQLiteHelper.COLUMN_LONG));

                Intent intent=new Intent(DisplayActivity.this,DisplayInfoActivity.class);
                intent.putExtra("Address",adr);
                intent.putExtra("City", city);
                intent.putExtra("Zip", " " + zip + ", ");
                intent.putExtra("State", state);
                intent.putExtra("Name", "Store name: " + name);
                intent.putExtra("StoreID", "Store ID: " + store_id);
                intent.putExtra("Phone", "Phone number: " + phone);
                intent.putExtra("Latitude", "Latitude=" + latitude);
                intent.putExtra("Longitude", "Longitude=" + longitude);

                final ImageView imageView = (ImageView)v.findViewById(R.id.imageView1);
                final BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
                final Bitmap myBitmap = bitmapDrawable.getBitmap();
                intent.putExtra("BitmapImage", myBitmap);

                startActivity(intent);
            }
    });
}
}
}

Here's the second activity:

public class  DisplayInfoActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ImageView logo = (ImageView) findViewById(R.id.imageView2);
    setContentView(R.layout.list_entry);
    TextView address = (TextView) findViewById(R.id.address_entry);
    TextView city = (TextView) findViewById(R.id.city_entry);
    TextView zip = (TextView) findViewById(R.id.zip_entry);
    TextView state = (TextView) findViewById(R.id.state_entry);
    TextView name = (TextView) findViewById(R.id.name_entry);
    TextView store_id = (TextView) findViewById(R.id.store_id_entry);
    TextView phone = (TextView) findViewById(R.id.number_entry);
    TextView latitude = (TextView) findViewById(R.id.latitude_entry);
    TextView longitude = (TextView) findViewById(R.id.longitude_entry);
    Intent intent = getIntent();

    Bitmap img = (Bitmap) intent.getParcelableExtra("BitmapImage");
    if (logo == null)
        Log.v("MY TAG ","logo is null");
    else Log.v("MY TAG ","logo is NOT null");
    logo.setImageBitmap(img);

    address.setText(intent.getStringExtra("Address"));
    city.setText(intent.getStringExtra("City"));
    zip.setText(intent.getStringExtra("Zip"));
    state.setText(intent.getStringExtra("State"));
    name.setText(intent.getStringExtra("Name"));
    store_id.setText(intent.getStringExtra("StoreID"));
    phone.setText(intent.getStringExtra("Phone"));
    latitude.setText(intent.getStringExtra("Latitude"));
    longitude.setText(intent.getStringExtra("Longitude"));
}

}

And here's a fragment from LogCat:

12-02 20:01:04.854: V/MY TAG(23878): logo is null
12-02 20:01:04.854: D/AndroidRuntime(23878): Shutting down VM
12-02 20:01:04.854: W/dalvikvm(23878): threadid=1: thread exiting with uncaught exception (group=0x419a32a0)
12-02 20:01:04.854: E/AndroidRuntime(23878): FATAL EXCEPTION: main
12-02 20:01:04.854: E/AndroidRuntime(23878): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bottlerocket/com.example.bottlerocket.DisplayInfoActivity}: java.lang.NullPointerException
12-02 20:01:04.854: E/AndroidRuntime(23878):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
12-02 20:01:04.854: E/AndroidRuntime(23878):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
12-02 20:01:04.854: E/AndroidRuntime(23878):    at android.app.ActivityThread.access$700(ActivityThread.java:134)
12-02 20:01:04.854: E/AndroidRuntime(23878):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
12-02 20:01:04.854: E/AndroidRuntime(23878):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 20:01:04.854: E/AndroidRuntime(23878):    at android.os.Looper.loop(Looper.java:137)
12-02 20:01:04.854: E/AndroidRuntime(23878):    at android.app.ActivityThread.main(ActivityThread.java:4867)
12-02 20:01:04.854: E/AndroidRuntime(23878):    at java.lang.reflect.Method.invokeNative(Native Method)
12-02 20:01:04.854: E/AndroidRuntime(23878):    at java.lang.reflect.Method.invoke(Method.java:511)
12-02 20:01:04.854: E/AndroidRuntime(23878):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
12-02 20:01:04.854: E/AndroidRuntime(23878):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
12-02 20:01:04.854: E/AndroidRuntime(23878):    at dalvik.system.NativeStart.main(Native Method)
12-02 20:01:04.854: E/AndroidRuntime(23878): Caused by: java.lang.NullPointerException
12-02 20:01:04.854: E/AndroidRuntime(23878):    at com.example.bottlerocket.DisplayInfoActivity.onCreate(DisplayInfoActivity.java:33)
12-02 20:01:04.854: E/AndroidRuntime(23878):    at android.app.Activity.performCreate(Activity.java:5047)
12-02 20:01:04.854: E/AndroidRuntime(23878):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-02 20:01:04.854: E/AndroidRuntime(23878):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
12-02 20:01:04.854: E/AndroidRuntime(23878):    ... 11 more
Monica
  • 389
  • 1
  • 7
  • 19

3 Answers3

2

Yes its possible.

you can pass it on onclick event of your listview.

final ImageView imageView = (ImageView) view.findViewById(R.id.idOfImageviewInListview);
                final BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
                final Bitmap yourBitmap = bitmapDrawable.getBitmap();
                Intent intent = new Intent(currentclass.this, Secondclass.class);
                intent.putExtra("BitmapImage", yourBitmap);

in Secondclass:

Intent intent = getIntent();
Bitmap img = (Bitmap) intent.getParcelableExtra("BitmapImage");

EDIT :

Please Change this line

final ImageView imageView = (ImageView) findViewById(R.id.imageView1);

To

final ImageView imageView = (ImageView)v.findViewById(R.id.imageView1);

EDIT : You can not fetch current view's object before setting the view.

Change

 super.onCreate(savedInstanceState);
    ImageView logo = (ImageView) findViewById(R.id.imageView2);
    setContentView(R.layout.list_entry);

To:

 super.onCreate(savedInstanceState);
  setContentView(R.layout.list_entry);
 ImageView logo = (ImageView) findViewById(R.id.imageView2);
Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44
  • I tried to use this method but it does not work for me. I get some NullPointerException but in the LogCat it does not tell me where is the source of this exception. It appears on a line after the line 12-01 22:28:01.711: D/skia(13062): --- SkImageDecoder::Factory returned null – Monica Dec 02 '13 at 06:34
  • Have u change id of imageview according your listview on this `final ImageView imageView = (ImageView) view.findViewById(R.id.image);` line? – Bhoomika Brahmbhatt Dec 02 '13 at 06:46
  • Yes, I replaced it with final ImageView imageView = (ImageView) findViewById(R.id.imageView1); I thingk that there is a typo in your line, that it should be (ImageView) findViewById(R.id.image); – Monica Dec 02 '13 at 06:53
  • please put debug point on this lines nd debug it. & show your error code. – Bhoomika Brahmbhatt Dec 02 '13 at 08:30
  • I added the edited code to the onclick event. Then in the second activity I added ImageView logo = (ImageView) findViewById(R.id.imageView2); followed by your 2 lines of code, followed by logo.setImageBitmap(img); But here logo is null so I get a NullPointerException. Is there something missing or wrong in the second activity? Why is logo null? Thanks for your advice so far. – Monica Dec 03 '13 at 03:14
  • Please post Logcat & second Activity. Try to put debug points & debug it. – Bhoomika Brahmbhatt Dec 03 '13 at 03:52
  • I posted the second activity and the LogCat. I'll try to debug it. – Monica Dec 03 '13 at 04:08
  • It works!! I noticed that the line ImageView logo = (ImageView) findViewById(R.id.imageView2); was written before the layout for the activity is declared. Great! Thanks a lot! – Monica Dec 03 '13 at 04:12
  • Happy to help!! If my answer solves your issue. u can accept it as an Answer. – Bhoomika Brahmbhatt Dec 03 '13 at 04:18
1

This:

ImageView logo = (ImageView) findViewById(R.id.imageView2);

must be AFTER this:

setContentView(R.layout.list_entry);
gian1200
  • 3,670
  • 2
  • 30
  • 59
0

You can also pass the path of image using intent to second activity.

Looking Forward
  • 3,579
  • 8
  • 45
  • 65