在Android SDCard上存储图像& SharedPreferences中的路径URL

时间:2015-07-15 22:20:05

标签: java android android-intent

我目前在使用ACTION_IMAGE_CAPTURE意图捕获后在SharedPreferences中存储这样的照片:

 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();
        }

相反,我想将此图像存储到SD卡和共享首选项中文件路径的URL,以便我可以使用文件路径加载图像,并且我可以附加这些照片使用ACTION_SEND意图。看来我不能用我当前存储图像的方式来做到这一点。

2 个答案:

答案 0 :(得分:1)

这将在sdcard中返回已保存图像的文件名,现在您可以将其保存在shared preferences中,也可以根据需要进行修改。

    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;
}

答案 1 :(得分:0)

有解决方案:

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;
}

你可以像这样使用它:

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();
        }