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?

2
  • Check the logcat..are you getting any runtime exceptions, Out of memory exception for example? Commented Feb 16, 2013 at 14:43
  • @Eneko Lismont : You can check the answer of similar question answered by me on SO : stackoverflow.com/questions/18255572/… Commented Aug 18, 2013 at 13:23

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

3 Comments

The this keyword is not valid in a static statement.
When I use this code and i put it as a method and call it right at the same spot as in my code, it just crashes instantly, normally it would take a second or 2, now it crashes instantly.
@Svexo it's because you have not defined THUMBNAIL_SIZE
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.

7 Comments

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.
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
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?
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.
The pic is 1280x720, and the errors look like this: gyazo.com/c98a9c4a034f418f8ccf9fa417929cb9 but I have no idea what they mean. There is also another Error but it is offscreen: gyazo.com/1761492cfdc33f0629ae9744d55b6a93 . To me, this means my AsyncTask is making the application crash?
|
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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.