gpt4 book ai didi

java - 如何将图像保存到用户的手机上?然后加载它?

转载 作者:行者123 更新时间:2023-12-01 16:52:43 27 4
gpt4 key购买 nike

我遵循了很多关于如何将图像保存到手机并重新加载图像的指南,最后得出以下结论:在视频中它有效,但由于某种原因它对我不起作用。

可能有更好的版本吗?我稍后如何读取此内容,以便将其与 Picasso 一起粘贴到另一个 ImageView 中?

(更新:Logcat 说:Logcat 说:java.io.FileNotFoundException:/storage/emulated/0/LiFit/profileimage.jpg(没有这样的文件或目录),但我明确使用 mkdir 命令创建此文件夹那么为什么它不起作用?)

BitmapDrawable drawable = (BitmapDrawable) profilBild.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File(sdCard.getAbsolutePath()+ "/LiFit");
directory.mkdir();
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(directory, fileName);
Toast.makeText(getActivity(), "Pic saved", Toast.LENGTH_LONG).show();
try {
outputStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
try {
outputStream.flush();
outputStream.close();
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(outFile));
getContext().sendBroadcast(intent);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}

最佳答案

显然您需要将应用程序设置中的权限更改为......

关于java - 如何将图像保存到用户的手机上?然后加载它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61658584/

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