Creating bitmap from canvas java
Asked Answered
A

2

0

SOLUTION

Thanks to @ChandraSekhar's suggestions the issue was that I was passing in an Immutable Bitmap to the canvas constructor. The solution is to create a copy of it when using BitmapFactory.decodeFile();

Bitmap bmp = BitmapFactory.decodeFile(imageURL).copy(Bitmap.Config.ARGB_8888, true);

So I have a bitmap that I am using bitmapFactory.decodeFile() for and this works. I am able to create the bitmap, then I need to create a canvas and this is where things get weird.

Here's the flow of what is happening. I capture an image, then pass it to functionA that sizes it, and saves it out and returns its file path. ( I am using Phonegap Cordova )

I then pass that URL back to my java and use the previously saved image and manipulate it in functionB

CODE IN QUESTION:

// GET URL TO IMAGE
final JSONObject options = optionsArr.optJSONObject(0);
String imageURL = options.optString("image");

// create image bitmap
Bitmap bmp = BitmapFactory.decodeFile(imageURL);
bmp = Bitmap.createBitmap(bmp,0,0,655,655);

/* Everything works fine until this point */

// create image canvas
Canvas canvas = new Canvas(bmp);
Bitmap one = Bitmap.createBitmap(bmp);
canvas.drawBitmap(one,0,0,null);

I receive no errors, it just hangs. Here's the kick in the pants - if I run another function say functionB first that one works but the other doesn't.

I thought maybe I needed to flush and close my first FileOutputStream, but that didn't seem to have any effect. I've tried different variable names for all elements, bitmaps, canvas, and fileoutputstreams.

here is an example of the full function... NOTE: Because I am using phonegap / cordova I am returning a string

public String none(JSONArray optionsArr) {

    // SET FILE PATH
            String filePath = "";
            File path = new File(Environment.getExternalStorageDirectory()+"/myApp/");

            // TMP.jpg is where we store our temporary version of the image
            File NewFilePath = new File(path, "tmp_NBB.jpg");

            // CREATE FOLDERS IF NEEDED
            try{
                boolean success = false;

                if(!path.exists()){
                    success = path.mkdir();
                }
                if (!success){ 
                    Log.d("NONE","Folder not created.");
                }
                else{
                    Log.d("NONE","Folder created!");
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }
            // GET URL TO IMAGE
                final JSONObject options = optionsArr.optJSONObject(0);
                String imageURL = options.optString("image");

                // create image bitmap
                Bitmap bmp = BitmapFactory.decodeFile(imageURL);
                bmp = Bitmap.createBitmap(bmp,0,0,655,655);

                // create image canvas
                Canvas canvas = new Canvas(bmp);
                Bitmap none = Bitmap.createBitmap(bmp);
                canvas.drawBitmap(none,0,0,null);

                // SAVE IMAGE
                try {
                    // OUTPUT STREAM
                    FileOutputStream out = new FileOutputStream(NewFilePath);
                    none.compress(Bitmap.CompressFormat.JPEG, 100, out);

                    // GET FILE PATH
                    Uri uri = Uri.fromFile(NewFilePath);
                    filePath = uri.toString();

                    try{
                        out.flush();
                        out.close();

                        // RETURN FILE PATH
                        return filePath;
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
            return filePath;
        }

Like I said this works for the first image, but when I attempt to open this image again, based on the returned filepath it chunks out at the create canvas line.

edit: The image path I am using looks like this: /mtn/sdcard/myApp/tmp.jpg thoughts?

Abutilon answered 4/6, 2012 at 13:27 Comment(0)
S
4
Bitmap one = Bitmap.createBitmap(bmp);

In the above code bmp is a Bitmap and you are creating another Bitmap object one from bmp.

Remove that line and try by changing

canvas.drawBitmap(one,0,0,null);

to

canvas.drawBitmap(bmp,0,0,null);
Silvio answered 4/6, 2012 at 13:31 Comment(5)
I just tried that, the issue is that it doesn't get past the line above for Canvas canvas = New Canvas(bmp);Abutilon
@DrewDahlman it should not create any problem. by the way what problem it is showing there? Did you tried to debug it?Silvio
hmm... I am super new to Java - how would I debug that? I have logging happening along the way to check to see where things get hung up and it appears to be in that canvas creation area.Abutilon
Just added a try catch and the problem is "Immutable bitmap passed to canvas constructor" which I found a possible solution for. you may have just pushed me in the right direction.Abutilon
got it! :) the issue was that I was passing an immutable bitmap, all I needed to do was copy it!Abutilon
C
0

Are you sure, the device on which you are running supports image size:655x655? Does bitmap get created?

Carencarena answered 4/6, 2012 at 13:44 Comment(3)
Yes like I said this works the first time, and I can see the created image on my SDcard, but when i attempt to use that new image it fails. Oddly enough - if I use the raw image, before editing it doesn't fail..Abutilon
Can you please explain why have you created a bitmap from already created bitmap (Bitmap one = Bitmap.createBitmap(bmp);)? As its a method that requires a lot of internal computation. Can you omit any of the call.. Obviously this will not solve your problem but just for info... :)Carencarena
Total beginner to Java :P I have removed that code as @ChandraSekhar suggested, but the problem is that it never gets past the Canvas canvas = New Canvas(bmp);Abutilon

© 2022 - 2024 — McMap. All rights reserved.