Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法从URI获取位图_Android_Bitmap - Fatal编程技术网

Android 无法从URI获取位图

Android 无法从URI获取位图,android,bitmap,Android,Bitmap,我似乎无法从fileUri创建位图 我希望能够将此位图设置为Imageview,以便预览图像,然后向图像添加元素 知道图像设置不正确的原因吗? public class FeedActivity extends Fragment implements OnClickListener { ImageView m_ImageView; ImageButton btnCamera, btnGallery; private final String TAG_CAMERA_FRAGMENT = "ca

我似乎无法从fileUri创建位图

我希望能够将此位图设置为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;
}

}

我似乎找到了解决办法


如果这为您解决了问题,请确保将其标记为已接受。代码中的更改似乎是inSampleSize。这将有助于解释,在不设置样本大小的情况下,来自相机的高分辨率图像将导致内存错误。
 * 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();
    }
}