【问题标题】:How to select video from gallery and set the selected video icon to ImageView如何从图库中选择视频并将所选视频图标设置为 ImageView
【发布时间】:2013-12-29 02:45:04
【问题描述】:

此技术已成功用于使用图库中的图片 但我想为 Audio 和 video 实现同样的东西。 代码是

public void video(View v)
{
    Intent i = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
    RESULT_LOAD_VIDEO = 1;
    startActivityForResult(i, RESULT_LOAD_VIDEO);
}   
public void audio(View v)
{
    Intent i = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
    RESULT_LOAD_AUDIO = 1;
    startActivityForResult(i, RESULT_LOAD_AUDIO);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);



      if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK &&null != data)
      {
          Uri selectedImage = data.getData(); String[]
      filePathColumn = { MediaStore.Images.Media.DATA };

      Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
      cursor.moveToFirst();

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
      String picturePath = cursor.getString(columnIndex); cursor.close();

      // String picturePath contains the path of selected Image

      NewPoll.flag++;
      Path.patha= picturePath;
      Intent ints=new  Intent(getApplicationContext(),NewPoll.class);
      ints.putExtra("address",picturePath);
      ints.putExtra("option",comingData);
      startActivity(ints); 
      }


      if (requestCode == RESULT_LOAD_VIDEO && resultCode == RESULT_OK &&null != data)
      {
          Uri selectedVideo= data.getData(); String[]
      filePathColumn = { MediaStore.Video.Media.DATA };

      Cursor cursor = getContentResolver().query(selectedVideo,filePathColumn, null, null, null);
      cursor.moveToFirst();

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
      String videoPath = cursor.getString(columnIndex); cursor.close();

      // String picturePath contains the path of selected Image

      NewPoll.flag++;
      Path.patha= videoPath;
      Intent ints=new  Intent(getApplicationContext(),NewPoll.class);
      ints.putExtra("address",videoPath);
      ints.putExtra("option",comingData);
      startActivity(ints); 
      }




      if (requestCode == RESULT_LOAD_AUDIO && resultCode == RESULT_OK &&null != data)
      {
          Uri selectedAudio= data.getData(); String[]
      filePathColumn = { MediaStore.Audio.Media.DATA };

      Cursor cursor = getContentResolver().query(selectedAudio,filePathColumn, null, null, null);
      cursor.moveToFirst();

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
      String audioPath = cursor.getString(columnIndex); cursor.close();

      // String picturePath contains the path of selected Image

      NewPoll.flag++;
      Path.patha= audioPath;
      Intent ints=new  Intent(getApplicationContext(),NewPoll.class);
      ints.putExtra("address",audioPath);
      ints.putExtra("option",comingData);
      startActivity(ints); 
      }

}

我在照片库上取得了成功,但不知道如何使用视频和音频库 来自你方的代码是适用的! 谢谢

【问题讨论】:

    标签: android gallery onactivityresult video-gallery


    【解决方案1】:
    //from this u get all audio 
        private void getallaudio()
        {
    
            String[] STAR = { "*" };
    
    
            Cursor audioCursor = ((Activity) cntx).managedQuery(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,color_string));
    //                    Log.i("Audio Path",path);
                    }while (audioCursor.moveToNext());
    
                }
    //            audioCursor.close();
            }
        }
    
        //from this u get all video
        private void getallvideo()
        {
             String[] STAR = { "*" };
    
    
            controller.videoWrapper.clear();
            Cursor videoCursor = ((Activity) cntx).managedQuery(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));
    //                    Log.i("Video Path",path);
                    }while (videoCursor.moveToNext());
    
                }
    //            videoCursor.close();
            }
        }
    

    【讨论】:

    • 什么是控制器??
    • 忽略它实际上是我的类名
    • 好的!我试过你的代码它没有错误,我试过调试,,你的代码正在执行但没有给出输出,它甚至没有打开画廊.....
    • 老兄,它只给你所有的图像细节,如名称路径等等等,然后你只需让一个适配器获取网格视图并在我的数据的帮助下设置它:) 它的自定义老兄
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多