2014-03-14 195 views
1

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

请问我该如何构建一个意图来执行它?

+0

你需要或(或其中任何一个)或AND(全部)? –

+0

@Atul O Holic非常感谢Atul,以及所有这些,以便用户可以选择。 –

+0

单击按钮,您可以打开ContextMenu并添加图片库,视频库或音频播放器。 – Piyush

回答

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  

     } 

} }

与此尝试只是的onclick任意按钮的传递文件路径,然后该方法poen任何类型的文件

音频

​​

和视频

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

      } 
     } 
    } 
+0

谢谢Bhanu,但我不需要打开一个文件,而是需要用户看到Gallery和MediaPlayer应用程序,以便他可以选择要使用的音频,视频或图片。 –

+0

K I更新我答 –

+0

现在看到我的比格比格biggg答:)它会给ü所有ü要@LisaAnne :) –

0

我会意识到这样说:
在按钮的点击,弹出一个自定义对话框的三个键。

  1. 图片库按钮
  2. 视频库按钮
  3. 音频播放器按钮

根据用户的选择,你开始对应于行动的意图。

+0

谢谢你懒惰的忍者,但不幸的是我只需要1按钮。 LISA –

+0

@LisaAnne只有一个按钮,但你必须添加一个弹出对话框。 –