如何从最低版本

时间:2015-08-26 11:48:14

标签: java android

在我的应用中,我可以选择从图库中选择图片。我想将图像放在图像视图上...我的代码完美地工作(在图像视图上放置图像)用于最高版本(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);
        
    }

如何解决..

感谢您提前。

0 个答案:

没有答案