I am new to Android. I have create one list view which list down all video files from specific folder. I have also created context menu in which there's option for deleting video. But I don't know how to get absolute url of selected video on onContextItemSelected.
Here is my getView() of CustomArrayAdapter
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;
}
Here is 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);
}
}
Here is class file
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){
}
}
Here is the code
case R.id.delete_video:
#info.position returns the index of the item in the listview
VideoListRowItem videoListRowItem =(VideoListRowItem)videoListRowItem.get(info.position);
//use the videoListRowItem class to get the video url