Android拍摄图片获取为空

时间:2015-04-10 13:09:46

标签: android image android-intent take

我在android上使用图像caputre。在存储设备上保存图像后,我尝试拍照并发送图像。像LG这样的设备为URI镜像返回null。我不知道为什么在LG和三星S4上获得这个价值。 S5很棒。

代码:

启动摄像头意图并创建文件目录:

public class PhotoIntentHelper
{
    public static final int YOUR_SELECT_PICTURE_REQUEST_CODE = 4;
    public Uri outputFileUri = null;

    public void startCamera(Context ctx)
    {
        generateTimeStampPhotoFileUri(ctx);

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
        ((ActivityBaseBar)ctx).startActivityForResult(intent, YOUR_SELECT_PICTURE_REQUEST_CODE);
    }

    private void generateTimeStampPhotoFileUri(Context ctx) 
    { 

        final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir" + File.separator);
        if(!root.exists())
        {
            root.mkdirs();
        }

        final String fname = "img_"+ System.currentTimeMillis() + ".jpg";
        final File sdImageMainDirectory = new File(root, fname);
        outputFileUri = Uri.fromFile(sdImageMainDirectory);
    }

    public Bitmap imageFromUri()
    {
        String url = outputFileUri.getPath();

        if(url == null || (url != null && url.length() == 0))
        {
            return null;
        }

        //First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(url, options);

        // Calculate inSampleSize, Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        int inSampleSize = 1;

        if (height > 600) 
        {
            inSampleSize = Math.round((float)height / (float)800);
        }
        int expectedWidth = width / inSampleSize;

        if (expectedWidth > 800) 
        {
            //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
            inSampleSize = Math.round((float)width / (float)600);
        }

        options.inSampleSize = inSampleSize;

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        Bitmap bitmap = BitmapFactory.decodeFile(url,
                options); 

        bitmap = ExifUtils.rotateBitmap(url, bitmap);

        return bitmap;
    }
...
}

活动结果代码:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        Bitmap imageBitmap = null;

        if(resultCode == RESULT_OK)
        {
            if(requestCode == PhotoIntentHelper.YOUR_SELECT_PICTURE_REQUEST_CODE )
            {
                imageBitmap = photoHelper.imageFromUri();
            }
            else
            {
                Toast.makeText(this, "sadasda", Toast.LENGTH_SHORT).show();
                return;
            }
            if(imageBitmap != null)
            {
                //something
            }
            else
            {
                Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
            }

        }
    }

Line“String url = outputFileUri.getPath();”返回null异常。 需要帮助。

0 个答案:

没有答案