1

I work with image caputre on android. I try to take a picture and send image after saving image on memory device. Some devices like LG return null for URI image. I dont know why get this value on LG and Samsung S4. S5 work great.

Code:

Start camera intent and create file directory:

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

Code for activity result:

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();" return null exception. Need help.

Michael
  • 780
  • 1
  • 10
  • 34

0 Answers0