我正在使用 android 相机开发一个简单的视频应用程序。在我的应用程序中,我有一个名为“制作视频”的按钮和一个列表视图,用于显示我的应用程序录制的视频名称。现在,当我单击“制作视频”按钮时,它会打开我的移动摄像头进行录制,但是当我完成录制时,摄像头会给我两个选项。“保存”和“丢弃”。现在通过单击“保存”选项,我想将录制的视频的名称添加到我的列表视图中。我在这方面开发了一些代码,它工作正常,但我面临的问题是如何在 onActivityResult 方法的列表视图中添加录制视频的名称并更新我的列表视图。请帮助我,我会非常感谢你。
您可以在下面查看我的代码。
public class MainActivity extends ListActivity
{
private ArrayList<String> cameraVideoList = new ArrayList<String>();
Context ctx;
// Resources res;
int REQUEST_VIDEO_CAPTURED =1;
Uri uriVideo = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ctx = getApplicationContext();
Button makeVideo = (Button) findViewById(R.id.button1);
makeVideo.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent, REQUEST_VIDEO_CAPTURED);
}
});
ListView videoList = getListView();
videoList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3)
{
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
setListAdapter(new ImageAndTextAdapter(ctx, R.layout.list_item_icon_text, cameraVideoList));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
if (requestCode == REQUEST_VIDEO_CAPTURED)
{
uriVideo = data.getData();
// Toast.makeText(MainActivity.this, uriVideo.getPath(),
// Toast.LENGTH_LONG).show();
//
// Toast.makeText(MainActivity.this, uriVideo.toString(),
// Toast.LENGTH_LONG).show();
cameraVideoList.add(getFileNameFromUrl(uriVideo.getPath().toString()));
}
}
}
public String getFileNameFromUrl(String path)
{
String[] pathArray = path.split("/");
return pathArray[pathArray.length - 1];
}
public class ImageAndTextAdapter extends ArrayAdapter<String>
{
private LayoutInflater mInflater;
private ArrayList<String> mStrings;
private int mViewResourceId;
public ImageAndTextAdapter(Context context, int textViewResourceId,ArrayList<String> objects)
{
super(context, textViewResourceId, objects);
mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mStrings = objects;
mViewResourceId = textViewResourceId;
}
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.icon);
iv.setImageResource(R.drawable.video_icon);
TextView tv = (TextView)convertView.findViewById(R.id.text);
tv.setText(mStrings.get(position));
return convertView;
}
}
}