Question

I get an java null exception for the bitmap b in the following code: what could i do? Why doesn't the getDrawingCache function work in creating the bitmap; The onClickListener for the button that starts the action:

KaufenGListener = new OnClickListener() {

            @Override
            public void onClick(View v) {

                Coupon coupon = null;

                System.out.println("-- iv: " + couponView);

                if (clickedcoup == -1) {
                    Toast.makeText(getActivity(), "Please select a coupon", Toast.LENGTH_SHORT).show();
                } else {
                    for (int i = 0; i < coupons.size(); i++) {
                        if (clickedcoup == i) {
                            coupon = coupons.get(i);
                        }
                    }
                    String username;
                    Bitmap coupo = BitmapFactory.decodeResource(getResources(), R.drawable.coupon1);
                    Bitmap barcode = Util.generateEAN("9310779300005", getActivity(), coupo.getWidth() / 3);
                    // Drawable couponDraw = new
                    // BitmapDrawable(getResources(),b);
                    System.out.println("------------b : " + barcode);
                    if (user.address.Nachname != null) {
                        username = user.address.Nachname + " " + user.address.Vorname; // bla
                    } else {
                        username = "";
                    }
                    viewFlipperVino.setDisplayedChild(8);
                    // LayoutParams params = new LayoutParams();
                    // params.width=LayoutParams.MATCH_PARENT;
                    // params.height=(int) (params.width/1.5);

                    //couponView.buildDrawingCache();
                    couponView.setUp(coupo, barcode, coupon.Code, gutscheinName, username, coupon.Wert);
                    clickedcoup = -1;
                    String[] recipients = new String[] { "rosu_alin@ymail.com" };
                    String emailSubject = "Gutschein Schenken fur "+ gutscheinName;
                    String emailText = "Glückwunsch, Sie haben einen Gutschein für Wein & Co!!";
                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.setType("plain/text");
                    intent.putExtra(Intent.EXTRA_EMAIL, recipients);
                    intent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
                    intent.putExtra(Intent.EXTRA_TEXT, emailText);
                    File root = Environment.getExternalStorageDirectory();
                    File file = new File(root, "gutschein.png");
                    try {
                        file.createNewFile();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }                       
                    couponView.setDrawingCacheEnabled(true);
                    couponView.buildDrawingCache();
                    Bitmap b = couponView.getDrawingCache();
                    System.out.println("bitmapget drawin cache:   "+couponView.getDrawingCache());
                    Saver.saveImg(file, b, getActivity());
                    try {
                        System.out.println("bitmap"+b);
                           FileOutputStream out = new FileOutputStream(file);
                           b.compress(Bitmap.CompressFormat.PNG, 100, out);
                           System.out.println("bitmap.compress"+b.compress(Bitmap.CompressFormat.PNG, 100, out));
                    } catch (Exception e) {
                           e.printStackTrace();
                    }

                    if (!file.exists() || !file.canRead()) {
                        Toast.makeText(getActivity(), "Befestigung Fehler", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Uri uri = Uri.parse("file://" + file);
                    intent.putExtra(Intent.EXTRA_STREAM, uri);
                    startActivity(Intent.createChooser(intent, "Send email..."));
                }
            }

        };

This is the CouponView class that extends View: public class CouponView extends View {

private Bitmap coupon, barcode;
Paint paint = new Paint();
Paint paint2 = new Paint();
Paint paint3 = new Paint();
private String name;
private String code;
private String username;
private String wert;
int w;
int h;

public CouponView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public CouponView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public CouponView(Context context) {
    super(context);
    init();
}

private void init() {

    paint.setColor(0xFF000000);
    paint.setTextSize(7);
    paint2.setColor(0xFF000000);
    paint2.setTextSize(10);
    paint3.setColor(0xFF000000);
    paint3.setTextSize(25);

}

public void setUp(Bitmap coupon, Bitmap barcode, String code, String name, String username,String wert) {
    this.coupon = coupon;
    this.barcode = barcode;
    this.name = name;
    this.username = username;
    this.code = code;
    this.wert = wert;
     w = coupon.getWidth();
     h = coupon.getHeight();
    System.out.println("height"+h);
    System.out.println("width"+w);

    invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    System.out.println("-------------CouponView.onDraw()");
    canvas.drawBitmap(coupon, 0, 0, null);
    canvas.drawBitmap(barcode, w/1.61f, h/1.20f, null);
    canvas.drawText("9310779300005", w/1.429f, h/1.018f, paint);
    canvas.drawText("9310779300005", w/18.12f , h/1.073f, paint2);
    canvas.drawText(code, w/1.8875f, h/1.073f, paint2);
    canvas.drawText(username, w/11.32f, h/10.3f, paint2);
    canvas.drawText(name, w/11.32f, h/5.936f, paint2);
    canvas.drawText(wert, w/1.294f, h/1.641f, paint3);
}
}

After my logic, I set the drawingcache for the couponView to (true) and then i start building the drawingcache. And then i initiate to the bitmap the getDrawingCache(). That alone should work on saving my file.What am i missing? Also the Saves.saveIMG function does the same thing as the try catch after it (i only took it from a colleague because it was more organized. and i though it uses the imageView to save, not the bitmap.

public class Saver {
public static void saveImg(File pic, Bitmap picture,Context context) {
    folderExists(pic);
    try {
        FileOutputStream out = new FileOutputStream(pic);
        picture.compress(Bitmap.CompressFormat.JPEG, 100, out);
        Log.i(Saver.class.toString(), " picture writed at: " + pic.getAbsolutePath());
        Toast.makeText(context, "pic saved", Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        pic.delete();
        Log.i(Saver.class.toString(), " picture writing error: " + e.getMessage());
        Toast.makeText(context, "pic not saved", Toast.LENGTH_SHORT).show();
    }
}

private static void folderExists(File dir) {
    System.out.println("-=-=- : " + dir.getAbsolutePath());
    if (!dir.getParentFile().exists()) {
        dir.mkdirs();
    }

}
Was it helpful?

Solution 2

I first created an empty bitmap, and used it as a canvas to draw into it. Code:

couponView.setDrawingCacheEnabled(true);
  couponView.buildDrawingCache();
  couponView.setUp(coupo, barcode, coupon.Code, gutscheinName, username, coupon.Wert);
  Bitmap b = Bitmap.createBitmap(couponView.w, couponView.h, 
                            Config.ARGB_8888);
  Canvas canvas = new Canvas(b);
  couponView.draw(canvas);
  Saver.saveImg(file, b, getActivity());
  Uri uri = Uri.parse("file://" + file);
  intent.putExtra(Intent.EXTRA_STREAM, uri);
  startActivity(Intent.createChooser(intent, "Send email..."));

OTHER TIPS

Not sure exactly what is wrong with your code, yet this works for me to save and read bitmaps from the phones memory:

Write Bitmap to memory:

public void writeBitmapToMemory(String filename, Bitmap bitmap) {
        FileOutputStream fos;
        // Use the compress method on the Bitmap object to write image to the OutputStream
        try {
            fos = this.openFileOutput(filename, Context.MODE_PRIVATE);
            // Writing the bitmap to the output stream
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();

        } 
        catch (FileNotFoundException e) {
            e.printStackTrace();


        } 
        catch (IOException e) {
            e.printStackTrace();


        }

    }

To read:

public Bitmap readBitmapFromMemory(String filename) {
        Bitmap defautBitmap = null;
        File filePath = this.getFileStreamPath(filename);
        FileInputStream fi;
        try {
            fi = new FileInputStream(filePath);
            defautBitmap = BitmapFactory.decodeStream(fi);

        } 
        catch (FileNotFoundException e) {
            e.printStackTrace();

        }

        return defautBitmap;

    }

I hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top