0

I have a problem, this code works perfectly, but if the video is in sdcard can not be erased by mediaScan. It is a mistake? or it is a special permission?

    @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();
}

edit:

More code:

public class VideosFragment extends ContractListFragment implements LoaderManager.LoaderCallbacks, SimpleCursorAdapter.ViewBinder { private 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);
}

}

Cœur
  • 37,241
  • 25
  • 195
  • 267
diaconu liviu
  • 1,032
  • 2
  • 13
  • 30

1 Answers1

0

To correctly delete media that has been indexed by the media scanner, you should use delete it through the content provider. For example:

getContentResolver().delete(uri, null, null);
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Error:(159, 61) error: incompatible types: String cannot be converted to Uri – diaconu liviu Feb 11 '16 at 18:39
  • Then create a Uri from the String. `Uri u = android.net.Uri.parse(uri)` – Doug Stevenson Feb 11 '16 at 18:42
  • Now stop de app. log: java.lang.IllegalArgumentException: Unknown URL /storage/sdcard1/DCIM/Camera/VID_20160211_190216.mp4 – diaconu liviu Feb 11 '16 at 18:52
  • but the location is correct. if I play. works perfectly – diaconu liviu Feb 11 '16 at 18:55
  • So you're saying that string is not a uri. It's a file path. `Uri u = android.net.Uri.fromFile(uri)` – Doug Stevenson Feb 11 '16 at 18:57
  • Uri u = android.net.Uri.parse(uri); getContentResolver().delete(u, null,null); and app is stop – diaconu liviu Feb 11 '16 at 19:07
  • and this Error:(155, 66) error: incompatible types: String cannot be converted to File from Uri u = android.net.Uri.fromFile(uri) – diaconu liviu Feb 11 '16 at 19:12
  • and this getContentResolver().delete(Uri.fromFile(new File(uri)), null,null); app is stoped log: file://java.lang.IllegalArgumentException: Unknown URL /storage/sdcard1/DCIM/Camera/VID_20160211_190216.mp4 – diaconu liviu Feb 11 '16 at 19:25
  • You're going to have to show more code or at least say how you're querying for videos and getting that file path. The idea is that you need to tell the media content provider to delete the video from its own index, which will also delete the file. The video uri comes from the content provider when you query it. – Doug Stevenson Feb 11 '16 at 19:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103223/discussion-between-diaconu-liviu-and-doug-stevenson). – diaconu liviu Feb 11 '16 at 19:51