Hyun Suk Lee
Hyun Suk Lee

Reputation: 3

Setting ImageView to Bitmap and sending it to next Activity

Hi fairly new to Android development. I'm just making a simple app that can retrieve a captured image and set that image as background. I was able to do so by using setBackground(new BitmapDrawble ...). My next activity was to editText View along with that background and whenever i onclick the save button it would proceed to the next activity that can view the both edited textview and background.

My Question is that I was able to view my edited text but not able to view the background. Here is my following code.

RetrieveImage.Activity

public static final String EXTRA_MESSAGE = "com.example.hyunsukcirllee.footstep.MESSAGE";
private ImageView mdisplayBackground;
public static final int SEND_MESSAGE = 1;


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

    //Bundle to get intent
    Bundle extras = getIntent().getExtras();

    //If Bundle is not empty proceed
    if(extras != null){
        //get the imagepass key from the previous activity and set it to bitmap
        Bitmap retrieveImage = (Bitmap) extras.get("imagepass");

        //If the bitmap is not empty
        if(retrieveImage != null){
            //Set it to following imageView
            mdisplayBackground = (ImageView) findViewById(R.id.background_captured);
            mdisplayBackground.setBackground(new BitmapDrawable(getResources(), retrieveImage));
            mdisplayBackground.getBackground().setAlpha(80);

        }
    }
}

//onClick Send button
public void sendMessage(View view){
    //Creating an explicit intent to display the sent message
    Intent footstep_intent = new Intent(this, DisplayMessageActivity.class);

    //EditText Id
    EditText editText = (EditText) findViewById(R.id.editText);

    //User editText
    String message = editText.getText().toString();

    //This part is what I want to implement!!!
    mdisplayBackground = (ImageView) findViewById(R.id.background_captured);
    mdisplayBackground.buildDrawingCache();
    Bitmap image_pass_again = mdisplayBackground.getDrawingCache();

    //Put both Bitmap and String Message to the intent
    footstep_intent.putExtra("Image", image_pass_again);
    footstep_intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(footstep_intent);
}

}

Next Activity to show both image and text

private ImageView mdisplayBbackground;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);

    //if the intent has the name image do setbackground
    if(getIntent().hasExtra("Image")) {
        Bundle iimage = getIntent().getExtras();
        Bitmap b = (Bitmap) iimage.get("image");
        mdisplayBbackground = (ImageView) findViewById(R.id.background_capturedd);
        mdisplayBbackground.setBackground(new BitmapDrawable(getResources(), b));

    }

    //if the intent contains the message-> view
    if (getIntent().hasExtra(RetrievePhotoBackgroundActivity.EXTRA_MESSAGE)){
        Intent footstep_intent = getIntent();
        String message = footstep_intent.getStringExtra(RetrievePhotoBackgroundActivity.EXTRA_MESSAGE);
        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(message);
    }
}

}

Following Code Currently does not work after clicking the Send Button. I've searched around to pass the bitmap using above code and Setting it to Content URI but did not fairly understand the concept well I guess. So the question is "is there way of getting around of using external storage to call the direct path of image file to set an imageView for the next Activity?" Another question is "Im still practicing on this area so if there are better formatting in terms of coding I would like to get some feedback" Thanks!

Upvotes: 0

Views: 75

Answers (2)

Munir
Munir

Reputation: 2558

Highly recommend a different approach.

It might not work if you have an older phone and a big bitmap.It's possible if you REALLY want to do it, but it costs a lot of memory and is also slow. Rather if you really want to do then pass bitmap like below

Send Bitmap

try {
    //Write file
    String filename = "abc.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();
}

Receiving side

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

Upvotes: 0

JaLoveAst1k
JaLoveAst1k

Reputation: 511

You should not send bitmaps between different screens, it will at one point lead you to memory leaks since every bitmap should be recycled after work is done.
Also, you should not handle image loading/disposing/caching/etc yourself - consider using some library like Glide or Picasso, the first one is recommended by Google.

Here is an example of loading image from file system, profileAvatar is an ImageView:

Glide.with(mContext)
    .load(new File(filePath))
    .into(profileAvatar);

Really easy to use ;) By default Glide will cache images in memory.

Upvotes: 1

Related Questions