首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从最低版本获取onActivityResult

如何从最低版本获取onActivityResult
EN

Stack Overflow用户
提问于 2015-08-26 19:48:14
回答 1查看 48关注 0票数 0

在我的应用程序中,我可以选择从图库中选择图片。我想把图像放在图像view...my代码完美地工作(将图像放在图像视图上)最高版本(API21),但不工作(放置图像上图像视图)最低版本(API15)。请任何人帮帮我!

我的应用配置:

代码语言:javascript
复制
    minSdkVersion 14
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"

我的代码如下:

代码语言:javascript
复制
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:

代码语言:javascript
复制
public void onActivityResult(int requestCode, int resultCode, Intent data) {
       super.onActivityResult(requestCode,resultCode,data);
        ProfileFragment pm = new ProfileFragment();
        pm.onActivityResult(requestCode,resultCode,data);
        
    }

如何解决这个问题..

谢谢你的预支。

EN

回答 1

Stack Overflow用户

发布于 2015-08-26 20:15:08

我认为你的代码不应该在任何版本上工作。您正在创建一些片段类实例,但它是本地的-它只存在于onActivityResult()作用域中。ProfileFragment已附加到您的MenuActivity实例,因此您应该使用FragmentManagerSupportFragmentManager的方法获取对其实例的引用。创建新的ProfileFragment对象没有意义。

  • 另一件事是您应该使用持有者活动实例上下文调用startActivityForResult,这样:getActivity().startActivityForResult(...)
  • in MenuActivityonActivityResult()方法只将调用放入<代码>D15,不会有其他调用。<代码>H216<代码>F217
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32225827

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档