问题
i'm using cropping functionality in my android application and it works fine but i want to save cropped image to my SD Card
. For that what steps needs to be followed?
回答1:
If the Bitmap
you cropped is called yourBitmap
File sdcard = Environment.getExternalStorageDirectory();
File f = new File (sdcard, "filename.png");
FileOutputStream out = new FileOutputStream(f);
yourBitmap.compress(Bitmap.CompressFormat.PNG, 90, out)
回答2:
Try this :
public void saveBitmap(Bitmap bmp)
{
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/NewFolder";
File dir = new File(file_path);
if(!dir.exists)
dir.mkdirs();
File file = new File(dir, "myImage.png");
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
}
Following permission is required in Manifest
file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Thanks.
回答3:
first for get image store in sdcard:-
public static String storeImage(Bitmap bitmap, String filename) {
String stored = null;
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, filename + ".png");
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
stored = "success";
} catch (Exception e) {
e.printStackTrace();
}
return stored;
}
second get image from sdcard:-
public static File getImage(String imagename) {
File mediaImage = null;
try {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root);
if (!myDir.exists())
return null;
mediaImage = new File(myDir.getPath() + imagename);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mediaImage;
}
3 Convert image from sdcard to bitmap
File file = CommonUtils.getImage("/CoverPic.png");
String path = file.getAbsolutePath();
if (path != null)
picture = BitmapFactory.decodeFile(path);
来源:https://stackoverflow.com/questions/14135764/store-image-to-sd-card