gpt4 book ai didi

android 将位图正确保存为 jpeg 但不显示在图片库中

转载 作者:行者123 更新时间:2023-11-29 21:05:14 25 4
gpt4 key购买 nike

大家好,我正在将我的 imageviews 位图保存为 jpeg/png 格式,但它们没有列在我的图片库中(android 默认库)

这是我的方法:

    public void save_Image() {

String path = Environment.getExternalStorageDirectory().toString()
+ "/ImageFrame";
Log.d("Files", "Path: " + path);

File f = new File(path);

File file[] = f.listFiles();

Log.d("Files", "Size: " + file.length);
for (int i = 0; i < file.length; i++) {
Log.d("Files", "FileName:" + file[i].getName());
}

// BitmapDrawable drawable = (BitmapDrawable) img_large.getDrawable();
// Bitmap bitmap = drawable.getBitmap();

RelativeLayout frm_l = (RelativeLayout) findViewById(R.id.frm_l);

frm_l.setDrawingCacheEnabled(true);
Bitmap bitmap = frm_l.getDrawingCache();

File image = new File(sdCardDirectory + "/ImageFrame/", "HDWallpaper "
+ file.length + ".png");

//File image = new File(sdCardDirectory + "/ImageFrame/", "HDWallpaper "
// + file.length + ".jpeg");
boolean success = false;
FileOutputStream outStream;
try {

outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
// bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);

/* 100 to keep full quality of the image */

outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

if (success) {
System.out.println("this is path where stored.."
+ Environment.getExternalStorageDirectory()
.getAbsolutePath());
Toast.makeText(getApplicationContext(),

"Saved Successfully", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image sending", Toast.LENGTH_LONG).show();
}

}

此方法适用于我并保存位图但它没有在我的图像库中列出我应该怎么做才能克服这个问题?

最佳答案

您需要“告诉”画廊您添加了一张图片。您可以通过 MediaScannerConnection#scanFile 执行此操作:

MediaScannerConnection.scanFile(this,
new String[] { "YourFilePathHere" }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
//now visible in gallery
}
}
);

取自this answer .您可能想查看该帖子的其他答案,因为它们也可以解决您的问题。

关于android 将位图正确保存为 jpeg 但不显示在图片库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24804665/

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