除了我的应用程序中的照片外,我还添加了上传视频的功能。为了实现这一点,现在当您单击附加按钮b2
而不是加载图像选择器时,它会加载一个警告对话框showDialog()
,询问您在选择时要上传哪个(视频或照片),然后它会加载图像或视频选择器。现在的问题是当您从内部类调用方法时doPositiveClick(Activity activity, int requestCode)
onActivityResult 没有被触发并且没有返回数据。我觉得这与从内部类调用它有关,MyAlertDialogFragment
但我不确定如何处理它。谢谢你。
public static final int REQUEST_CODE = 0, RESULT_PHOTO = 1, RESULT_VID = 2;
void showDialog() {
DialogFragment newFragment = MyAlertDialogFragment.newInstance(
R.string.alert_dialog_two_buttons_title);
newFragment.setTargetFragment(ChatRoomFragment.this, REQUEST_CODE);
newFragment.show(getFragmentManager(), "dialog");
}
@Override
if(requestCode == REQUEST_CODE && data.getData() != null) {
Log.v("response", "Photo Selected");
Uri _uri = data.getData();
Log.v("response", "cp1/4");
代码做到了这一点^^^^ b2.setImageResource(R.drawable.picattached);
if (_uri != null) {
//User has pick an image.
Cursor cursor = getActivity().getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
//cursor.moveToFirst();
if (cursor == null){
uploadMsgPic = _uri.getPath();
Log.i("response", "cursor == null");
Log.i("response", "uploadMsgPic now = " + uploadMsgPic);
}else{
Log.i("response", "cursor = "+ cursor);
cursor.moveToFirst();
Log.v("response", "cp2/4");
//Link to the image
//cursor.moveToFirst();
final String imageFilePath = cursor.getString(0);
Log.i("response", "imageFilePath == " + imageFilePath);
Log.v("response", "cp3/4");
uploadMsgPic = imageFilePath;
Log.v("response", "4/4");
cursor.close();
if (uploadMsgPic == null)
uploadMsgPic = _uri.getPath();
}
Log.i("response", "uploadMsgPic == " + uploadMsgPic);
media_attached=true;
}
}
if(requestCode == 6 && data != null && data.getData() != null){
Uri _uri = data.getData();
Log.v("response", "cp1/4");
b2.setImageResource(R.drawable.picattached);
if (_uri != null) {
//User has pick an image.
Cursor cursor = getActivity().getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
//cursor.moveToFirst();
if (cursor == null){
uploadMsgPic = _uri.getPath();
Log.i("response", "cursor == null");
Log.i("response", "uploadMsgPic now = " + uploadMsgPic);
}else{
Log.i("response", "cursor = "+ cursor);
cursor.moveToFirst();
Log.v("response", "cp2/4");
//Link to the image
//cursor.moveToFirst();
final String imageFilePath = cursor.getString(0);
Log.i("response", "imageFilePath == " + imageFilePath);
Log.v("response", "cp3/4");
uploadMsgPic = imageFilePath;
Log.v("response", "4/4");
cursor.close();
if (uploadMsgPic == null)
uploadMsgPic = _uri.getPath();
}
Log.i("response", "uploadMsgPic == " + uploadMsgPic);
media_attached=true;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
//Generics:
//1. Long: Type of reference(s) passed to doInBackground()
//2. String: Type of reference passed to onProgressUpdate()
//3. STRING: Type of reference returned by doInBackground()
//Value passed to onPostExecute()
public static void doPositiveClick(Activity activity, int requestCode) {
Log.i("ChatRoomFragMent", "doPositive Clicked");
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
activity.startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_CODE);
// Do stuff here.
Log.i("ChatRoomFragMent", "picture selector loaded");
}
public void doNegativeClick() {
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Video"), RESULT_VID);
// Do stuff here.
Log.i("FragmentAlertDialog", "Negative click!");
}
public static class MyAlertDialogFragment extends SherlockDialogFragment {
public static MyAlertDialogFragment newInstance(int title) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");
return new AlertDialog.Builder(getActivity())
//.setIcon(R.drawable.alert_dialog_icon)
.setTitle(title)
.setPositiveButton("Photo",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { Intent data = new Intent();
//((FragmentAlertDialogSupport)getActivity()).doPositiveClick();
ChatRoomFragment.doPositiveClick(getActivity(), 5);
getTargetFragment().onActivityResult(getTargetRequestCode(), RESULT_PHOTO, data);
}
}
)
.setNegativeButton("Video",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//((FragmentAlertDialogSupport)getActivity()).doNegativeClick();
//doNegativeClick();
}
}
)
.create();
}
}
}