0

Since yesterday I've tried every solution here in SO for "How to pass image in second activity" and related. None of them helped me so I would need some help. I don't have problem to pass another strings etc. but image didn't want to go into activity2. Here is my code and what I take and tried from this thread

FirstActivity

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {

      Intent intent = new Intent(Restaurants.this, RestaurantInformation.class);

      intent.putExtra("id", restaurantAdaptor.getItemId(position));
      intent.putExtra("text", stocks[position].text); 
      intent.putExtra("name", stocks[position].name);

      ByteArrayOutputStream bStream = new ByteArrayOutputStream();
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);
      byte[] byteArray = bStream.toByteArray();

      intent.putExtra("image", byteArray);
      startActivity(intent);
      finish();
  }

 });

In the received activity I have this

String text, name;
ImageView imgView;

long rest_id;
TextView txtView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.restaurant_informations);

    txtView = (TextView) findViewById(R.id.name);
    txtView = (TextView) findViewById(R.id.text);
    imgView = (ImageView) findViewById(R.id.image1);

    Bundle b = getIntent().getExtras();
    if (b != null) {

        rest_id = b.getLong("id");
        txtView.setText(rest_id+"");

        name = b.getString("name");
        ((TextView)findViewById(R.id.name)).setText(name+"");

        text = b.getString("text");
        ((TextView)findViewById(R.id.text)).setText(text+"");


        Bitmap bmp;

        byte[] byteArray = getIntent().getByteArrayExtra("image");
        bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

        imgView.setImageBitmap(bmp); 

When I click on listview item the app crash and in the LogCat I get this

11-18 04:16:25.746: E/AndroidRuntime(1799): FATAL EXCEPTION: main
11-18 04:16:25.746: E/AndroidRuntime(1799): Process: com.reserveme, PID: 1799
11-18 04:16:25.746: E/AndroidRuntime(1799): java.lang.NullPointerException
11-18 04:16:25.746: E/AndroidRuntime(1799):     at com.reserveme.Restaurants$1.onItemClick(Restaurants.java:101)
11-18 04:16:25.746: E/AndroidRuntime(1799):     at android.widget.AdapterView.performItemClick(AdapterView.java:299)
11-18 04:16:25.746: E/AndroidRuntime(1799):     at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
11-18 04:16:25.746: E/AndroidRuntime(1799):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
11-18 04:16:25.746: E/AndroidRuntime(1799):     at android.widget.AbsListView$3.run(AbsListView.java:3638)
11-18 04:16:25.746: E/AndroidRuntime(1799):     at android.os.Handler.handleCallback(Handler.java:733)
11-18 04:16:25.746: E/AndroidRuntime(1799):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-18 04:16:25.746: E/AndroidRuntime(1799):     at android.os.Looper.loop(Looper.java:136)
11-18 04:16:25.746: E/AndroidRuntime(1799):     at android.app.ActivityThread.main(ActivityThread.java:5017)
11-18 04:16:25.746: E/AndroidRuntime(1799):     at java.lang.reflect.Method.invokeNative(Native Method)
11-18 04:16:25.746: E/AndroidRuntime(1799):     at java.lang.reflect.Method.invoke(Method.java:515)
11-18 04:16:25.746: E/AndroidRuntime(1799):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-18 04:16:25.746: E/AndroidRuntime(1799):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-18 04:16:25.746: E/AndroidRuntime(1799):     at dalvik.system.NativeStart.main(Native Method)

Line number 101 in Restaurants is

bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);
Community
  • 1
  • 1
Goro
  • 499
  • 1
  • 13
  • 31
  • Your log report says you have another problem not about the problem you are asking. there is a NullPointerException in Restaurants.java:101. please check that line why it gets null. – Sayem Nov 18 '14 at 09:36
  • I don't know why it gets null since I get the image on first activity and everything is shown correctly there. – Goro Nov 18 '14 at 09:41
  • can you tell me in which line you are getting null? – Sayem Nov 18 '14 at 09:42
  • It is in the question under the LogCat -> `bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);` – Goro Nov 18 '14 at 09:42
  • did you declared bitmap and bStream correctly? if you're getting the error on that line, all code posted before is irrelevant since it looks alright. i guess – PedroHawk Nov 18 '14 at 09:45
  • I have for bitmap `public Bitmap bitmap;` – Goro Nov 18 '14 at 09:49
  • In that case i think "bitmap" is null. please check when performing click it gets null or not. You just declared it " public Bitmap bitmap;" please initialize that bitmap – Sayem Nov 18 '14 at 09:52
  • According to `LogCat` is null but the image is there? – Goro Nov 18 '14 at 09:54
  • you need to initialize your bitmap by your image. I think you haven't done that. – Sayem Nov 18 '14 at 09:56

2 Answers2

2

You should save the image to some location in phone and then pass the URI of that saved image through intent. And access that URI in second activity.

use this code to save file

String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "filename.jpg"); // the File to save to
fOut = new FileOutputStream(file);

Bitmap pictureBitmap = getImageBitmap(myurl); // obtaining the Bitmap
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush();
fOut.close(); // do not forget to close the stream

MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
Suhail Mehta
  • 5,514
  • 2
  • 23
  • 37
1

Best practice is to use Activity 1:

try {
    //Write file
    String filename = "bitmap.png";
    FileOutputStream stream = this.openFileOutput(filename, Context.MODE_PRIVATE);
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);

    //Cleanup
    stream.close();
    bmp.recycle();

    //Pop intent
    Intent in1 = new Intent(this, Activity2.class);
    in1.putExtra("image", filename);
    startActivity(in1);
} catch (Exception e) {
    e.printStackTrace();
}

In Activity 2, load up the bitmap:

Bitmap bmp = null;
String filename = getIntent().getStringExtra("image");
try {
    FileInputStream is = this.openFileInput(filename);
    bmp = BitmapFactory.decodeStream(is);
    is.close();
} catch (Exception e) {
    e.printStackTrace();
}

Solution to your Problem

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {

      Intent intent = new Intent(Restaurants.this, RestaurantInformation.class);

      intent.putExtra("id", restaurantAdaptor.getItemId(position));
      intent.putExtra("text", stocks[position].text); 
      intent.putExtra("name", stocks[position].name);

        final ImageView imageView = (ImageView) view.findViewById(R.id.image);
        final BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
        final Bitmap yourBitmap = bitmapDrawable.getBitmap();



      ByteArrayOutputStream bStream = new ByteArrayOutputStream();
      yourBitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);
      byte[] byteArray = bStream.toByteArray();

      intent.putExtra("image", byteArray);
      startActivity(intent);
      finish();
  }

 });
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
  • Thank you for your answer. I've tried your method and the app doesn't crash but again there is no image. And in logcat is same line error + on this line in second activity `FileInputStream is = this.openFileInput(filename);` and when I check with `Log.e` what is returned for image I see this `android.widget.ImageView{b2ebe100 V.ED.... ......I. 0,0-0,0 #7f080023 app:id/image1}` – Goro Nov 18 '14 at 09:40
  • You do not pass an imageView, but the image – Murtaza Khursheed Hussain Nov 18 '14 at 09:41
  • Well.. is what I'm trying. I have no idea why is trying to pass the ImageView. – Goro Nov 18 '14 at 09:44
  • get image bitmap from imageView, like this Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap(); – Murtaza Khursheed Hussain Nov 18 '14 at 09:46
  • Where should I put this? In `try` block or outside? In `setOnItemClickListener`? – Goro Nov 18 '14 at 09:50
  • where is your ImageView ? – Murtaza Khursheed Hussain Nov 18 '14 at 09:51
  • It is in `onCreate` where are all other TextView's and ListView `image = (ImageView) findViewById(R.id.image);` – Goro Nov 18 '14 at 09:53
  • if you paste your complete code, i will able to help you – Murtaza Khursheed Hussain Nov 18 '14 at 09:54
  • I've posted full source for activity1 in pastebin. Please check it here - http://pastebin.com/NGAX9AbS – Goro Nov 18 '14 at 10:00
  • so you want restaurant image from the listview row which is clicked to be send to another activity ? – Murtaza Khursheed Hussain Nov 18 '14 at 10:03
  • Yes. This listview currently contain text, name and image. They are passed successfully to second activity but image doesn't. – Goro Nov 18 '14 at 10:05
  • Now I get this in `Logcat` `11-18 05:17:11.606: W/Bundle(2051): Key image expected String but value was a [B. The default value was returned. 11-18 05:17:11.616: W/Bundle(2051): Attempt to cast generated internal exception: 11-18 05:17:11.616: W/Bundle(2051): java.lang.ClassCastException: byte[] cannot be cast to java.lang.String ` – Goro Nov 18 '14 at 10:19
  • and this is still there also `android.widget.ImageView{b2ebe100 V.ED.... ......I. 0,0-0,0 #7f080023 app:id/image1}` – Goro Nov 18 '14 at 10:23
  • I've put my old source in activity2 - `byte[] byteArray = getIntent().getByteArrayExtra("image"); bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);` and the image is there. But whit your is not. – Goro Nov 18 '14 at 10:26
  • thats good. it means your problem is solved, probably I was doing something wrong. – Murtaza Khursheed Hussain Nov 18 '14 at 10:27
  • I still see this in log `android.widget.ImageView{b2ebe100 V.ED.... ......I. 0,0-0,0 #7f080023 app:id/image1}` but at least the image is there. Thank's for your help. – Goro Nov 18 '14 at 10:28