面对从图库中获取图像并在图像视图中以片段

时间:2015-07-29 08:56:39

标签: android android-fragments android-imageview

  

我在片段中工作,我需要从中选择一个图像   画廊并将其设置为imageView。代码运行良好,没有崩溃,没有错误,但图像没有进入ImageView。

这是我的代码: -

 public class ProfileOzoneSetting extends Fragment implements OnClickListener   
{

 Boolean setImg ;
NetworkImageView genSetOzoneUserImage;

     private static int RESULT_LOAD = 1;

        @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                View rootView = inflater.inflate(R.layout.profiesettingozone, container, false);

        genSetOzoneUserImage = (NetworkImageView)rootView.findViewById(R.id.genSetOzoneUserImage);

       genSetOzoneUserImage.setOnClickListener(new 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);
            getActivity().startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);

        }
    });


    APIAccess.fetchData(ProfileOzoneSetting.this, getActivity(), getActivity());
    return rootView;

}
@Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {

            //Toast.makeText(getActivity(), "onactivityyyyyfor result ", Toast.LENGTH_SHORT);

            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                System.out.println("Image Path : " + selectedImagePath);
                genSetOzoneUserImage.setImageURI(selectedImageUri);

                setImg = true;
            }
        }
    }

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
     AppContoller.getInstance().pref=getActivity().getSharedPreferences(AppContoller.getInstance().PREF_NAME, 0);

        strUserID=AppContoller.getInstance().pref.getString(AppContoller.getInstance().USER_ID, "0");
        APIAccess.fetchData(ProfileOzoneSetting.this, getActivity(), getActivity());
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

请让我知道我在这里犯了什么错误???

1 个答案:

答案 0 :(得分:0)

谢谢。我在你的代码中做了一些修改。我应该工作。

public class ProfileOzoneSetting extends Fragment implements OnClickListener{

        Boolean setImg ;
        NetworkImageView genSetOzoneUserImage;
        private static int RESULT_LOAD = 1;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            View rootView = inflater.inflate(R.layout.profiesettingozone, container, false);
            genSetOzoneUserImage = (NetworkImageView)rootView.findViewById(R.id.genSetOzoneUserImage);
            genSetOzoneUserImage.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent galleryIntent = new Intent();
                    // Show only images
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent, "Choose Image"), RESULT_LOAD);
                }

            });
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data){
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

                Uri uri = data.getData();

                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                    genSetOzoneUserImage.setImageBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    catch(Exception e){

    }
    }
    }