从相机文件夹将图像设置为imageview

时间:2015-05-11 10:45:43

标签: android

我有一个ImageView,其中需要设置图库所选图像,但是当我选择相机文件夹图像时,ImageView中未设置所选图像。

 if (requestCode == REQUEST_PICK_IMAGE_FROM_GALLERY ) {

            Uri selectedImg = data.getData();
            String[] filePath = {MediaStore.Images.Media.DATA};
            Cursor c = getActivity().getContentResolver().query(selectedImg, filePath, null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePath[0]);
            String picturePath = c.getString(columnIndex);
            bitmap = BitmapFactory.decodeFile(picturePath,options);
            mUserProfileImage.setImageBitmap(bitmap);
    }

2 个答案:

答案 0 :(得分:1)

if(selectedImg!=null) { c = getActivity().getContentResolver().query(selectedImg, filePath, null, null, null); }else{ Toast.makeText(mContext,"This image does not exiest in device",Toast.LENGTH_SHORT).show(); return; } f = new File(picturePath);

mUserProfileImage.setImageBitmap(BitmapFactory.decodeFile(路径,选项)); //在复制的图像后调用此方法,然后在

之后删除
    if (f != null) {
        f.delete();  // previously i set f in imageView and already handled NPE, so no crash appeared.
    }

我得到了问题的解决方案,当我从相机捕获图像时,它存储在temp.png中,我将此图像复制到其他路径并删除该temp.png,所以当我再次尝试设置删除时图像,它没有设置,我也处理了NEP。

我调试并纠正了代码。 :)

答案 1 :(得分:0)

Bitmap realImage;
         final BitmapFactory.Options options = new BitmapFactory.Options();
          options.inSampleSize = 5;

            options.inPurgeable=true;                   

            options.inInputShareable=true;             


        realImage = BitmapFactory.decodeByteArray(data,0,data.length,options);
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(path + c.getTime().getSeconds()
                    + ".jpg");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            Log.d("EXIF value",
                    exif.getAttribute(ExifInterface.TAG_ORIENTATION));
            if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
                    .equalsIgnoreCase("1")) {
                realImage = rotate(realImage, 90);
            } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
                    .equalsIgnoreCase("8")) {
                realImage = rotate(realImage, 90);
            } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
                    .equalsIgnoreCase("3")) {
                realImage = rotate(realImage, 90);
            } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
                    .equalsIgnoreCase("0")) {
                realImage = rotate(realImage, 90);
            }
        } catch (Exception e) {

        }

        image.setImageBitmap(realImage);