【发布时间】:2020-08-22 19:06:26
【问题描述】:
我遵循了很多关于如何将图像保存和重新加载到手机的指南并得出了这个结论: 在视频中它可以工作,但由于某种原因它对我不起作用。
有没有更好的版本?以后如何读取此内容,以便将其与 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();
}
【问题讨论】:
-
您要求写权限吗? logcat 中是否有一些错误消息?
-
您是否已授予设备访问存储的权限?
-
还有关于毕加索的问题,加载没有问题local images with picasso
-
Logcat 说:java.io.FileNotFoundException: /storage/emulated/0/LiFit/profileimage.jpg(没有这样的文件或目录)
标签: java android image android-studio fileoutputstream