zsh5032 zsh5032 - 15 days ago 7
Android Question

Error passing image from imageview to second activity

So I have an imageview1 that when it is clicked on a new activity is started where the same image is displayed in a new imageview2 on the next activity. The imageview1 that gets clicked on is accessed by a listview on my mainactivity. The listview is populated by an array and each listview item has a coresponding image.

So when a listview item is clicked it starts a new activity where imageview1 resides. This imageview is populated using an xml typedarray which references the drawable image.

Im having trouble passing the image from my first imageview to the next one on the new activity. I'm getting a null pointer exception that I've included below. Im very new to programming and am not really sure how to fix the issue. Any help would be really appreciated.

heres my routeDetails class which contains the first imageview

public class RouteDetails extends AppCompatActivity {

ImageView routeImage;

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

//TextView for route details
final TextView routeDetailsView = (TextView) findViewById(R.id.routeDetailsView);
routeDetailsView.setText(getIntent().getExtras().getString("route"));

//ImageView for route details
routeImage = (ImageView) findViewById(R.id.routeImage);
routeImage.setImageResource(getIntent().getIntExtra("imageResourceId", 0));


//passing image to fullscreenimage
routeImage.setDrawingCacheEnabled(true);
routeImage.buildDrawingCache();
final Bitmap bitmap = routeImage.getDrawingCache();

////////// Trying to pass image to new activity

routeImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
Intent i = new Intent(RouteDetails.this, FullScreenImage.class);
//Bitmap _bitmap; // your bitmap
ByteArrayOutputStream _bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);
i.putExtra("byteArray", _bs.toByteArray());
startActivity(i);
}

});


////////////
}


and heres my fullscreenimage class im trying to pass the image to

public class FullScreenImage extends AppCompatActivity {

TouchImageView routeImageFull;

@Override
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);


setContentView(R.layout.full_screen_image);
routeImageFull = (TouchImageView) findViewById(R.id.fullScreenImageView);


if(getIntent().hasExtra("byteArray")) {
TouchImageView routeImageFull= new TouchImageView(this);
Bitmap bitmap = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
routeImageFull.setImageBitmap(bitmap);
}
}
}


and heres the error im getting

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zach.listview, PID: 3429
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
at com.example.zach.listview.RouteDetails$1.onClick(RouteDetails.java:47)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Answer

Rather than passing the whole bitmap. You can simply send the resource id you are getting here to another activity and can show image by same way, you are doing here. Below is the changed code.

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

    //TextView for route details
    final TextView routeDetailsView = (TextView)    
    findViewById(R.id.routeDetailsView);
    routeDetailsView.setText(getIntent().getExtras().getString("route"));

    //ImageView for route details
    routeImage = (ImageView) findViewById(R.id.routeImage);

    int mImageResource = getIntent().getIntExtra("imageResourceId", 0);
    routeImage.setImageResource(mImageResource);


   //passing image to fullscreenimage
   routeImage.setDrawingCacheEnabled(true);
   routeImage.buildDrawingCache();
   final Bitmap bitmap = routeImage.getDrawingCache();

   // Trying to pass image to new activity

   routeImage.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view)
        {
            Intent i = new Intent(RouteDetails.this,FullScreenImage.class);
            i.putExtra("mImageResource", mImageResource);
            startActivity(i);
        }

   });
}
Comments