[英]Changing an ImageView in fragment on clicking the ImageView
I am using Fragments
in my app. 我在我的应用程序中使用
Fragments
。 I have an ImageView
in a Fragment
. 我在
Fragment
有一个ImageView
。 on clicking the ImageView
user should be able to select the image from the gallery and the image should be displayed in the same ImageView
. 单击
ImageView
用户应该能够从图库中选择图像,并且该图像应显示在同一ImageView
。 I tried this separately in an Activity
Class and it is working fine but how can I do this in a Fragment
Class? 我在
Activity
类中单独尝试了此方法,但效果很好,但是如何在Fragment
类中进行此操作呢? The code I tried is- 我尝试的代码是-
public class FragmentCreateGroup extends Fragment {
ImageView group;
EditText et;
LayoutInflater glob_inflater;
View glob_view;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_creategroup, container, false);
glob_view=v;
glob_inflater=inflater;
et = (EditText) v.findViewById(R.id.cr_group_grpname_input);
group = (ImageView) v.findViewById(R.id.group_image);
group.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), 1);
}
});
return v;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == -1) {
if (requestCode == 1) {
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath, options);
group.setImageBitmap(BitmapFactory.decodeFile(selectedImagePath));
}
}
}
public String getPath(Uri uri) {
// just some safety built in
if( uri == null ) {
// TODO perform some logging or show user feedback
return null;
}
Activity act=new Activity();
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor =act.managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// this is our fallback here
return uri.getPath();
}
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
}
you shall not new the Activity
but use startActivityForResult
in your Fragment
, and then in your Fragment
, override onActivityResult
to do your logic. 您不应该新建
Activity
而是在Fragment
使用startActivityForResult
,然后在Fragment
,覆盖onActivityResult
以执行您的逻辑。
it' s pretty same, please has a try! 差不多,请尝试一下!
For StartActivityForResult you have to follow these steps to get better solution. 对于StartActivityForResult,您必须遵循以下步骤以获得更好的解决方案。
1.While Starting use getActivity.StartActivityForResult() 1.在开始使用getActivity.StartActivityForResult()时
2.User Interfaces to communicate with the fragment and the activity. 2.用于与片段和活动进行通信的用户界面。
private void chooseOption() {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
pickImage();
break;
case DialogInterface.BUTTON_NEGATIVE:
takePic();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(
this.getActivity());
builder.setMessage("Choose your option")
.setPositiveButton("From Gallery", dialogClickListener)
.setNegativeButton("Camera", dialogClickListener).show();
}
public void pickImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
getActivity().startActivityForResult(intent, REQUEST_CODE);
}
public void takePic() {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
getActivity().startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
The code to show a dialog to get the image from gallery or from camera then 显示对话框以从图库或相机中获取图像的代码,然后
In your activity define this interface 在您的活动中定义此界面
public interface onImageGot {
public void onImageGotSelected(Uri position);
}
And create an instance for this interface in your activity Itself before oncreate like 并在您自己的活动中为此接口创建实例,然后再进行oncreate之类的操作
onImageGot mCallback;
And initiate this instance onActivityResult and use.. 并启动此实例onActivityResult并使用。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri selectedImage = null;
EditProfile titleFragmentByTag = (EditProfile) getSupportFragmentManager()
.findFragmentByTag("edit");
if (titleFragmentByTag != null) {
mCallback = (onImageGot) titleFragmentByTag;
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
try {
selectedImage = data.getData();
} catch (Exception e) {
e.printStackTrace();
}
else if (requestCode == CAMERA_PIC_REQUEST
&& resultCode == Activity.RESULT_OK) {
selectedImage = getImageUri(getApplicationContext(),
(Bitmap) data.getExtras().get("data"));
}
if (selectedImage != null) {
mCallback.onImageGotSelected(selectedImage);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(),
inImage, "Title", null);
return Uri.parse(path);
}
By Implementing HomeScreenActivity.onImageGot in your fragment class Your fragment will override this 通过在片段类中实现HomeScreenActivity.onImageGot,您的片段将覆盖此片段
@Override
public void onImageGotSelected(Uri position) {
UpdateImage(position);
}
Here ur getting the uri so from here u can use as like u want.. 在这里您获得了uri,因此您可以在这里随意使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.