我正试图在我的Fragment
中将图像从图库设置为图像视图。使用下面给出的GallerUtil
类选择图片。
profile_home.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent gallery_Intent = new Intent(getActivity(), GalleryUtil.class);
startActivityForResult(gallery_Intent, GALLERY_ACTIVITY_CODE);
}
});
GalleryUtil.class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
//Pick Image From Gallery
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_SELECT_IMAGE);
}catch(Exception e){
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case RESULT_SELECT_IMAGE:
if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
try{
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 picturePath = cursor.getString(columnIndex);
cursor.close();
//return Image Path to the Main Activity
Intent returnFromGalleryIntent = new Intent();
Log.d("pathhh",picturePath);
returnFromGalleryIntent.putExtra("picturePath",picturePath);
setResult(RESULT_OK,returnFromGalleryIntent);
finish();
此图片的路径未进入我的onActivityResult
片段中。所以请帮我找一个解决方案
片段中的onActivityResult -
@Override
@SuppressLint("NewApi")
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("pathhh1","11");
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_ACTIVITY_CODE) {
Log.d("pathhh","12");
if(resultCode == Activity.RESULT_OK){
picturePath = data.getStringExtra("picturePath");
Log.d("pathhh",picturePath);
//perform Crop on the Image Selected from Gallery
performCrop(picturePath);
}
}
if (requestCode == RESULT_CROP ) {
if(resultCode == Activity.RESULT_OK){
Bundle extras = data.getExtras();
selectedBitmap = extras.getParcelable("data");
//selectedBitmap1=getRoundedCroppedBitmap(selectedBitmap, 50);
CircleDrawable circle = new CircleDrawable(selectedBitmap,true);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
profile_home.setBackground(circle);
else
profile_home.setBackgroundDrawable(circle);
// Set The Bitmap Data To ImageView
// profile_img.setImageBitmap(selectedBitmap1);
profile_home.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
答案 0 :(得分:1)
将此代码写入您放置片段的MainActivity中。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
//Write your fragment name instead of YourFragmentName
if (fragment instanceof YourFragmentName) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
}