gpt4 book ai didi

java - 将图像存储在 Android SDCard 上并将路径 URL 存储在 SharedPreferences 中

转载 作者:行者123 更新时间:2023-12-01 11:19:22 25 4
gpt4 key购买 nike

我目前正在使用 ACTION_IMAGE_CAPTURE Intent 捕获照片后将照片存储在 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 Intent 附加这些照片。看来我无法以当前存储图像的方式做到这一点。

最佳答案

这将返回 SD 卡中保存的图像的文件名,因此现在您可以将其保存在共享首选项中,或者您可以根据需要进行修改。

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

关于java - 将图像存储在 Android SDCard 上并将路径 URL 存储在 SharedPreferences 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31442211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com