1

My app calls the camera to take a picture and save it into my app local directory (getApplicationContext().getFilesDir()) which works fine.

When I try to convert the picture into a bitmap using BitmapFactory the result is null. This the code I use :

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;

String picturePath = pictureFile.getAbsolutePath();
Bitmap bitmap = BitmapFactory.decodeFile(picturePath, options);

Note that pictureFile was created as follows :

pictureFile = File.createTempFile(
                    imageFileName,  /* prefix */
                    ".jpg",         /* suffix */
                    foodwarnDir      /* directory */
            );
Fred Sullet
  • 371
  • 1
  • 6
  • 18

4 Answers4

1

Didn't you forget add permissions READ_EXTERNAL_STORAGE and/or WRITE_EXTERNAL_STORAGE ?

File locationOfFile = new 
File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/images");
File destination= new File(locationOfFile , fileName + ".JPG");
FileInputStream fileInputStream;
fileInputStream= new FileInputStream(destination);
Bitmap img = BitmapFactory.decodeStream(fileInputStream);

OR

This is my working code in my project here:

            View imageHolder = LayoutInflater.from(this).inflate(R.layout.image_item, null);
            ImageView thumbnail = (ImageView) imageHolder.findViewById(R.id.media_image);

            try {

                String path = uri.getPath();
                Bitmap bmImg = BitmapFactory.decodeFile(path);

                Point p = new Point();
                p.set(100, 100);
                Bitmap bitmapp = waterMark(bmImg, mRefNo, p, Color.RED, 90, 60, true);

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmapp.compress(Bitmap.CompressFormat.PNG, 100, stream);
                Glide.with(this)
                        .load(stream.toByteArray())
                        .asBitmap()
                        .error(R.mipmap.ic_launcher)
                        .into(thumbnail);

                mSelectedImagesContainer.addView(imageHolder);
                thumbnail.setLayoutParams(new FrameLayout.LayoutParams(wdpx, htpx));
            } catch (Exception e) {
                e.printStackTrace();
            }

Hope this helps you other helpful Links1 Link2

InsaneCat
  • 2,115
  • 5
  • 21
  • 40
0

Create temp file:

File tempFile = File.createTempFile("temp_file, ".jpg", this.getExternalCacheDir());

get path created:

String mPath = tempFile.getAbsolutePath();

now in you activityResult

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeFile(mPath, options);
Yossi
  • 327
  • 2
  • 5
0

Assuming you're using java.io.File class. According to Java docs function .createTempFile creates empty file on the System.

As such, this file will have only meta info without any content, with zero length, and this is probably a reason why it is not possible to extract Bitmap.

So you need to Create File Object instance instead of actual file, using new File()

You can also use WeakReference and similar to Bitmap you create if you're looking to decrease chance of memory leaks in early implementation.

Kiran Rao
  • 321
  • 2
  • 11
Gotiasits
  • 1,135
  • 1
  • 10
  • 21
  • this is what is recommended on android developper page https://developer.android.com/training/camera/photobasics.html#TaskScalePhoto. My code is inspired by it so I guess it is correct – Fred Sullet Apr 12 '18 at 14:23
  • Guide you provided also "puts something" in this newly created file in `dispatchTakePictureIntent()` . Try debugging, by reading `.length` of the file before you extract Bitmap. If it is 0, you have a good chance of finding a problem. – Gotiasits Apr 12 '18 at 14:32
  • sorry but I see nothing being put in the newly created file in their code. I debugged as you advised and file length is indeed 0 but why ?? – Fred Sullet Apr 12 '18 at 14:40
  • From guide, `startActivityForResult()` will put picture taken by camera to newly created file, after user takes a picture. – Gotiasits Apr 12 '18 at 14:43
  • Nope. I basically don't have to store the picture taken by the camera as I have to OCR it to extract the text. I plan to use the camera API to directly get a bitmap. Is it feasible ? – Fred Sullet Apr 17 '18 at 10:21
  • Everything is possible, its just a matter of time. But I would say that this is a different question. It seems to me that "Why does it returns null" is answered: the file is empty. – Gotiasits Apr 17 '18 at 10:25
0

use

data.getExtras().get("data");//for getting bitmap
Uri u = intent.getData();// for getting the Uri and get the path from uri

for getting data from your camera

Hitesh Sarsava
  • 666
  • 4
  • 15