gpt4 book ai didi

android:如何将图像添加到相册

转载 作者:太空狗 更新时间:2023-10-29 15:36:50 26 4
gpt4 key购买 nike

任何人都可以共享代码(或向我指出 Android 示例代码)来帮助我将图像添加到媒体商店(图库)中的相册。

在我的应用程序中,我从我们的服务器下载图像,并使用相机拍摄新图像(通过 Intent)。

我想将这些图片整理到一个应用特定的相册中,类似于 Facebook(和其他应用)应用所做的那样,让相关图片整齐有序。

我不久前根据 Media Store API 文档调查过这个问题,但它对我不起作用....所以需要一些帮助。

谢谢

最佳答案

这是一种用于将图像存储在公共(public)图片文件夹中的方法,其中包含一个自定义应用程序文件夹。

public void saveImageToExternal(String imgName, Bitmap bm) throws IOException {
//Create Path to save Image
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES+appFolder); //Creates app specific folder
path.mkdirs();
File imageFile = new File(path, imgName+".png"); // Imagename.png
FileOutputStream out = new FileOutputStream(imageFile);
try{
bm.compress(Bitmap.CompressFormat.PNG, 100, out); // Compress Image
out.flush();
out.close();

// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(context,new String[] { imageFile.getAbsolutePath() }, null,new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
} catch(Exception e) {
throw new IOException();
}
}

关于android:如何将图像添加到相册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11983654/

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