gpt4 book ai didi

android - 在 Android 的 MediaStore 中存储图像的问题

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

我写了一段代码,通过java程序将新图片插入到Android设备图片库中,请在下面找到代码,

ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "title");
values.put(Images.Media.BUCKET_ID, "test");
values.put(Images.Media.DESCRIPTION, "test Image taken");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(uri);
receivedBitmap.compress(Bitmap.CompressFormat.JPEG, 70, outstream);
outstream.close();
alertDialog.showMessage("Image Stored Successfully", "Media");
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
} catch (Exception e) {

图像保存完好,但问题是我无法立即查看图像。我需要关闭并打开设备才能查看图像。谁能帮我解决这个问题?

编辑:嗨 Aleadam,感谢重播,请检查下面的代码

 m_pScanner = new MediaScannerConnection(this,
new MediaScannerConnectionClient() {
public void onMediaScannerConnected() {
m_pScanner.scanFile(returnUrl, null /*mimeType*/);
}

public void onScanCompleted(String path, Uri uri) {
if (path.equals(returnUrl)) {
ImageViewActivity.this.runOnUiThread(new Runnable() {
public void run() {

}
});
m_pScanner.disconnect();
}
}

});
m_pScanner.connect();

它对我不起作用,它甚至没有与 MediaScanner 连接。我是否错过了什么。

谢谢拉贾潘迪安

最佳答案

媒体扫描器需要重新扫描存储才能在图库中显示图像。

寻找 MediaScannerConnection实现这一目标的 API。

MediaScannerConnection provides a way for applications to pass a newly created or downloaded media file to the media scanner service. The media scanner service will read metadata from the file and add the file to the media content provider. The MediaScannerConnectionClient provides an interface for the media scanner service to return the Uri for a newly scanned file to the client of the MediaScannerConnection class.

关于android - 在 Android 的 MediaStore 中存储图像的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6148195/

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