问题在Android的在MediaStore存储图像图像、问题、Android、MediaStore

2023-09-06 19:05:27 作者:逼大无脑@

我已经写了code块通过java程序插入到Android设备图片库新形象,请在下面找到code,

I have written a block of code to insert new image to Android device Image gallery through java program, please find the code below,

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) {                          

图像是完全存储,但问题是我无法立即查看图像。我需要关闭,并把它在设备上查看图像。谁能普莱舍帮助我解决这个问题?

Image is stored perfectly, but the problem is i could not view the image immediately. I need to switch off and turn it on the device to view the image. Can anyone plese help me to solve this problem?

编辑:你好Aleadam,感谢您的重播,请检查我的code以下

Hi Aleadam, Thanks for the replay, pls check my code below

 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相连。我是否错过了一些东西。

It not working for me, it not even connected with the MediaScanner. whether i missed out something.

谢谢Rajapandian

Thanks Rajapandian

推荐答案

媒体扫描仪需要重新扫描存储,以显示在画廊的形象。

Media Scanner needs to rescan the storage in order to show the image in the Gallery.

认准 MediaScannerConnection API来做到这一点。

Look for the MediaScannerConnection API to make it happen.

MediaScannerConnection为应用程序提供通过新创建的或下载的媒体文件的媒体扫描仪服务的一种方式。媒体扫描服务将读取文件元数据和文件添加到媒体内容提供商。该MediaScannerConnectionClient提供了媒体扫描服务,为新扫描的文件返回的URI MediaScannerConnection类的客户端的接口。

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.