簡體   English   中英

如何從最低版本獲取onActivityResult

[英]How to get onActivityResult from lowest version

在我的應用程序中,我可以選擇從圖庫中選擇圖片。 我想將圖像放置在圖像視圖上...我的代碼對於最高版本(API 21)可以完美地工作(將圖像放置在圖像視圖上),但對於最低版本(Api 15)則無法工作(將圖像放置在圖像視圖上)。 請任何人幫助我!

我的應用程序配置:

    minSdkVersion 14
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"

我的代碼在這里:

 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:

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

如何解決。

感謝前進。

  • 我認為您的代碼不應在任何API版本上運行。 您正在創建一些Fragment類實例,但是它是本地的-它僅位於onActivityResult()范圍內。 MenuActivity已經連接到MenuActivity實例,因此您應該使用FragmentManagerSupportFragmentManager的方法來引用它的實例。 創建新的ProfileFragment對象沒有任何意義。
  • 另一件事是,您應該使用持有人Activity實例上下文調用startActivityForResult ,方法是: getActivity().startActivityForResult(...)
  • MenuActivityonActivityResult()方法中,僅調用super.onActivityResult(...) ,僅此而已。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM