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.
Related
I want to save an image to SECONDARY_STORAGE (Not saved to phone memory, which android understands is SdCard ), this is code:
In the manifests file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
Java code:
public void Clickdownload(View v) {
Bitmap myImage = GetImageBitmapFromUrl();
String path = System.getenv("SECONDARY_STORAGE") + "/";
OutputStream out = null;
try {
File file = new File(path, "nameImage.jpg");
out = new FileOutputStream(file);
myImage.compress(Bitmap.CompressFormat.JPEG, 85, out);
out.flush();
out.close();
} catch (Exception e) {
}
}
When I run with Debug I get this error
Error at:
myImage.compress(Bitmap.CompressFormat.JPEG, 85, out);
and it didn't save.
This is the code for reading image from sd card :
String path =System.getenv("SECONDARY_STORAGE")+"/myImage/";
File imgFile = new File(path);
if(imgFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView imageView=(ImageView)findViewById(R.id.imageView);
imageView.setImageBitmap(myBitmap);
}
When I use:
String path = Environment.getExternalStorageDirectory().toString();
I can save an image to the phone memory.
I can save images from my camera and copy images from my computer on my SdCard, so I do not think this is a Read only SdCard. I tried with many types SdCards but it does not work.
I am using android 5.1.1.
I have a bitmap file which i need to upload to my php server but as the file is very large I decided to resize it and save it. Later on I try to read it back to display resized image. But this time I am not getting the same image
Below is code for writing image and returning File
public static File savebitmap(Bitmap bmp) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "testimage.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
return f;
}
Below is code for reading and displaying
File file=ImageUtil.savebitmap(this.bitmap);
this.imgChoosenImage.setImageURI(Uri.parse(file.getAbsolutePath()));
Please tell me what exactly is going wrong here
first check the images are saved in ur path as defined, and Make sure ur giving correct path for retriving image.
I have used this below code for saving imge in gallery
String iconsStoragePath = Environment.getExternalStorageDirectory()+ File.separator;
File sdIconStorageDir = new File(iconsStoragePath);
//create storage directories, if they don't exist
sdIconStorageDir.mkdirs();
try {
String filePath = null;
filePath = Environment.getExternalStorageDirectory() + File.separator + "testimage" + ".jpg";
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
bmp.compress(Bitmap.CompressFormat.JPG, 100, bos);
bos.flush();
bos.close();
} catch (IOException e) {
Log.w("TAG", "Error saving image file: " + e.getMessage());
Toast.makeText(getApplicationContext(), "Failed to Create folder",
Toast.LENGTH_SHORT).show();
}
For bitmap display in imageview :
File imgFile = new File("/sdcard/Images/testimage.jpg");
//Here File file = ur file path
if(imgFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
Permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
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();
}
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();
Actually I have retrieved image saved on facebook but i am not getting how to save image to my emulator and get the path location of saved image as i have to save the path in sqlite.
In most of the answer in stackoverflow,it is only describing about saving the image but not abut retrieving path
imageURL = "http://graph.facebook.com/"+id+"/picture?type=small";
bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent());
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File file = new File(extStorageDirectory, fileName+".jpg");
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 75, fos);
fos.flush();
fos.close();
And i have even updated my manifest file
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
your file object already hold that information.
Get it this way:
file.getAbsolutePath()
Use this snippet hope this will solve your issue
MediaStore.Images.Media.insertImage(getContentResolver(), bitmapimage,
barcodeNumber + ".jpg Card Image", barcodeNumber
+ ".jpg Card Image");