【问题标题】:MediaScannerConnection scanFile scanner is crashingMediaScannerConnection scanFile 扫描仪正在崩溃
【发布时间】:2010-09-27 01:57:48
【问题描述】:

当我运行以下代码行时:

MediaScannerConnection scanner = new MediaScannerConnection( this, null );
    scanner.connect();
    scanner.scanFile( szFinalFileName, null ); //<---crash here

我在控制台窗口中遇到了崩溃和这条消息:

09-26 14:47:44.074: ERROR/MediaScannerService(10288): Failed to delete file /data/data/com.android.providers.media/pause_scan

有没有人知道为什么这不起作用以及我可以做些什么来解决它?

提前致谢:-)

【问题讨论】:

    标签: android


    【解决方案1】:

    另一种方法是广播意图并让系统处理它。

    例如

    public void scanFile(File downloadedFile){
        Uri contentUri = Uri.fromFile(downloadedFile);
        Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
        mediaScanIntent.setData(contentUri);
        mContext.sendBroadcast(mediaScanIntent);
    }
    

    【讨论】:

      【解决方案2】:

      我想通了。

      代码如下:

      final String szFile = szFinalFileName;
      
                  m_pScanner = new MediaScannerConnection(this,
                          new MediaScannerConnectionClient() {
                              public void onMediaScannerConnected() {
                                  m_pScanner.scanFile(szFile, null /*mimeType*/);
                              }
      
                              public void onScanCompleted(String path, Uri uri) {
                                  if (path.equals(szFile)) {
                                      MugMashView.this.runOnUiThread(new Runnable() {
                                          public void run() {
                                              Toast
                                                  .makeText(getApplicationContext(),
                                                      "Image now available in Home > Pictures",
                                                      Toast.LENGTH_SHORT)
                                                  .show();
                                          }
                                      });
                                      m_pScanner.disconnect();
                                  }
                              }
      
                      });
                  m_pScanner.connect();
      

      【讨论】:

        猜你喜欢
        • 2013-04-26
        • 1970-01-01
        • 1970-01-01
        • 2016-05-30
        • 1970-01-01
        • 1970-01-01
        • 2011-06-06
        • 2020-06-02
        • 1970-01-01
        相关资源
        最近更新 更多