我通过Intent从另一个活动获取byteArray,如下所示:
if (view == null) {
view = new ImageView(mContext);
}
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap default_b = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
view.setImageBitmap(default_b);
view.setImageResource(drawables.get(position));
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
view.setTag(String.valueOf(position));
return view;
但是我得到的错误是getIntent()未定义GridViewAdapter类型(这是在我的gridView基础适配器类中)
我在这里创建了意图:
Intent intent = new Intent(v.getContext(), GridViewAdapter.class);
intent.putExtra("picture", byteArray);
v.getContext().startActivity(intent);
如何解决此错误?
增加:
以下是我创建意图的全部内容:
Log.d("AppInfoAdapter", "Data Set To Display");
addCheckbox
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (addCheckbox.isChecked()) {
System.out.println("Checked");
PackageManager pm = mContext.getPackageManager();
Drawable icon = null;
try {
icon = pm
.getApplicationIcon(entry.activityInfo.packageName);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Drawable default_icon = pm.getDefaultActivityIcon();
if (icon instanceof BitmapDrawable
&& default_icon instanceof BitmapDrawable) {
BitmapDrawable icon_bd = (BitmapDrawable) icon;
Bitmap icon_b = icon_bd.getBitmap();
BitmapDrawable default_bd = (BitmapDrawable) pm
.getDefaultActivityIcon();
Bitmap default_b = default_bd.getBitmap();
if (icon_b == default_b) {
// It's the default icon
scaleDownBitmap(default_b, 100, v.getContext());
Log.d("AppInfoAdapter", "Scale Bitmap Chosen");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
default_b.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Log.d("AppInfoAdapter", "Scale Bitmap to Array");
Intent intent = new Intent(v.getContext(), GridViewAdapter.class);
intent.putExtra("picture", byteArray);
v.getContext().startActivity(intent);
Log.d("AppInfoAdapter", "Intent started to send Bitmap");
}
}
} else {
System.out.println("Un-Checked");
}
}
});
答案 0 :(得分:2)
将您的Activity上下文传递给Adapter构造函数
你可以像这样访问你的意图
((Activity)mContext).getIntent()
答案 1 :(得分:0)
getIntent()
用于获取用于启动Intent
的{{1}}。由于您不在Activity
,因此没有Activity
到“获取”,而Intent
正如它所说的那样,不是getIntent()
类的函数。 / p>
在调用Adapter
类的Activity
中使用该代码,并传递该类所需的数据
答案 2 :(得分:0)
我们走了:
Intent intent = ((Activity) context).getIntent();
int value = intent.getIntExtra("myvalue", 0);