In my Android project I am trying to save a bitmap to the SD card at a given location.
When I run this, I get an IOException every time, saying permission denied. This is on creating the FileOutputStream.
This leads me to believe that I am missing permissions, but I include READ and WRITE_EXTERNAL_STORAGE. They are declared before my uses-sdk block and are outside the application block. I also read/write text to a file elsewhere in my application and that works just fine. My current code was modified to match several of the solutions I found online, but I have not been able to successfully save a bitmap file.
If anyone could point out my error I would be extremely thankful.
ImageView imageView = (ImageView) findViewById(R.id.new_pnm_pic);
Bitmap bm = (Bitmap)data.getExtras().get("data");
imageView.setImageBitmap(bm);
String outPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Recruits";
try {
File d = new File(outPath);
if (!d.exists())
d.mkdirs();
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd-kk-mm-ss");
String currentTime = sd.format(new Date());
File file = new File(outPath+'/'+currentTime+".jpg");
FileOutputStream fOut = new FileOutputStream(file.getPath());
bm.compress(Bitmap.CompressFormat.JPEG, 50, fOut);
fOut.flush();
fOut.close();
}
catch(IOException ioe){
Toast.makeText(this, "Nice try but didn't work", Toast.LENGTH_SHORT).show();
}
Change This
File file = new File(outPath+'/'+currentTime+".jpg");
To
File file = new File(d+'/'+currentTime+".jpg");
And Add Permission to Manifest File:-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Edit
Before we start saving the image to sd card ,we need to check whether the sd card is mounted or not.
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 was looking for a way to save a bitmap to a folder (with my app's name), in such a way the default gallery will recognize it.
So I managed to save the image, but I can't see it either from the gallery or my PC (using explorer)
this is my code:
// Save bitmap to internal memory
private void savePhoto(Bitmap bmp){
String appName = "myApp";
String file_path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ File.separator + appName;
File dir = new File(file_path);
if(!dir.exists())
dir.mkdirs();
// Image file
File file = new File(dir, "IMG" + "_" + System.currentTimeMillis() + ".jpg");
FileOutputStream out = null;
try
{
out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
out = null;
} catch (Exception e)
{
e.printStackTrace();
}
}
I can see it using some File Manager installed on my galaxy, but it doesn't help a lot.
I know I should inform the gallery using a media scanner (which is another bridge I need to cross) but may someone help me understand way I can't even find the file..should I change its visibility somehow?
and one more thing: I read at another question regarding the issue, that I should add metadata to the image/folder so that the gallery would show it. Is it necessary?
Many thanks!!!
Apparently it did have something to do with the media scanner, I added this line
MediaScannerConnection.scanFile(this, new String[] { file.getAbsolutePath() }, null, null);
and now it works.
from the documentation:
MediaScannerConnection provides a way for applications to pass a newly created or downloaded media file to the media scanner service. The media scanner service will read metadata from the file and add the file to the media content provider.
try to replace this getExternalStoragePublicDirectory(....)
with this one getExternalStorageDirectory();
In my application, I have a view with customer details, I want to save that view as image or PDF to SD card then print the view through third party application (Otherwise print that view directly through printer is possible).
I have no idea of how to save view as image or PDF. Can any one know please help me to solve this problem?
Add permission in the manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Use the code below
LinearLayout content = findViewById(R.id.rlid);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File file,f;
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
{
file =new File(android.os.Environment.getExternalStorageDirectory(),"TTImages_cache");
if(!file.exists())
{
file.mkdirs();
}
f = new File(file.getAbsolutePath()+file.seperator+ "filename"+".png");
}
FileOutputStream ostream = new FileOutputStream(f);
bitmap.compress(CompressFormat.PNG, 10, ostream);
ostream.close();
}
catch (Exception e){
e.printStackTrace();
}
Above code work properly but, image override because of same name so, for avoid this problem add below line:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss");
Date now = new Date();
String fileName = formatter.format(now);
f = new File(file.getAbsolutePath()+file.separator+ "image_"+fileName+".png");
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");
I am trying to write a jpg image to the external SD card. However, I am getting System.err FileNotFoundException: /mnt/sdcard/test.images/temp/savedImage (no such file or directory). Creating the directory also seems to fail and gives a false in LogCat and I also cannot see the folder when looking on my SD card.
My code is as follows:
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File folder = new File(Environment.getExternalStorageDirectory() + "/test.images/temp");
try {
if(!folder.exists()){
boolean dir = new File(Environment.getExternalStorageDirectory() + "/test.images/temp").mkdir();
Log.v("creating directory", Boolean.toString(dir));
}
File imageOutputFile = new File(Environment.getExternalStorageDirectory() + "/test.images/temp", "savedImage");
FileOutputStream fos = new FileOutputStream(imageOutputFile);
Image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
I have permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
in the manifest and have cleaned and rebuilt.
Use mkdirs() instead of mkdir().
Guido posted a solution that works for me in the comment. I am going to repeat it just to make sure it can be an answer.