如何打开图像/视频/音频?

时间:2014-03-14 09:47:18

标签: android

在我的应用程序中有一个按钮,当按下时,它应该打开图片库,视频库或音频播放器。

请问我将如何构建执行此操作的意图?

2 个答案:

答案 0 :(得分:1)

尝试使用此按钮只需单击任意按钮即可传递文件路径,然后根据该文件将此方法用于任何类型的文件

 File file = new File(filePath); 
    MimeTypeMap map = MimeTypeMap.getSingleton();
       String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName());
         String type = map.getMimeTypeFromExtension(ext);
        if (type == null)
        type = "*/*";

    Uri uri = Uri.parse("www.google.com");
    Intent type_intent = new Intent(Intent.ACTION_VIEW, uri);
    Uri data = Uri.fromFile(file);
    type_intent.setDataAndType(data, type);
    startActivity(type_intent);

对于图像

private void getallimages(File dir)
    {

    String[] STAR = { "*" };
    controller.images.clear();

    final String orderBy = MediaStore.Images.Media.DEFAULT_SORT_ORDER;
    Cursor imagecursor = cntx.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, STAR, null, null, orderBy);
    int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
    int count = imagecursor.getCount();
    for (int i = 0; i < count; i++) {
        imagecursor.moveToPosition(i);
        int id = imagecursor.getInt(image_column_index);
        ImageItem imageItem = new ImageItem();//this is my wrapper class 
        if(new File(imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA))).length()<=10485760)
        {
            imageItem.filePath = imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA));

        imageItem.id = id;
        imageItem.selection = false; //newly added item will be selected by default this it do for check box unselect u dont need to fill this 
        controller.images.add(imageItem);//this i just add all info in wrapper class    

        }

}      }

for audio

private void getallaudio()
{
     String[] STAR = { "*" };
    controller.audioWrapper.clear();
    Cursor audioCursor = cntx.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, STAR, null, null, null);
    if (audioCursor != null) 
    {
        if (audioCursor.moveToFirst()) 
        {
            do 
            {

                String path = audioCursor.getString(audioCursor.getColumnIndex(MediaStore.Audio.Media.DATA));


                           controller.audioWrapper.add(new MediaWrapper(new File(path).getName(), path, "Audio",false));



             }while (audioCursor.moveToNext());

        }
     }


}

和视频

private void getallvideo()
    {
         String[] STAR = { "*" };

        controller.videoWrapper.clear();
        Cursor videoCursor = cntx.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, STAR, null, null, null);

        if (videoCursor != null) 
        {
            if (videoCursor.moveToFirst()) 
            {
                do 
                {


                    String path = videoCursor.getString(videoCursor.getColumnIndex(MediaStore.Images.Media.DATA));

                               controller.videoWrapper.add(new MediaWrapper(new File(path).getName(), path, "Video",false,color_string));




                 }while (videoCursor.moveToNext());

            }
         }
    }

答案 1 :(得分:0)

我会这样认识:
单击“按钮”,弹出一个带有3个按钮的自定义对话框。

  1. 图片库按钮
  2. 视频库按钮
  3. 音频播放器按钮
  4. 根据用户选择,启动与操作相对应的意图。