gpt4 book ai didi

android - 使用新创建的位图更新 android 图片库

转载 作者:太空宇宙 更新时间:2023-11-03 11:29:42 25 4
gpt4 key购买 nike

我正在尝试将图像文件保存到外部存储器。我可以将图片保存到 SD 卡,但它不会显示在 Android 的图库应用程序中。我试过这种方法:

File path = Environment.getExternalStorageDirectory();
File f = new File(path + "/mydirectory/" + imageName + "_" + System.currentTimeMillis() + ".jpg");
FileOutputStream fos = new FileOutputStream(f);
f.mkdirs();
b.compress(CompressFormat.JPEG, 100, fos);
fos.close();

Uri contentUri = Uri.fromFile(f);
Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
mediaScanIntent.setData(contentUri);
getApplicationContext().sendBroadcast(mediaScanIntent);

但它没有出现在图库中。谁能指出我解决这个问题的正确方向?

最佳答案

使用此代码在 android 设备库中保存图像位图

public void savePhoto(Bitmap bmp)
{
imageFileFolder = new File(Environment.getExternalStorageDirectory(),"Rotate");
imageFileFolder.mkdir();
FileOutputStream out = null;
Calendar c = Calendar.getInstance();
String date = fromInt(c.get(Calendar.MONTH))
+ fromInt(c.get(Calendar.DAY_OF_MONTH))
+ fromInt(c.get(Calendar.YEAR))
+ fromInt(c.get(Calendar.HOUR_OF_DAY))
+ fromInt(c.get(Calendar.MINUTE))
+ fromInt(c.get(Calendar.SECOND));
imageFileName = new File(imageFileFolder, date.toString() + ".jpg");
try
{
out = new FileOutputStream(imageFileName);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
scanPhoto(imageFileName.toString());
out = null;
} catch (Exception e)
{
e.printStackTrace();
}
}


public String fromInt(int val)
{
return String.valueOf(val);
}


public void scanPhoto(final String imageFileName)
{
msConn = new MediaScannerConnection(PreviewDemo1.this,new MediaScannerConnectionClient()
{
public void onMediaScannerConnected()
{
msConn.scanFile(imageFileName, null);
Log.i("msClient obj in Photo Utility","connection established");
}
public void onScanCompleted(String path, Uri uri)
{
msConn.disconnect();
Log.i("msClient obj in Photo Utility","scan completed");
}
});
msConn.connect();
}

这里我将图像保存在“Rotate”文件夹中,如果您不希望您可以在 savePhoto 方法中轻松更改它。

关于android - 使用新创建的位图更新 android 图片库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7687723/

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