如何在图库中的imageview中以片段形式显示图像? [英] how to display image in imageview from gallery, in fragment?

查看:91
本文介绍了如何在图库中的imageview中以片段形式显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从图库中获取图像并从相机捕获图像,并使用片段在imageView中显示图像,但是onActivityResult()没有响应.下面是我从相机或图库中捕获图像的代码.

I tried to get image from gallery and capture from camera and display image in my imageView using fragment but the onActivityResult() does not response. Below is my code for capturing image from camera or gallery.

final CharSequence[] items = { "Take Photo", "Choose from Library",
            "Cancel" };

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Add Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals("Take Photo")) {
                final Intent intent = new Intent(
                        MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
                startActivityForResult(intent, CAPTURE_IMAGE);
            } else if (items[item].equals("Choose from Library")) {

                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, ""),
                        PICK_IMAGE);
            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    System.out.println("OnActivityResult Call");
    if (resultCode != Activity.RESULT_CANCELED) {
        if (requestCode == PICK_IMAGE) {
            imagePath = getAbsolutePath(data.getData());
            myImageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageLoader.displayImage("file://" + imagePath, ivUpload,
                    options);
        } else if (requestCode == CAPTURE_IMAGE) {
            imagePath = getImagePath();
            System.out.println("IMAGE PATH===" + imagePath);
            myImageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageLoader.displayImage("file://" + imagePath, ivUpload,
                    options);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}

private String getAbsolutePath(Uri uri) {
    String[] projection = { MediaColumns.DATA };
    @SuppressWarnings("deprecation")
    Cursor cursor = getActivity().managedQuery(uri, projection, null, null,
            null);
    if (cursor != null) {
        int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } else
        return null;
}

private Uri setImageUri() {
    File file = new File(Environment.getExternalStorageDirectory()
            + "/DCIM/", "image" + new Date(CAPTURE_IMAGE).getTime()
            + ".png");
    Uri imgUri = Uri.fromFile(file);
    this.imagePath = file.getAbsolutePath();
    return imgUri;
}

private String getImagePath() {
    return imagePath;
}

请给我有关该片段的任何解决方案.我也尝试在活动中.这是可行的,但片段中不会上传图库中的图片.

please give me any solution for that fragment. i also try with in activity. it is work but in fragment does not upload image from gallery.

推荐答案

如果您具有图像路径,则可以直接从图像路径显示图像.

If you have Image path, you can directly display image from image path..

您可以这样写...

 imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

BitmapFactory.decodeFile() 方法允许您从文件路径解码图像.因此您可以通过setImageBitmap()方法将解码后的图像直接设置为ImageView.

BitmapFactory.decodeFile() method allows you to decode image from file path. so you can set decoded image directly to the ImageView by setImageBitmap() method.

我在这里添加示例代码以进行选择..
您可以参考一下,看看那里出了什么问题.

Here I am adding sample code for picking intent..
You can take reference and see whats the problem there..

调用图像意图

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 = context.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));
            btn_set.setEnabled(true);
            cursor.close();
        } else {
            Toast.makeText(getActivity(), "Try Again!!", Toast.LENGTH_SHORT)
                    .show();
        }

}

这可能会对您有所帮助.

This may help you..

这篇关于如何在图库中的imageview中以片段形式显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆