Akhil Yeleswar
Akhil Yeleswar

Reputation: 85

Setting image view background to bitmap

Ive been looking around but couldn't find the solution to my problem. Im am trying to pass the bmp1 to the second activity, Profile. The code pasted does not work, anyone with possible suggestions would be great. Here is my code for the first part

Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions); 
int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)dh); 
int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)dw); 
            if (heightRatio > 1 && widthRatio > 1) 
            { 
            if (heightRatio > widthRatio) { 
            bmpFactoryOptions.inSampleSize = heightRatio;
            } else {
bmpFactoryOptions.inSampleSize = widthRatio;
                }
            }
bmpFactoryOptions.inJustDecodeBounds = false; 
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions);
Bitmap bmp1 = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions); 
Bitmap alteredBitmap = Bitmap.createBitmap(bmp1.getWidth(),bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(alteredBitmap); 
Paint paint = new Paint();

Matrix matrix = new Matrix(); 
matrix.setValues(new float[] {
.5f, 0, 0, 

0, .5f, 0, 

0, 0, 1
 });

canvas.drawBitmap(bmp, matrix, paint);
ImageView alteredImageView = (ImageView) this.findViewById(R.id.AlteredImageView); 
alteredImageView.setImageBitmap(alteredBitmap);

    chosenImageView.setImageBitmap(bmp1); 
 } catch (FileNotFoundException e) {  Log.v("ERROR",e.toString());

 }
        }
        Nex.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Uri imageFileUri = intent.getData();

                Intent intent = new Intent(Choose.this, Profile.class);
                 // your bitmap
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                bmp1.compress(Bitmap.CompressFormat.PNG, 50, bs);
                intent.putExtra("byteArray", bs.toByteArray());
                intent.putExtra("location", textView1.getText().toString());
                startActivity(intent);
            }
        }
        );

    }
}


public class Profile extends Activity {
    ImageView picture;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.profile);

        picture = (ImageView) findViewById(R.id.Picture);


        Bitmap bitmap = (Bitmap) intent.getParcelableExtra("bytearray");

Upvotes: 2

Views: 1031

Answers (3)

Kalpesh Lakhani
Kalpesh Lakhani

Reputation: 993

No need to think this much.. take one static Bitmap in Choose Activity and use it in Profile Activity. Hope this will help you to understand:

Choose.java
    static Bitmap bit=null;
  //then assign bitmap when it available
   bit=bmp1 //this is your bitmap

 //now at Profile.java use Choose.bit
   if(Choose.bit!=null)
 {
     profimageview.setBitmap(Choose.bit); 
 }

Upvotes: 0

JRowan
JRowan

Reputation: 7114

use

intent.putExtra("BitmapImage", bmp1);

and

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");  

Upvotes: 1

Gopal Gopi
Gopal Gopi

Reputation: 11131

There is a mistake in your code. you are using String "byteArray" as Key for putting byte array but retrieving in Profile Activity with different String Key "Image"

Upvotes: 0

Related Questions