从图库中获取图像以在片段中的imageview中设置?

时间:2015-12-08 12:31:14

标签: android android-fragments

如何从图片中的Imageview中设置gallary图像? onActivityResult没有在片段中调用..这是我在片段

中编写的代码
img_user.setOnClickListener(new View.OnClickListener() {
            @Override
        public void onClick(View v) {

            Intent i = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, RESULT_LOAD_IMAGE);


        }
    });


public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


        if (data != null) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContext().getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);

            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            img_user.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            cursor.close`enter code here`();
        } else {
            Toast.makeText(getActivity(), "Try Again!!", Toast.LENGTH_SHORT)
                    .show();
        }

    }

3 个答案:

答案 0 :(得分:4)

你可以这样做....在你的主要活动中

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       //call super 
       super.onActivityResult(requestCode, resultCode, data);
     }

并在你的片段中

 Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                // ******** code for crop image
                i.putExtra("crop", "true");
                i.putExtra("aspectX", 100);
                i.putExtra("aspectY", 100);
                i.putExtra("outputX", 256);
                i.putExtra("outputY", 356);

                try {

                    i.putExtra("return-data", true);
                    startActivityForResult(
                            Intent.createChooser(i, "Select Picture"), 0);
                }catch (ActivityNotFoundException ex){
                    ex.printStackTrace();
                }

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==0 && resultCode == Activity.RESULT_OK){
            try {
                Bundle bundle = data.getExtras();
                Bitmap bitmap = bundle.getParcelable("data");
                img_user.setImageBitmap(bitmap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

适合我的作品

答案 1 :(得分:1)

我也遇到了同样的问题。实际上startActivityForResult()的回调会回到父活动,所以你需要做 从片段到onActivityResult()函数的显式调用如下。

在Parent Activity类中,覆盖onActivityResult()方法,甚至覆盖Fragment Class中的相同方法,并调用以下代码。

在父类中:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Fragment fragment = getFragmentManager().findFragmentById(R.id.container);
    fragment.onActivityResult(requestCode, resultCode, data);
}

儿童班: Extends片段为imageview上的图像设置执行逻辑。

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {



        if (data != null) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);

                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                 String picturePath = cursor.getString(columnIndex);
                image.setImageBitmap(BitmapFactory.decodeFile(picturePath));

                cursor.close();
            } else {
                Toast.makeText(getActivity(), "Try Again!!", Toast.LENGTH_SHORT).show();
            }
            super.onActivityResult(requestCode, resultCode, data);
        }

答案 2 :(得分:0)

请找到以下代码,它会起作用。在片段中添加此内容

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

//Your stuff

}