如何从 android 的 onContextItemSelected 列表视图中获取所选视频的绝对路径?

How to get absolute path of selected video from list view in onContextItemSelected of android?

我是 Android 的新手。我创建了一个列表视图,其中列出了特定文件夹中的所有视频文件。我还创建了上下文菜单,其中有删除视频的选项。但我不知道如何在 onContextItemSelected.

上获得所选视频的绝对 url

这是我的 CustomArrayAdapter

的 getView()
public View getView(int position, View convertView, ViewGroup parent){
    ViewHolder holder;
    VideoListRowItem videoListRowItem = getItem(position);

    LayoutInflater mInfalter = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if(convertView == null){
        convertView = mInfalter.inflate(R.layout.list_row_video, null);
        holder = new ViewHolder();
        holder.textView = (TextView) convertView.findViewById(R.id.name_video);
        holder.videoView = (ImageView) convertView.findViewById(R.id.video_icon);
        holder.videoPath = (TextView) convertView.findViewById(R.id.video_path);


    }else{
        holder = (ViewHolder) convertView.getTag();
    }


    holder.textView.setText(videoListRowItem.getTitle());
    holder.videoView.setImageBitmap(videoListRowItem.getVideoId());
    holder.videoPath.setText(videoListRowItem.getVideoPath());

    return convertView;
}

这里是onContextItemSelected()

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    // Here I want to get absolute url of selected video
    String path = ''
    switch (item.getItemId()) {
        case R.id.analyze:
            Toast.makeText(getActivity(), path, Toast.LENGTH_SHORT).show();
            //editNote(info.id);
            return true;
        case R.id.delete_video:
            Toast.makeText(getActivity(), path, Toast.LENGTH_SHORT).show();
            //deleteNote(info.id);
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

这是class文件

    package com.example.dp.facialemotionalyzer.fragment;

import android.app.Activity;
import android.app.ListFragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dp.facialemotionalyzer.R;
import com.example.dp.facialemotionalyzer.activity.MainActivity;
import com.example.dp.facialemotionalyzer.other.CustomArrayAdapter;
import com.example.dp.facialemotionalyzer.other.VideoListRowItem;
import com.example.dp.facialemotionalyzer.service.VideoAnalyzerService;

import java.io.File;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import static android.R.attr.data;


/**
 * A fragment representing a list of Items.
 * <p/>
 * Activities containing this fragment MUST implement the {@link }
 * interface.
 */
public class VideoListItemFragment extends ListFragment implements AdapterView.OnItemClickListener {

    // TODO: Customize parameter argument names
    private static final String ARG_COLUMN_COUNT = "column-count";
    // TODO: Customize parameters
    private int mColumnCount = 1;

    // videoPathUrl to store clicked videos path
    String videoPathUrl;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public VideoListItemFragment() {
    }

    // TODO: Customize parameter initialization
    @SuppressWarnings("unused")
    public static VideoListItemFragment newInstance(int columnCount) {
        VideoListItemFragment fragment = new VideoListItemFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_COLUMN_COUNT, columnCount);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
        }
    }

    List<VideoListRowItem> videoListRowItem;
    List<File> videoFiles = new ArrayList<File>();
    List<Bitmap> videoImages = new ArrayList<Bitmap>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.list_fragment, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        videoListRowItem = new ArrayList<>();
        File facialEmoDir = new File(Environment.getExternalStorageDirectory(), "FacialEmotionalyzer");
        File videoDir = new File(facialEmoDir.getAbsolutePath(), "Video");
        if(videoDir.exists()){
            listFilesForFolder(videoDir);
        }
        for (int i=0; i<videoFiles.size(); i++) {
            VideoListRowItem item = new VideoListRowItem(videoImages.get(i),
                    videoFiles.get(i).getName(), videoFiles.get(i).getAbsolutePath());
            videoListRowItem.add(item);
        }
        CustomArrayAdapter customArrayAdapter = new CustomArrayAdapter(getActivity().getBaseContext(),
                R.layout.list_fragment, videoListRowItem);
        setListAdapter(customArrayAdapter);
        ListView listView = getListView();
        registerForContextMenu(listView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView tv = (TextView) view.findViewById(R.id.video_path);
                videoPathUrl = tv.getText().toString();
                Toast.makeText(getActivity(), videoPathUrl, Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
        Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo){
        super.onCreateContextMenu(menu, view, menuInfo);
        MenuInflater inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.video_menus, menu);
        menu.setHeaderTitle("Select Options");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId()) {
            case R.id.delete_video:
                Toast.makeText(getActivity(), videoPathUrl, Toast.LENGTH_SHORT).show();
                //deleteVideo(videoPathUrl);
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }

    public void listFilesForFolder(final File folder) {
        for (final File fileEntry : folder.listFiles()) {
            if (fileEntry.isDirectory()) {
                listFilesForFolder(fileEntry);
            } else {
                if(isVideoFile(fileEntry.getAbsolutePath())){
                    videoFiles.add(fileEntry);
                    Bitmap thumb = ThumbnailUtils.createVideoThumbnail(fileEntry.getAbsolutePath(),
                            MediaStore.Images.Thumbnails.MINI_KIND);
                    videoImages.add(thumb);
                }
            }
        }
    }

    public boolean isVideoFile(String path) {
        String mimeType = URLConnection.guessContentTypeFromName(path);
        return mimeType != null && mimeType.startsWith("video");
    }

    public void deleteVideo(String name){

    }
}

这是代码

        case R.id.delete_video:
           #info.position returns the index of the item in the listview
           VideoListRowItem videoListRowItem =(VideoListRowItem)videoListRowItem.get(info.position);

//使用videoListRowItem class获取视频url