简体   繁体   中英

Storing Image on Android SDCard & Path URL in SharedPreferences

I'm currently storing the photos like so in the SharedPreferences after it's captured using ACTION_IMAGE_CAPTURE intent:

 if (requestCode == REQUEST_IMAGE_CAPTURE_TWO && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            mImageTwo.setImageBitmap(imageBitmap);


            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] b = baos.toByteArray();

            String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

            SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
            SharedPreferences.Editor edit = shre.edit();
            edit.putString("image_two", encodedImage);
            edit.commit();
        }

Instead I'd like to store this image to the SD card and the url to the file path in shared preferences, so that I can load the images using the filepath and I'm able to attach these photos with the ACTION_SEND intent. It appears I can't do that with the way I'm currently storing the images.

This will return file name of saved image in sdcard so now you can save it in shared preferences or you can modify as you need.

    public static String saveImage(Bitmap imageBitmap) {

    File sdCardDirectory = Environment.getExternalStorageDirectory();
    File dir = new File(sdCardDirectory + "/Folder_Name");
    dir.mkdir();

    String fileName = "image" + System.currentTimeMillis() + ".jpeg";
    File image = new File(dir, fileName);
    fileName = image.getPath();

    FileOutputStream outStream;
    try {

        outStream = new FileOutputStream(image);
        imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);

        outStream.flush();
        outStream.close();
    } catch (FileNotFoundException e) {
        fileName = Constants.ERROR_IMAGE_SAVING;
        e.printStackTrace();
    } catch (IOException e) {
        fileName = Constants.ERROR_IMAGE_SAVING;
        e.printStackTrace();
    }

    return fileName;
}

There is solution:

public static String saveEncodedImage(String folderName, String imgFileName, String encodedImage) {
    //check sdcard is available
    String sdStatus = Environment.getExternalStorageState();
    if(!sdStatus.equals(Environment.MEDIA_MOUNTED)){
        // sdcard not avaliable
        return "";
    }
    String urlToPath = Environment.getExternalStorageDirectory().getAbsolutePath()
            + File.separator
            + folderName;
    File folder = new File(urlToPath);
    if (!folder.exists()){
        folder.mkdir();
    }
    urlToPath = urlToPath + File.separator + imgFileName;
    File imgFile = new File(urlToPath);
    try {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(imgFile));
        bufferedWriter.write(encodedImage);
        bufferedWriter.flush();
        bufferedWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
        return "";
    }
    return urlToPath;
}

You can use it like this:

if (requestCode == REQUEST_IMAGE_CAPTURE_TWO && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            mImageTwo.setImageBitmap(imageBitmap);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] b = baos.toByteArray();

            String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
            //save to sdcard
            String urlToImage = saveEncodedImage("images","image_two",encodedImage);
            SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
            SharedPreferences.Editor edit = shre.edit();
            edit.putString("image_two", urlToImage);
            edit.commit();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM