我试图从我的相册中获取图片...下面这段代码完美地适用于一个活动,但是当我从一个片段中使用它时......它不会崩溃,它只是没有达到“ onActivityResult“方法......任何人都可以提出什么建议
@Override
public void onClick(View v) {
if (v.getId() == mCIB.getId()) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(i, ""), RESULT_LOAD_IMAGE);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
//BitmapFactory.decodeFile(picturePath)
mCIB.setBitmapDrawable((BitmapDrawable) BitmapDrawable.createFromPath(picturePath), picturePath);
try {
mCIB.createImagePath();
mCIB.addImageToDB();
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
请尝试此代码
父活动:SampleActivity.java
public class SampleActivity extends FragmentActivity {
FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parent_layout);
fragmentManager = getSupportFragmentManager();
if (savedInstanceState == null) {
fragmentManager.beginTransaction().add(R.id.container,
new SampleFragment(),"sample_fragment").commit();
}
}
父布局:parent.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame" />
Child Fragment:SampleFragment.java
public class SampleFragment extends Fragment {
int reqCode = 111;
ImageView image;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_sample, null);
image = (ImageView) rootView.findViewById(R.id.image);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == image.getId()) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//photoPickerIntent.setType("image/*");
startActivityForResult(Intent.createChooser(photoPickerIntent, ""), reqCode);
}
}
});
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (null != imageReturnedIntent && requestCode == 111) {
//String strUriPatPic = imageReturnedIntent.getData().toString();
Uri selectedImage = imageReturnedIntent.getData();
onPictureSelected(selectedImage);
}
}
private void onPictureSelected(Uri uri) {
Bitmap userPictureBitmap = decodeSampledBitmapFromUri(getActivity(),
uri, 100, 100);
image.setImageBitmap(userPictureBitmap);
}
private Bitmap decodeSampledBitmapFromUri(Activity callingActivity,
Uri uri, int reqWidth, int reqHeight){
// TODO decode as you want
}
}