I created canvas that can be used to draw some shapes on it.
How can I save its content to PNG file on user's SD card?
check out this link this link
In this link you can find the method
void saveImage() {
try {
String filename = Environment.getExternalStorageDirectory().toString();
File f = new File(filename ,"myImage.png");
f.createNewFile();
System.out.println("file created " + f.toString());
FileOutputStream out = new FileOutputStream(f);
Bitmap bitmap = showImage(urlStr);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
}
which is used to save the image that you got into a bitmap. and check this link for getting bitmap from canvas
hope this helps you.
Happy coding
Canvas is just a means to draw to the Bitmap.
You should have created Canvas with new Canvas(myBitmap);. So when you draw on the Canvas, it draws to your bitmap.
so using myBitmap Do the following (code here:
String fileName = Environment.getExternalStorageDirectory() + "/test.png";
OutputStream stream = new FileOutputStream(fileName);
/* Write bitmap to file using JPEG or PNG and 80% quality hint for JPEG. */
myBitmap.compress(CompressFormat.PNG, 80, stream);
stream.close();
Related
I have a set of icons stored in the drawable folder.
Based on users selection I'll be posting these icons to the backend.
When I try to retrieve the path for these drawables, I get the following error
HTTP FAILED: java.io.FileNotFoundException: (No such file or directory)
This is how I am getting the path
public String getURLForResource (int resourceId) {
return Uri.parse("android.resource://"+ BuildConfig.APPLICATION_ID+"/" +resourceId).toString();
}
File fileToUpload = new File(getURLForResource(R.drawable.icon));
I have referred the following link says that it's not possible to retrieve the path for a drawable.
Any idea on how I can solve this problem?
As solution you can write your drawable to file.
Convert drawable to bitmap
Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.your_drawable);
Save bitmap to file
String path = Environment.getExternalStorageDirectory().toString();
File file = new File(path, "image" + System.currentTimeMillis() + ".jpg");
try (FileOutputStream out = new FileOutputStream(file)) {
bm.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush(); // Not really required
out.close(); // do not forget to close the stream
} catch (IOException e) {
e.printStackTrace();
}
You can use file now.
my bitmap image in the bitmap variable whose name is imageFile how we further give name this bitmap image and store it in my mobile memory
in this picture a function show which receive image file and convert it into bitmap
1- you have to find the path to where you wanna save your image.
2- open an OutputStream object and give it the file path.
3- save your bitmap to the output stream.
4- flush.
5- close.
// Get environment dir
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "MyImage"+".jpg"); // the File to save to
fOut = new FileOutputStream(file);
Bitmap pictureBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), optons); // obtaining the Bitmap
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush(); // Not really required
fOut.close(); // do not forget to close the stream
I am frustrated searching almost every google page for this problem.
I need to save my bitmap to file.
I have used this method several times with no problem at all. But now I am having a problem.
The Bitmap I am saving is a round image with transparency and having .png format which I have placed in res folder. But I am getting black portion in place of transparent portion.
This is the code I am using for saving the bitmap.
void saveImage(Bitmap bmp) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bytes);
finalImg = new File(Environment.getExternalStorageDirectory(),
"emoticon_temp" + ".png");
finalImg.createNewFile();
FileOutputStream fo = new FileOutputStream(finalImg);
fo.write(bytes.toByteArray());
fo.flush();
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I am saving this bitmap as file because I need to share this image and for sharing the image I need the Stream Uri.
If you got any other Idea for sharing a bitmap, please let me know.
I created canvas that can be used to draw some shapes on it.
How can I save its content to PNG file on user's SD card?
check out this link this link
In this link you can find the method
void saveImage() {
try {
String filename = Environment.getExternalStorageDirectory().toString();
File f = new File(filename ,"myImage.png");
f.createNewFile();
System.out.println("file created " + f.toString());
FileOutputStream out = new FileOutputStream(f);
Bitmap bitmap = showImage(urlStr);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
}
which is used to save the image that you got into a bitmap. and check this link for getting bitmap from canvas
hope this helps you.
Happy coding
Canvas is just a means to draw to the Bitmap.
You should have created Canvas with new Canvas(myBitmap);. So when you draw on the Canvas, it draws to your bitmap.
so using myBitmap Do the following (code here:
String fileName = Environment.getExternalStorageDirectory() + "/test.png";
OutputStream stream = new FileOutputStream(fileName);
/* Write bitmap to file using JPEG or PNG and 80% quality hint for JPEG. */
myBitmap.compress(CompressFormat.PNG, 80, stream);
stream.close();
I am trying to use the VuDroid PDF viewer and I need to take the rendered bitmap and store it as a byte[]. Then I need to convert it back into a Bitmap that can be displayed on a view using something like "canvas.drawBitmap(bitmap, 0, 0, paint);".
I have spent many hours trying to access the Bitmap and I might have done it already, but even if I get the byte[] to return something it still wont render as a Bitmap on the canvas.
Could someone please help me here, I must be missing something. Thank you so much.
I believe it is supposed to accessed via...
PDFPage.java .... public Bitmap renderBitmap(int width, int height, RectF pageSliceBounds)
-or-
through Page.java -or- DocumentView.java -or- DecodeService.java
Like I said I have tried all of these and have gotten results I just cannot see where I am going wrong since I cannot render it to see if the Bitmap was called correctly.
Thank you again :)
The doc says the method returns "null if the image could not be decode." You can try:
byte[] image = services.getImageBuffer(1024, 600);
InputStream is = new ByteArrayInputStream(image);
Bitmap bmp = BitmapFactory.decodeStream(is);
I think This will help you:-
Render a byte[] as Bitmap in Android
How does Bitmap.Save(Stream, ImageFormat) format the data?
Copy image with alpha channel to clipboard with custom background color?
if you want to get each pdf page as independent bitmap you should consider that
VuDroid render the pages,
PDFView only display them.
you should use VuDroid functions.
now you can use this example and create your own codes
Example code : for make bitmap from a specific PDF page
view = (ImageView)findViewById(R.id.imageView1);
pdf_conext = new PdfContext();
PdfDocument d = pdf_conext.openDocument(Environment.getExternalStorageDirectory() + "your PDF path");
PdfPage vuPage = d.getPage(1); // choose your page number
RectF rf = new RectF();
rf.bottom = rf.right = (float)1.0;
Bitmap bitmap = vuPage.renderBitmap(60, 60, rf); //define width and height of bitmap
view.setImageBitmap(bitmap);
for writing this bitmap on SDCARD :
try {
File mediaImage = new File(Environment.getExternalStorageDirectory().toString() + "your path for save thumbnail images ");
FileOutputStream out = new FileOutputStream(mediaImage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
for retrieve saved image:
File file = new File(Environment.getExternalStorageDirectory().toString()+ "your path for save thumbnail images ");
String path = file.getAbsolutePath();
if (path != null){
view = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(path), YOUR_X, YOUR_Y, false);
}
Try this code to check whether bitmap is properly generating or not
PdfContext pdf_conext = new PdfContext();
PdfDocument d = (PdfDocument) pdf_conext.openDocument(pdfPath);
PdfPage vuPage = (PdfPage) d.getPage(0);
RectF rf = new RectF();
Bitmap bitmap = vuPage.renderBitmap(1000,600, rf);
File dir1 = new File (root.getAbsolutePath() + "/IMAGES");
dir1.mkdirs();
String fname = "Image-"+ 2 +".jpg";
File file = new File (dir1, fname);
if (file.exists ())
file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}