如何将图像保存到用户的 phone?然后加载它?

How can I save an Image to the phone of the user ? and later load it?

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

是否有更好的版本?我以后如何读取它以便我可以将它与 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();
}

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