When I save a bitmap with this function, I get an unreadable file, bigger than the original image(using Root explorer on my phone) what is wrong?
The bitmap is set by the user using the stock image picker.
Here is the call:
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
saveToInternalSorage(bitmap);
Here the saveToInternalSorage method (from here Saving and Reading Bitmaps/Images from Internal memory in Android )
private String saveToInternalSorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
String filename = randomString();
System.out.println(filename);
File mypath=new File(directory,filename);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return directory.getAbsolutePath();
}
Related
I know there are many post on Environment.getExternalStorageDirectory() for finding out folder for saving files. When I used it, I am getting
/storage/emulated/0/myfolder
as folder path and getting below error when try to use for saving file.
w/System.err: java.io.FileNotFoundException: /storage/emulated/0/myfolder/myfile (No such file or directory)
How to resolve this issue? I would like to use Internal Memory for storing Image & video in the same fashion as other app does.
Thanks in advance for your help.
Update : As requested Code for saving Image that is been created.
public String saveTofolder(Bitmap bitmap, String filename) {
String stored = null;
File sdcard = Environment.getExternalStorageDirectory();
File folder = new File(sdcard.getAbsoluteFile(), "myfolder");
if (!folder.exists()) folder.mkdirs();
File file = new File(folder.getAbsoluteFile(), filename);
if (!file.exists()) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
try {
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.flush();
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
stored = "success";
}
return stored;
}
I want to load an image from a file path which I have already defined, but don't want to instantiate another file object since I have already defined the path when saving the image.
I have tried retrieving the image with:
Picasso.with(this).load(filename).into(image_tv);
This is my code for saving the image;
Bitmap bitMapImg;
void saveImage() {
File filename;
try {
String path =
Environment.getExternalStorageDirectory().toString();
new File(path + "/folder/subfolder").mkdirs();
filename = new
File(path+"/folder/subfolder/image.jpg");
FileOutputStream out = new FileOutputStream(filename);
bitMapImg.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Use this
Picasso.with(context).load(Uri.parse("file://" + yourFilePath).into(imageView);
I'm trying to save bitmap to gallery to open it again from ImagePicker intent.
The problem is - if I'm saving it on external storage, it will not work on devices without it, and when I'm writing it on internal storage - it won't shown in gallery, so i can not pick that image.
Tried so many ways of resolving it, but no success this far.
I mean, is it possible to save bitmap in internal storage and open it from activity, which called to choose image from gallery?
edit: added code
private String saveToInternalStorage(Bitmap bitmapImage, String filename){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,filename + ".jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return directory.getAbsolutePath();
}
With that procedure it's seems like saving bitmap is ok, but i can't find that file in gallery.
I wanted to create a folder and store image into the phone's internal storage. I tried using the code below to download an image from the url. It managed to load the image into imageView but unable to store and create folder in the internal storage. Plus I got no warning or error message. Any idea what's wrong code below?
Bitmap bm = null;
InputStream in;
try{
in = new java.net.URL("http://blogs.computerworld.com/sites/computerworld.com/files/u177/google-nexus-4.jpg").openStream();
bm = BitmapFactory.decodeStream(new PatchInputStream(in));
File mydir = this.getDir("mydir", Context.MODE_PRIVATE);
mydir.mkdirs();
File fileWithinMyDir = new File(mydir, "myfile");
FileOutputStream out = new FileOutputStream(fileWithinMyDir);
bm.compress(Bitmap.CompressFormat.JPEG, 85, out);
}
catch(Exception e1){
e1.printStackTrace();
}
ImageView img = (ImageView) findViewById(R.id.image_display);
img.setImageBitmap(bm);
once you have your bitmap bm:
FileOutputStream fos;
try {
fos = openFileOutput("file_Name", Context.MODE_PRIVATE);
bm.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
}catch (Exception e){
...
}
will save your file to internal storage
use the following:
String path=Environment.getExternalStorageDirectory()
.toString() + File.separator
to get the directory and save the image.
I want to store bitmap image on internal storage (not external storage). I have written this code but it seems something has problem. Because when i download image from DDMS, I can't open it.
public String writeFileToInternalStorage(Context context, Bitmap outputImage) {
String fileName = Long.toString(System.currentTimeMillis()) + ".png";
try {
OutputStreamWriter osw = new OutputStreamWriter(context.openFileOutput(fileName, Context.MODE_PRIVATE));
osw.write(outputImage.toString());
Log.i(TAG, "Image stored at: " + fileName);
} catch (Exception e) {
Log.w(TAG, e.toString());
fileName = null;
}
return fileName;
}
outputImage.toString() is not the image :) the contant you put on the file is not the binary data, but some string!
A way to do it is this:
public String writeFileToInternalStorage(Context context, Bitmap outputImage) {
String fileName = Long.toString(System.currentTimeMillis()) + ".png";
final FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
outputImage.compress(CompressFormat.PNG, 90, fos);
}
I coded directly into the browser, it is possible to have some syntax errors, but the code should work.
The problem is that you use .toString() instead of compressing the Bitmap into a FileOutputStream:
FileOutputStream out = new FileOutputStream(filename);
outputImage.compress(Bitmap.CompressFormat.PNG, 90, out);
The internal storage can be retrieved via the Context, too.
File cacheDir = context.getCacheDir();