gpt4 book ai didi

android - 无法在android中的 Activity 之间传递位图

转载 作者:太空宇宙 更新时间:2023-11-03 12:00:01 26 4
gpt4 key购买 nike

我需要通过从其他 Activity 中选择图像来动态更改 Activity 的背景。并将所选图像传回调用 Activity ,但当我尝试获取图像时,它为空。这是我的代码。

  public class SelectGoalBackground extends OrmLiteBaseActivity<DatabaseHelper> {// implements{



GridView gridview ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_goal_background);
final String from = getIntent().getExtras().getString("from");
goalsList = new ArrayList<Goal>();

try {
goalsList = getTop3Goals();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (java.sql.SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}


gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

ImageView l = (ImageView)gridview.getChildAt(position);
Drawable path= l.getDrawable();


Bitmap bitmap = ((BitmapDrawable)path).getBitmap();

Intent intent = new Intent(SelectGoalBackground.this, AlarmAppActivity.class);

intent.putExtra("Bitmap", bitmap);
startActivity(intent);
SelectGoalBackground.this.finish();
return;

});

}


public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
mContext = c;
}

public int getCount() {
return mThumbIds.length;
}

public Object getItem(int position) {
return null;
}

public long getItemId(int position) {
return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
Bitmap bm = BitmapFactory.decodeResource(getResources(), mThumbIds[position]);
imageView.setImageBitmap(bm);
return imageView;
}

// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_0, R.drawable.sample_1

};
}

在调用 Activity 中我得到了这样的位图

    RelativeLayout rel_lay = (RelativeLayout) findViewById(R.id.main_lay);
Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");


if (bitmap != null) {

Log.v("bitmap", "not null");
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
rel_lay.setBackgroundDrawable(drawable);
}

但是位图在这里是空的,它没有设置这个 Activity 的背景。感谢任何形式的帮助,请帮助

最佳答案

有 3 种解决方案可以解决此问题。

1) 首先将图像转换为字节数组,然后传递给 Intent,在下一个 Activity 中,从 Bundle 中获取字节数组并转换为图像(位图)并设置到 ImageView 中。

将位图转换为字节数组:-

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

将字节数组传递给 Intent :-

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);

从 Bundle 中获取字节数组并转换为位图图像:-

Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

2) 首先将图像保存到 SDCard 中,然后在下一个 Activity 中将此图像设置到 ImageView 中。

3) 将 Bitmap 传递到 Intent 并在下一个 Activity 中从 bundle 中获取位图,但问题是如果您的 Bitmap/Image 尺寸当时很大,则图像不会在下一个 Activity 中加载。

关于android - 无法在android中的 Activity 之间传递位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11720589/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com