简体   繁体   English

如何从最低版本获取onActivityResult

[英]How to get onActivityResult from lowest version

In my app, i have the option to choose picture from gallery. 在我的应用程序中,我可以选择从图库中选择图片。 I want to place image on Image view...my code perfectly work(place image on image view) for highest version(API 21) but not working(place image on image view) for lowest version (Api 15). 我想将图像放置在图像视图上...我的代码对于最高版本(API 21)可以完美地工作(将图像放置在图像视图上),但对于最低版本(Api 15)则无法工作(将图像放置在图像视图上)。 Please any one help me!! 请任何人帮助我!

My app config: 我的应用程序配置:

    minSdkVersion 14
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"

My code here: 我的代码在这里:

 public class ProfileFragment extends Fragment { //private static Bitmap Image = null; private ImageView imageView; private static final int SELECT_PHOTO = 1; private String selectedImagePath = null; private Uri mSelectedImageUri; Button browseProfilePic; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { setHasOptionsMenu(true);//For option menu View view = inflater.inflate(R.layout.fragment_layout_profilepic, container, false); imageView = (ImageView)view.findViewById(R.id.profile_image); browseProfilePic = (Button) view.findViewById(R.id.btn_pick); browseProfilePic.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); // Show only images, no videos or anything else intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); // Always show the chooser (if there are multiple options available) startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PHOTO); } }); return view; } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == MenuActivity.RESULT_OK) { if (requestCode == SELECT_PHOTO) { mSelectedImageUri = data.getData(); selectedImagePath = getPath(mSelectedImageUri); System.out.println("Image Path : " + selectedImagePath); imageView.setImageURI(mSelectedImageUri); } } } public String getPath(Uri uri) { String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, null); if (cursor == null) return null; int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String s=cursor.getString(column_index); cursor.close(); return s; } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // Save the image bitmap into outState Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap(); outState.putParcelable("bitmap", bitmap); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); // Read the bitmap from the savedInstanceState and set it to the ImageView if (savedInstanceState != null){ Bitmap bitmap = (Bitmap) savedInstanceState.getParcelable("bitmap"); imageView.setImageBitmap(bitmap); } } @Override public void onCreateOptionsMenu(Menu menu,MenuInflater inflater ) { super.onCreateOptionsMenu(menu, inflater); // Inflate the menu; this adds items to the action bar if it is present. inflater.inflate(R.menu.menu_profile_pic, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle presses on the action bar items switch (item.getItemId()){ case R.id.action_done: Toast.makeText(getActivity().getApplicationContext(), "Saved", Toast.LENGTH_LONG).show(); } return super.onOptionsItemSelected(item); } } 

MenuActivity.java: MenuActivity.java:

 public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode,resultCode,data); ProfileFragment pm = new ProfileFragment(); pm.onActivityResult(requestCode,resultCode,data); } 

How to fix that.. 如何解决。

Thanks for advance. 感谢前进。

  • I think your code shouldn't work on any API version. 我认为您的代码不应在任何API版本上运行。 You're creating some Fragment class instance but it is local - it lives only in the onActivityResult() scope. 您正在创建一些Fragment类实例,但是它是本地的-它仅位于onActivityResult()范围内。 The ProfileFragment is already attached to your MenuActivity instance, so you should get referece to it's instance using FragmentManager or SupportFragmentManager 's methods. MenuActivity已经连接到MenuActivity实例,因此您应该使用FragmentManagerSupportFragmentManager的方法来引用它的实例。 Creating new ProfileFragment object doesn't make sense. 创建新的ProfileFragment对象没有任何意义。
  • Another thing is you should call startActivityForResult using holder Activity instance context, this way: getActivity().startActivityForResult(...) 另一件事是,您应该使用持有人Activity实例上下文调用startActivityForResult ,方法是: getActivity().startActivityForResult(...)
  • in MenuActivity 's onActivityResult() method put only call to super.onActivityResult(...) , nothing more. MenuActivityonActivityResult()方法中,仅调用super.onActivityResult(...) ,仅此而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM