2015-05-11 77 views
0

我有一个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); 
    } 

回答

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); 
+0

嗨devcelebi,是“数据”返回onActivityResult数据和什么是“ExifInterface”和“C”?感谢您的快速回复。 –

+0

orientation.c的Exifinterface是文件的名称。如果您不需要方向控制,则不会写入exif.You可以尝试在此代码中使用bitmapfactory.options – devcelebi

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。

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

+0

可以回答自己的问题,但可以发布完整的解决方案准确描述了您为解决问题所做的工作,并包含为了使其工作而进行的所有代码更改。 –

相关问题