I am saving a Bitmap (bitmap) to a file as follows:
String fileName = "image.jpg";
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo = openFileOutput(fileName,
Context.MODE_PRIVATE);
fo.write(bytes.toByteArray());
fo.close();
} catch (Exception e) {
e.printStackTrace();
}
How to access the saved image as a file? Thanks.
Try this.
String fileName = "image.jpg";
try {
String path = Environment.getExternalStorageDirectory().toString(); //path where the file will be stored.
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File file = new File(path,fileName); // the File to save , append
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.close();
} catch (Exception e) {
e.printStackTrace();
}
don't forget to add Storage Permission and get the permission on runtime as well.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Related
How to write image in phone directory. Directory is created but i could not write image in this folder. Here is my code .please check if i am doing something wrong thanks in advance.
Bitmap bitmap;
String directory = new_path; // I am getting path here of image like sdcard/0/emulated/image.jpg
String folder_name = "abc"
bitmap = BitmapFactory.decodeFile(directory);
iv_6.setImageBitmap(bitmap); // image displayed here but not saving in directory.
try {
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator +folder_name);
f.mkdirs();
FileOutputStream fo = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, fo);
fo.close();
}catch (Exception e)
{
e.printStackTrace();
}
public void savePng(Bitmap bitmap, String filePath) {
try {
File temp = new File(filePath);
FileOutputStream os = new FileOutputStream(temp + ".png");
bitmap.compress(Bitmap.CompressFormat.PNG, 50, os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
I,m trying to capture a snapshot of an android activity, but unable to save it in gallery.
the screenshot turns black
I'm also trying to convert this snapshot into a pdf file..Plz help..
This is my code..
public static void saveImage(Bitmap bitmap) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 60, bytes);
File f = new File("sdcard/camera_app/test.png");
//File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.png");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
To register an image into the gallery:
MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);
File f = new File(MainActivity.appContext.getFilesDir(), "Captured.jpg");
f.delete();
//Create new file
FileOutputStream fos = MainActivity.appContext.openFileOutput("Captured.jpg", Context
.MODE_WORLD_WRITEABLE);
//FileOutputStream out = new FileOutputStream(file);
fos.close();
//Get reference to the file
filezz = new File(MainActivity.appContext.getFilesDir(), "Captured.jpg");
as MODE_WORLD_WRITEABLE is deprecated and i don't want to go with that code so
can i get the replcement of above code ?
Replace Context.MODE_WORLD_WRITEABLE with 0 or Context.MODE_PRIVATE.
If you are doing this from bitmap then use below code
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(), "Captured.jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
have imagview want to save it to memory here is my code :
View content = findViewById(R.id.full_image_view);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try {
root.createNewFile();
FileOutputStream ostream = new FileOutputStream(root);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
after saving nothing happens and no image is exist ?
Try this..
Change root.createNewFile(); to cachePath.createNewFile();
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try {
cachePath.createNewFile();
FileOutputStream ostream = new FileOutputStream(cachePath);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
EDIT:
FileOutputStream ostream = new FileOutputStream(cachePath);
Use this function to save in SD card:
private void SaveIamge(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
and add in manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1 - you need an appropriate permission in amnifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2- out.flush() check the out is not null..
3 -
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/yourforlderName";
File dir = new File(file_path);
if(!dir.exists())
dir.mkdirs();
File file = new File(dir, "yourforlderName" + image + ".png");
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
if you don't create directory in your sdcard then how to store images in sdcard of specific location?
so please check this...i hope its useful to you.
To get bitmap from imageview:
imageview.buildDrawingCache();
Bitmap bm=imageview.getDrawingCache();
To save it in a file:
OutputStream fOut = null;
Uri outputFileUri;
try {
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "folder_name" + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
fOut = new FileOutputStream(sdImageMainDirectory);
} catch (Exception e) {
Toast.makeText(this, "Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
}
try {
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
}
add in manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
How about using following snippet ?
Drawable d = imageView.getBackground();
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
File file = new File(Environment.getExternalStorageDirectory(), "fileName.ext");
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
Having trouble with File coding. This code basically save the bitmap file into android gallery.
Java.IO.File MyDirectory = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures), "MyDirectory");
Java.IO.File MyFile= new Java.IO.File(MyDirectory , String.Format("Photo{0}.jpg", Guid.NewGuid()));
Bitmap photo;
Bundle extras = data.Extras;
photo = (Bitmap)extras.Get("data")
How to save the photo (Bitmap) into MyFolder android gallery?
I have tried this to save the photo:
Java.IO.FileOutputStream outFile = new Java.IO.FileOutputStream(MyFile);
photo.Compress(Bitmap.CompressFormat.Png, 100, outFile);
Error I received is when the photo is compress..
error: Cannot convert from Java.IO.FileOutputStream to System.IO.Stream.
Sorry, I am very newbie in File coding. Any helps or solutions are appreciated.
Here is my code.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
public static void saveBitmap(Context context, Bitmap bitmap) {
String env = Environment.getExternalStorageDirectory().getPath();
String path = env + "/test.png";
try {
File f = new File(path);
FileOutputStream fileOut = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOut);
try {
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bitmap.recycle();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This should work
var file = new FileStream(fname, FileMode.Create, FileAccess.Write, FileShare.None);
photo.Compress(Bitmap.CompressFormat.Jpeg, 85, file);
I use alternative way to compress my bitmap (using MemoryStream) and here is my code.
using(Bitmap bitmap = BitmapFactory.DecodeFile(myFileString))
{
MemoryStream stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);
myWebService.functionSave(stream.ToArray());
}
//myWebservice function parameter.
functionSave(byte[] fileStream)
{
//... save your bitmap code using byte[]
}