Android studio - How to save an image to SECONDARY_STORAGE - android

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.

Related

Android get image in secondary storage

I am trying to get an image from phone's secondary storage.
public void downloadImage(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 why I get this error and it didn't receive the file also.
Error at:
myImage.compress(Bitmap.CompressFormat.JPEG, 85, out);
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>
String photoPath = Environment.getExternalStorageDirectory()+"/photo.jpg";
and get bitmap by using code below.
The photo.jpg is the the name of the image you want to read.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
You can also use picasso image library
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.with(context).load(new File(...)).into(imageView3);

Saving a bitmap file in android and reading back to display in Imageview

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" />

Android IOException when compressing a Bitmap

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.

Unable to store Image into internal storage directory

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.

Save an image and get the saved location of image on android emulator

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");

Categories

Resources