android - 将 ImageView 背景设置为位图

标签 android canvas android-activity bitmap imageview

我一直在四处寻找,但找不到解决我的问题的方法。我正在尝试将 bmp1 传递给第二个 Activity 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");

最佳答案

使用

intent.putExtra("BitmapImage", bmp1);

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

关于android - 将 ImageView 背景设置为位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19463571/

相关文章:

android - 将图像从相机加载到 Fragment 中的 ImageView

android - Android 应用程序如何检测安装了哪个商店?

javascript - Ionic3 和 FabricJS

javascript - 在跟随鼠标指针的图像上制作轴引导线

android - 我怎样才能返回基本 Activity

java - 在没有值(value)的情况下调用 Retrofit

java - Java 中的扩展接口(interface)和转换错误

javascript - Greasemonkey 脚本,可在每个网站上创建 Kineticjs 拖放 Canvas

android - 如何将 Activity 实例传递给另一个 Activity

java - 无法按预期将我的 arrayList 写入文件