0

Ok so I am supposed to make an android application but for some reason, I cannot convert my picture to a bitmap image. It's a .png image and when I try to convert it in my code, my application just crashes, no errorcode or nothing. Ive tried fixing it a ton of times, but I'm just not that good in programming and I need help, it just won't work.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                if (requestCode == FOTO_NEMEN && resultCode == RESULT_OK)
                {
                    final File file = temp;
                    try {
                        String urienzo = "file:///sdcard/DCIM/2013-01-30_13-27-28.png";
                        Uri uri = Uri.parse(urienzo);
                        Bitmap foto = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
                        if (foto == null) {
                            Toast.makeText(this, Uri.fromFile(file).toString(), Toast.LENGTH_SHORT).show();
                            return;
                        }
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        foto.compress(Bitmap.CompressFormat.PNG, 0 , bos);
                        final byte[] bytes = bos.toByteArray();
                        bos.close();
                        AsyncTask<Void,Void,Void> taak = new AsyncTask<Void,Void,Void>() {
                            @Override
                            protected Void doInBackground(Void... params) {
                                stuurAfbeelding(bytes);
                                return null;
                            }   
                        };
                        taak.execute(null,null);
                    } catch (IOException e) {
                        Log.e("Snapper","Fout bij foto nemen: " + e);
                    }
                }
            }

Whenever I get to the bitmap foto part, it crashes my application without any error message. The reason my URI is hardcoded is because I think the URI.fromfile was giving me the wrong URI, so I wanted to be sure. Now it just crashes and I have no idea what is wrong with my code. Could someone aid me?

Eneko Lismont
  • 23
  • 1
  • 4
  • Check the logcat..are you getting any runtime exceptions, Out of memory exception for example? – Mahdi Hijazi Feb 16 '13 at 14:43
  • @Eneko Lismont : You can check the answer of similar question answered by me on SO : http://stackoverflow.com/questions/18255572/android-bitmap-cache-takes-a-lot-of-memory/18255693#18255693 – arshu Aug 18 '13 at 13:23

3 Answers3

1

In my opinion you get an outOfMemmoryError.

for getting bitmap from uri you should use something like this:

public static Bitmap getThumbnail(Uri uri) throws FileNotFoundException, IOException{
    InputStream input = this.getContentResolver().openInputStream(uri);

    BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
    onlyBoundsOptions.inJustDecodeBounds = true;
    onlyBoundsOptions.inDither=true;//optional
    onlyBoundsOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;//optional
    BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
    input.close();
    if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1))
        return null;

    int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight : onlyBoundsOptions.outWidth;

    double ratio = (originalSize > THUMBNAIL_SIZE) ? (originalSize / THUMBNAIL_SIZE) : 1.0;

    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
    bitmapOptions.inDither=true;//optional
    bitmapOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;//optional
    input = this.getContentResolver().openInputStream(uri);
    Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
    input.close();
    return bitmap;
}

private static int getPowerOfTwoForSampleRatio(double ratio){
    int k = Integer.highestOneBit((int)Math.floor(ratio));
    if(k==0) return 1;
    else return k;
}

where THUMBNAIL_SIZE is size of yout thumbnail you want to get. So, it works fine andI use this code in my applications0

link How to get Bitmap from an Uri?

Community
  • 1
  • 1
pavko_a
  • 507
  • 4
  • 16
0

You could something like this :

Bitmap image;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);

and for getting it from the intent you could try something like this :

bitmap = android.provider.MediaStore.Images.Media.getBitmap(getContentResolver(), intent.getData());
String path = getRealPathFromURI(context, intent.getData());
bitmap = scaleImage(imageView, path);

where

private String getRealPathFromURI(Context context, Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(context, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
lokoko
  • 5,785
  • 5
  • 35
  • 68
0

I imagine it's crashing because that's not how you load a image from a file.

the code is MUCH simpler than what you're trying:

Bitmap bmp = BitmapFactory.decodeFile(urienzo);

and that's all! Just be sure that this path is correct, because it doesn't look correct to me.

also, if you're loading a big image (e.g. 4MP)it will crash with out of memory, because the idea of Bitmaps is to use to put stuff on the screen which currently is around HD to FullHD resolutions.

Budius
  • 39,391
  • 16
  • 102
  • 144
  • It no longer crashes! But the bitmap image is returned as null and it displays the errorcode from if(foto == null). So the bitmap part doesn't make it crash anymore so it seems. – Eneko Lismont Feb 16 '13 at 15:41
  • as I said, double check the file path because doesn't seem correct. You should use `Enviroment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/2013-01-30_13-27-28.png";` instead – Budius Feb 16 '13 at 19:03
  • right now, it crashes again, is it possible that the image is simply too large in some way? And is it possible that this is not making it crash, but I have an Async Task in there too and it's trying to do something in the background and its getting a FATAL EXCEPTION? – Eneko Lismont Feb 16 '13 at 19:07
  • what's the image resolution? And "just crash" is not good enough. On Eclipse there's a view called LogCat that shows all the logs and all the "crash" errors. There's VERY valuable information there. – Budius Feb 16 '13 at 19:16
  • The pic is 1280x720, and the errors look like this: http://gyazo.com/c98a9c4a034f418f8ccf9fa417929cb9 but I have no idea what they mean. There is also another Error but it is offscreen: http://gyazo.com/1761492cfdc33f0629ae9744d55b6a93 . To me, this means my AsyncTask is making the application crash? – Eneko Lismont Feb 16 '13 at 19:29
  • as far as I understood inside this method `stuurAfbeelding` you're calling a `Toast`. but you can't call a toast (UI stuff) on a thread that is not the UI thread. It says on the error `Caused by: java.lang.RuntimeException: Can't create a handler inside a thread that has not called Looper.prepare()` and a few lines down you see `gip.chillax.MainActivity.stuurAfbeelding(MainActivity.java:195)`. So now you go and check what are you doing on line 195, cause there is where the error happened. – Budius Feb 16 '13 at 19:35
  • http://gyazo.com/30e22f2c2b02a6114ffcebc7903cfc1e This is what I am doing in line 195, I actually see nothing wrong with it. – Eneko Lismont Feb 16 '13 at 19:54