2016-02-11 84 views
0

我有一個問題,此代碼完美工作,但如果在SD卡中的視頻不能被mediaScan擦除。 這是一個錯誤?或者它是一個特殊的許可?ACTION_MEDIA_SCANNER_SCAN_FILE來自外部SD卡Lollipop +

@Override 
public void onVideoSelected(final String uri, String mimeType) { 

    final AlertDialog.Builder builder = new AlertDialog.Builder(TV.this); 
    builder.setTitle("Select"); 
    builder.setItems(new CharSequence[] 
        {"Play Video", "Remove Video"}, 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        switch (which) { 
         case 0: 
          Intent intent = new Intent(TV.this, MainActivity.class); 
          intent.putExtra("url", uri); 
          startActivity(intent); 
          break; 
         case 1: 

          File file = new File(uri); 
          file.delete(); 

          TV.this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(uri)))); 

          break; 
        } 
       } 
      }); 
    builder.create().show(); 
} 

編輯:

更多代碼:

公共類VideosFragment擴展ContractListFragment實現LoaderManager.LoaderCallbacks,SimpleCursorAdapter.ViewBinder { 私人ImageLoader的ImageLoader的;

@Override 
public void onAttach(Activity host) { 
    super.onAttach(host); 

    ImageLoaderConfiguration ilConfig= 
      new ImageLoaderConfiguration.Builder(getActivity()).build(); 

    imageLoader=ImageLoader.getInstance(); 
    imageLoader.init(ilConfig); 
} 

@Override 
public void onActivityCreated(Bundle state) { 
    super.onActivityCreated(state); 

    String[] from= 
      { MediaStore.Video.Media.TITLE, MediaStore.Video.Media._ID }; 
    int[] to= { android.R.id.text1, R.id.thumbnail }; 
    SimpleCursorAdapter adapter= 
      new SimpleCursorAdapter(getActivity(), R.layout.row, null, 
        from, to, 0); 

    adapter.setViewBinder(this); 
    setListAdapter(adapter); 

    getLoaderManager().initLoader(0, null, this); 
} 

@Override 
public void onListItemClick(ListView l, View v, int position, long id) { 
    CursorAdapter adapter=(CursorAdapter)getListAdapter(); 
    Cursor c=(Cursor)adapter.getItem(position); 
    int uriColumn=c.getColumnIndex(MediaStore.Video.Media.DATA); 
    int mimeTypeColumn= 
      c.getColumnIndex(MediaStore.Video.Media.MIME_TYPE); 

    getContract().onVideoSelected(c.getString(uriColumn), 
      c.getString(mimeTypeColumn)); 
} 

@Override 
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) { 
    return(new CursorLoader(
      getActivity(), 
      MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 
      null, null, null, 
      MediaStore.Video.Media.TITLE)); 
} 

@Override 
public void onLoadFinished(Loader<Cursor> loader, Cursor c) { 
    ((CursorAdapter)getListAdapter()).swapCursor(c); 
} 

@Override 
public void onLoaderReset(Loader<Cursor> loader) { 
    ((CursorAdapter)getListAdapter()).swapCursor(null); 
} 

@Override 
public boolean setViewValue(View v, Cursor c, int column) { 
    if (column == c.getColumnIndex(MediaStore.Video.Media._ID)) { 
     Uri video= 
       ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 
         c.getInt(column)); 
     DisplayImageOptions opts=new DisplayImageOptions.Builder() 
       .showImageOnLoading(R.drawable.ic_media_video_poster) 
       .build(); 

     imageLoader.displayImage(video.toString(), (ImageView)v, opts); 

     return(true); 
    } 

    return(false); 
} 

interface Contract { 
    void onVideoSelected(String uri, String mimeType); 
} 

}

回答

0

要正確刪除已收錄媒體掃描介質,您應該使用經過內容提供商將其刪除。例如:

getContentResolver().delete(uri, null, null); 
+0

錯誤:(159,61)錯誤:不兼容的類型:字符串不能轉換到的Uri –

+0

那麼字符串創建URI。 'Uri u = android.net.Uri.parse(uri)' –

+0

現在停止應用程序。日誌:java.lang.IllegalArgumentException:未知的URL /storage/sdcard1/DCIM/Camera/VID_20160211_190216.mp4 –