Question

I am trying to open a saved image in the gallery, and after writing some text on this opened image, trying to close it. But so far, it doesn't work. Could you tell me what I am doing wrong please?

**I checked the path, it is correct. Here is the my code :

String path = android.os.Environment.getExternalStorageDirectory().toString() + "/DCIM/100LGDSC/";
String pathiki = path+filename;
Log.d("pathiki:",pathiki);

try {
  Bitmap bm = BitmapFactory.decodeFile(pathiki);  
  Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);

  Paint paint = new Paint();
  paint.setStyle(Style.FILL);
  paint.setColor(Color.WHITE);
  paint.setTypeface(tf);
  paint.setTextAlign(Align.CENTER);
  paint.setTextSize(14);

  Canvas canvas = new Canvas(bm);
  canvas.drawText("bla bla bla", 100, 100, paint);

  OutputStream fOut = new FileOutputStream(new File(pathiki));
  bm.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
  fOut.flush();
  fOut.close();
} catch (Exception e) {
  // TODO: handle exception
  e.toString();
}
Was it helpful?

Solution

BitmapFactory.decodeFile always returns an immutable bitmap. Use Bitmap.copy to make a copy of bitmap which is mutable. Now perform modifications on the copied bitmap.

Bitmap bm = BitmapFactory.decodeFile(pathiki).copy(Bitmap.Config.ARGB_8888, true); 

Update the exception handler code. Either log e.getMessage() to logcat or use e.printStackTrace().

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