将图像视图背景设置为位图

时间:2013-10-19 07:26:27

标签: android canvas android-activity bitmap imageview

我一直在四处寻找,但无法找到问题的解决方案。我正在尝试将bmp1传递给第二个活动,Profile。粘贴的代码不起作用,任何有可能建议的人都会很棒。 这是第一部分的代码

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");

3 个答案:

答案 0 :(得分:1)

使用

intent.putExtra("BitmapImage", bmp1);

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

答案 1 :(得分:0)

您的代码中存在错误。您使用String“byteArray”作为键放置字节数组但在Profile Activity中使用不同的字符串键“Image”检索

答案 2 :(得分:0)

不需要这么想.. 选择活动中的一个静态位图,并在配置文件活动中使用它。 希望这有助于您理解:

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