0
votes

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);
2
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
I don't know why it gets null since I get the image on first activity and everything is shown correctly there. - Goro
can you tell me in which line you are getting null? - Sayem
It is in the question under the LogCat -> bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream); - Goro
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

2 Answers

2
votes

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());
1
votes

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();
  }

 });