2014-09-23 81 views
0

我似乎无法从fileUri创建位图。无法从URI获取位图

我希望能够将此位图设置为Imageview以预览图像,并稍后向图像添加元素。

任何想法为什么图像设置不正确?

public class FeedActivity extends Fragment implements OnClickListener { 

ImageView m_ImageView; 

ImageButton btnCamera, btnGallery; 
private final String TAG_CAMERA_FRAGMENT = "camera_fragment"; 
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; 
private Uri fileUri; 
public static final int MEDIA_TYPE_IMAGE = 1; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.activity_feed, container, false); 


    m_ImageView = (ImageView) view 
      .findViewById(R.id.imageViewFeed); 

    btnCamera = (ImageButton) view.findViewById(R.id.btn_Camera); 
    btnCamera.setOnClickListener(this); 
    btnGallery = (ImageButton) view.findViewById(R.id.btn_Gallery); 
    btnGallery.setOnClickListener(this); 

    return view; 
} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 

    case R.id.btn_Camera: 
     Log.e("CAMERA", "CAMERA BUTTON PRESSED"); 
     takePicture(); 
     break; 

    case R.id.btn_Gallery: 
     Log.e("Gallery", "GALLERY BUTTON PRESSED"); 
     break; 

    } 

} 

public void takePicture() { 

    // create Intent to take a picture and return control to the calling 
    // application 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 

    // start the image capture Intent 
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 
} 

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

     if (resultCode == getActivity().RESULT_OK) { 
      Log.e("ONACTIVITYRESULT", 
        "-----------------RESULT_OK----------------"); 

      Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath()); 
      m_ImageView.setImageBitmap(bitmap); 

      // Bundle bundle = new Bundle(); 
      // bundle.putParcelable("URI", fileUri); 
      // 
      // Fragment fragment = new PictureEditActivity(); 
      // fragment.setArguments(bundle); 
      // 
      // getFragmentManager() 
      // .beginTransaction() 
      // .replace(R.id.contentFragment, fragment, 
      // TAG_CAMERA_FRAGMENT).commit(); 

      if (fileUri != null) { 
       Log.e("CAMERA", "Image saved to:\n" + fileUri); 
       Log.e("CAMERA", "Image path:\n" + fileUri.getPath()); 
      } 

     } else if (resultCode == getActivity().RESULT_CANCELED) { 
      Log.e("ONACTIVITYRESULT", 
        "-----------------RESULT_CANCELLED----------------"); 

     } else { 

     } 
    } 

} 

/** Create a file Uri for saving an image or video */ 
private static Uri getOutputMediaFileUri(int type) { 
    return Uri.fromFile(getOutputMediaFile(type)); 
} 

/** Create a File for saving an image or video */ 
private static File getOutputMediaFile(int type) { 
    // To be safe, you should check that the SDCard is mounted 
    // using Environment.getExternalStorageState() before doing this. 

    File mediaStorageDir = new File(
      Environment 
        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), 
      "Pixagram"); 
    // This location works best if you want the created images to be shared 
    // between applications and persist after your app has been uninstalled. 

    // Create the storage directory if it does not exist 
    if (!mediaStorageDir.exists()) { 
     if (!mediaStorageDir.mkdirs()) { 
      Log.d("Pixagram", "failed to create directory"); 
      return null; 
     } 
    } 

    // Create a media file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss") 
      .format(new Date()); 
    File mediaFile; 
    if (type == MEDIA_TYPE_IMAGE) { 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator 
       + "IMG_" + timeStamp + ".jpg"); 
    } else { 
     return null; 
    } 

    return mediaFile; 
} 

}

回答

2

它看起来好像我已经找到了解决方案。

http://www.androidhive.info/2013/09/android-working-with-camera-api/

* Display image from a path to ImageView 
*/ 
private void previewCapturedImage() { 
    try { 
     // bimatp factory 
     BitmapFactory.Options options = new BitmapFactory.Options(); 

     // downsizing image as it throws OutOfMemory Exception for larger 
     // images 
     options.inSampleSize = 8; 

     final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), 
       options); 

     m_ImageView.setImageBitmap(bitmap); 
    } catch (NullPointerException e) { 
     e.printStackTrace(); 
    } 
} 
+0

如果这样可以解决它适合你,请务必将其标记为接受。代码中的更改显示为inSampleSize。这将有助于推断来自Camera的巨大分辨率图像会导致内存错误而无需设置样本大小。 – Knossos 2014-09-23 15:38:55