mho*_*gan 2 android android-arrayadapter android-intent android-gallery
我创建了一个自定义列表界面.我正在使用数组适配器来管理列表视图单元格的按钮点击.我的一个按钮会打开一个对话框,显示设备的图库并允许他们选择图像.
在我的Adapters getView方法中,我有按钮的单击侦听器.当我选择图像时.onActivityResult我的Activity中的方法被调用.但我无法阅读intent.putExtras("id", id),因为它是作为阅读null的活动.这是我在我的方法中startActivityForResult调用的方法MainActivityListAdapter:
public void showExtraPicture(String imgStr, final String qid, String qname) {
ImageButton gallery = (ImageButton) dialog.findViewById(R.id.gallerybtn);
gallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("BIT: Qid: " + qid);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
Bundle mBundle = new Bundle();
mBundle.putSerializable("qid", qid);
intent.putExtras(mBundle);
((Activity) QuestionListAdapter.this.context).startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
dialog.dismiss();
} // end method onClick
}); // end setOnClickListener
Run Code Online (Sandbox Code Playgroud)
}
我尝试过使用Bundle,putSerializable但这对我不起作用.我猜我的问题与我调用ArrayAdapter中的数据有关.
这是我onActivityResult在我的MainActivity:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bundle mBun = data.getExtras();
String qid = "";
if (mBun != null) {
qid = (String) mBun.getSerializable("qid");
}
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
try {
Bitmap image = getBitmapFromUri(selectedImage);
String stringRepresention = BitMapToString(image);
//Save string to DB
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
提供的意图onActivityResult()与您使用的意图不同startActivityForResult(); 为结果启动的活动将用于setResult(int resultCode, Intent data)设置提供给您的onActivityResult()函数的Intent .
Therefore, any extras you supply on your original intent are only useful when the target activity (an image chooser, in your case) is doing its job -- you will not see those extras in your result (unless the target activity goes out of its way to copy them into the result intent, of course).
What do you need to propagate qid for? You didn't include the usage on the receiving side... so I will assume that you need it to know where to put the chosen image in some way.
在这种情况下,您应该使用requestCode功能.让您的应用使用requestCode-> qid映射跟踪待处理的请求.确保为每个startActivityForResult()呼叫使用唯一的requestCode .您将把requestCode作为参数返回onActivityResult()- 在地图中查找,然后您将获得相关的qid.当请求返回时,不要忘记清理地图.
可能需要注意的是:您可能需要将映射持久存储在磁盘上,以防您的进程在映像选择器位于顶部时被终止.这种情况可能很少见,但你应该确保测试它并在需要时处理它.
| 归档时间: |
|
| 查看次数: |
2484 次 |
| 最近记录: |