Android 将图像视图背景设置为位图
我一直在四处寻找,但找不到解决我问题的办法。我正在尝试将bmp1传递给第二个活动,即配置文件。粘贴的代码不起作用,任何人都可以提出建议。 这是我第一部分的代码Android 将图像视图背景设置为位图,android,canvas,android-activity,bitmap,imageview,Android,Canvas,Android Activity,Bitmap,Imageview,我一直在四处寻找,但找不到解决我问题的办法。我正在尝试将bmp1传递给第二个活动,即配置文件。粘贴的代码不起作用,任何人都可以提出建议。 这是我第一部分的代码 Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactoryOptions); int heightRatio = (int)Math.ceil(bmpFactoryOptions.
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");
您的代码中有一个错误。您使用字符串“byteArray”作为放置字节数组的键,但在配置文件活动中使用不同的字符串键“Image”进行检索
intent.putExtra("BitmapImage", bmp1);
及
没必要想这么多。。 在“选择活动”中获取一个静态位图,并在“配置文件活动”中使用它。 希望这能帮助您理解:
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);
}
见JRowan的答案。在profile类中它看起来不错,我在“意图”上出错了,你知道为什么会这样吗?试试这个答案
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);
}