store image to SD card

左心房为你撑大大i 提交于 2019-12-11 15:26:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!