I need to convert string in the edittext to bitmap, but i am not getting the string but instead i am getting this (see the image)
my code is as follows
Canvas c=new Canvas();
MainActivity.editText.setCursorVisible(false);
MainActivity.editText.buildDrawingCache();
Bitmap bmp = Bitmap.createBitmap(MainActivity.editText.getDrawingCache());
System.out.println("string is "+MainActivity.editText.getText().toString());
File f =new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Photo Text");
if(!f.exists())
{
f.mkdirs();
}
f = new File(f.getAbsolutePath(),
String.valueOf(System.currentTimeMillis()) +"phototext.jpg");
if(!f.exists())
{
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
bmp.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(f));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c.drawBitmap(bmp,0,0, mPaint);
Please suggest me. I need the string from the edittext as a bitmap.
Why not get the text of the ExitText and then draw it on Canvas with drawText()?
String text = editText.getText().toString();
canvas.drawText(text, 0, 0, paint);
and set the canvas height and width depending of text height and length.
Related
My project has 30 images in drawable & I would like to save/copy all these images to sd card on a button click. I'm using below code to save image to sd card but I don't want to copy paste this code 30 times to save all the images. So is there any better solution for this problem. Thanks
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.aurora);
String fileName = "aurora.png";
File sd = Environment.getExternalStorageDirectory();
File folder = new File(sd + "/Wallpaper Pack");
folder.mkdir();
File dest = new File(folder, fileName);
try {
FileOutputStream out;
out = new FileOutputStream(dest);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Its very simple, create a array and loop it.
int[] drawablesArr = {R.id.name1, R.id.name2, ....}
for(int i=0l i<=drawablesArr.length; i++){
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), drawablesArr[i]);
String fileName = "image_"+ String.valueOf(i)+".png" ;
File sd = Environment.getExternalStorageDirectory();
File folder = new File(sd + "/Wallpaper Pack");
folder.mkdir();
File dest = new File(folder, fileName);
try {
FileOutputStream out;
out = new FileOutputStream(dest);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Murtaza Hussain's answer is right but it is better to run such operations out of UI (main) Thread. So, you can use ThreadPoolExecutor:
// SaveThread.java
public class SaveThread implements Runnable {
private int drawable;
private String fileName;
private Context context;
public SaveThread(Context context, int drawable, String fileName) {
this.drawable = drawable;
this.fileName = fileName;
this.context = context;
}
#override
public void run() {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), drawable);
File dest = new File(Environment.getExternalStorageDirectory(), "WallpaperPack/" + fileName);
dest.getParentFile().mkdirs();
try {
FileOutputStream out = new FileOutputStream(dest);
bitmap.compress(CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
then inside your activity or in other components:
int core = Runtime.getRuntime().availableProcessors();
ExecutorService executor =
new ThreadPoolExecutor(
core + 1,
core * 2 + 1,
60l,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>()
);
int[] drawables = {R.id.name1, R.id.name2, ....}
for(int drawable : drawables) {
executor.execute(new SaveThread(getApplicationContext(), drawable, "image_"+ drawable +".png"));
}
executor.shutdown();
Sorry for my bad english,if you do not understand something tell me that I try to explain it better
Hello, i'm doing a test, i want first draw using canvas and a surfaceview a line, then i want to save it on my smartphone... i try this code but it saves only black image..Do you know why?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.salvaaa);
SurfaceView surface = (SurfaceView) findViewById(R.id.surface);
surface.getHolder().addCallback(new Callback() {
private Context context;
#Override
public void surfaceCreated(SurfaceHolder holder) {
Paint paint = new Paint();
paint.setColor(Color.WHITE);
Canvas canvas = holder.lockCanvas();
canvas.drawColor(Color.WHITE);
canvas.drawLine(20,20,30,30, paint);
holder.unlockCanvasAndPost(canvas);
Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
Context context = getBaseContext();
this.context=context;
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut=null;
File file = new File(path, "immasine.jpg");
try {
fOut= new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
} });
}
EDIT------------
I've change the code, now it works
super.onCreate(savedInstanceState);
setContentView(R.layout.salvaaa);
Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawColor(Color.WHITE);
canvas.drawLine(20,20,30,30, paint);
OutputStream fOut=null;
File file = new File(getFilesDir(), "isne");
try {
fOut= new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(this.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
// holder.unlockCanvasAndPost(canvas);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I am going to create a jpeg by drawing. The element is a text and a image. When it comes to the implementation, only image and background color is drawn but there is no text. I have no diea what happens actually even I have called canvas.drawText
The below is my code
String folderName = "droidCanvas";
String textString ="Hello , I am user 12345. \n Below is my signature.";
String fileNameString = "test.jpg";
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + folderName );
if(!folder.exists()){
folder.mkdir();
}
File bmpFile = new File(folder.getAbsolutePath() + File.separator + fileNameString );
if(!bmpFile.exists()){
try {
bmpFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Draw something
Paint paint = new Paint();
paint.setColor(Color.RED);
Paint txtPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
txtPaint.setColor(Color.BLACK);
txtPaint.setTextSize(20);
bmpBase = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bmpBase);
canvas.drawColor(Color.WHITE);
canvas.drawCircle(w/2, h/2, 300, paint);
canvas.drawText(textString ,w/2, 0 , txtPaint);
//Export to jpeg
try
{
fos = new FileOutputStream(bmpFile);
bmpBase.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
fos = null;
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (fos != null)
{
try
{
fos.close();
fos = null;
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Try canvas.drawText(textString ,w/2, h/2 , txtPaint); and you will be able to at least see the text. Then you can re-position it later. Right now it is being drawn but you can't see it because its at extreme top center and out of bounds.
If you want to place it on top left corner then do this:
canvas.drawText(textString ,0 , txtPaint.getFontSpacing(), txtPaint);
How to create PDF file with multiple pages from image file in Android? I created one PDF file from image. That PDF file has one page. That is half of that image. In the right side search part is cut in PDF file.
I am using itext-5.3.4.jar for create PDF.
wbviewnews.loadUrl("http://developer.android.com/about/index.html");
// button for create wbpage to image than image to PDF file
Button btnclick =(Button)findViewById(R.id.btnclick);
btnclick.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Picture p = wbviewnews.capturePicture();
bitmap=null;
PictureDrawable pictureDrawable = new PictureDrawable(p);
bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
//Bitmap bitmap = Bitmap.createBitmap(200,200, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
ImageView imgdata=(ImageView)findViewById(R.id.imgdata);
imgdata.setImageBitmap(bitmap);
String filename = "pippo.png";
File sd = Environment.getExternalStorageDirectory();
File dest = new File(sd, filename);
String pdffilename = "pippo.pdf";
File pdffilepath = new File(sd, pdffilename);
try {
FileOutputStream out = new FileOutputStream(dest);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
Log.e("Exception", e.toString());
}
Document document=new Document();
try {
Log.e("pdffilepath", pdffilepath.toString());
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(pdffilepath));
document.open();
// URL url = new URL (Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+filename);
// Log.e("url", url.toString());
Image image = Image.getInstance(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+filename) ;
document.add(image);
document.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("FileNotFoundException", e.toString());
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("DocumentException", e.toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("MalformedURLException", e.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("IOException", e.toString());
}
}
});
Your search through StackOverflow is less I guess cause I found these answer already there having solution, yes its contained in different answer and looking at the Q/A I guess they can solve your problem, if not then keep trying :)
how to Generate Pdf File with Image in android?
How to create a PDF with multiple pages from a Graphics object with Java and itext
iText Example
if your linear layout height is very large than try this code
https://demonuts.com/android-generate-pdf-view/
follow this link and just change the lines
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(convertWidth, 10000, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();
Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawPaint(paint);
Bitmap bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth, 10000, true);
canvas.drawBitmap(bitmap, 0, 0 , null);
document.finishPage(page);`
If you want to create a pdf file with multiple images you can use PdfDocument from Android. Here is a demo:
private void createPDFWithMultipleImage(){
File file = getOutputFile();
if (file != null){
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
PdfDocument pdfDocument = new PdfDocument();
for (int i = 0; i < images.size(); i++){
Bitmap bitmap = BitmapFactory.decodeFile(images.get(i).getPath());
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(bitmap.getWidth(), bitmap.getHeight(), (i + 1)).create();
PdfDocument.Page page = pdfDocument.startPage(pageInfo);
Canvas canvas = page.getCanvas();
Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawPaint(paint);
canvas.drawBitmap(bitmap, 0f, 0f, null);
pdfDocument.finishPage(page);
bitmap.recycle();
}
pdfDocument.writeTo(fileOutputStream);
pdfDocument.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private File getOutputFile(){
File root = new File(this.getExternalFilesDir(null),"My PDF Folder");
boolean isFolderCreated = true;
if (!root.exists()){
isFolderCreated = root.mkdir();
}
if (isFolderCreated) {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
String imageFileName = "PDF_" + timeStamp;
return new File(root, imageFileName + ".pdf");
}
else {
Toast.makeText(this, "Folder is not created", Toast.LENGTH_SHORT).show();
return null;
}
}
Here images is the ArrayList of the images with path.
you can set the image like this way...
Bitmap bt=Bitmap.createScaledBitmap(btm, 200, 200, false);
bt.compress(Bitmap.CompressFormat.PNG,100, bos);
I was create image on the canvas using colorfilter
This my code
int color = mPaint.getColor();
f = new LightingColorFilter(color, 1);
mPaint.setColorFilter(f);
myBmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon10);
canvas.drawBitmap(myBmp, 20, 20, mPaint);
canvas.save();
canvas.restore();`
and then,I want to save it to sdcard
OutputStream outStream = null;
File file = new File(extStorageDirectory, "er.PNG");
try {
outStream = new FileOutputStream(file);
myBmp.compress(Bitmap.CompressFormat.PNG, 85, outStream);
outStream.flush();
outStream.close();
Toast.makeText(Draw.this, "Saved", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(Draw.this, e.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(Draw.this, e.toString(), Toast.LENGTH_LONG).show();
}
It's Work But have Problem,My picture on sd is old picture(not filter)
can I fix this problem??,Thank
You need draw into Bitmap. Try below:
int color = mPaint.getColor();
f = new LightingColorFilter(color, 1);
mPaint.setColorFilter(f);
Bitmap outBitmap = Bitmap.Create(myBmp.getWidth(),myBmp.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outBitmap);
canvas.drawBitmap(myBmp,20,20,mPaint);
And than you can "save it"(outBitmap) to SD card.
If you're open Bitmap with BitmapFactory you'd get immutable bitmap, and can't draw on it.
That's why you need to create temp. Bitmap, connect Canvas for draw, Drawing and can saving.