来自外部标牌Lollipop +

时间:2016-02-11 18:11:57

标签: android android-intent android-tv android-mediascanner

我有一个问题,这段代码工作正常,但如果视频在SD卡中无法被mediaScan删除。 这是一个错误?或者是特别许可?

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

编辑:

更多代码:

public class VideosFragment extends 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);
}

}

1 个答案:

答案 0 :(得分:0)

要正确删除已被媒体扫描程序编入索引的媒体,您应该使用通过内容提供商删除它。例如:

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