无法从URI获取位图 [英] Unable to get bitmap from URI

查看:61
本文介绍了无法从URI获取位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法从fileUri创建位图.

I seem to be having trouble creating a Bitmap from fileUri.

我希望能够将此位图设置为Imageview以便预览图像,然后在图像中添加元素.

I would like to be able to set this Bitmap to an Imageview for previewing the image, and later adding elements to the image.

有什么想法为什么不能正确设置图像?

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;
}

}

推荐答案

好像我找到了解决方案.

It appears as if I have found a solution.

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();
    }
}

这篇关于无法从URI获取位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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