从图库中选择图像或视频或音频文件

时间:2017-11-19 12:15:04

标签: android android-intent

我开发了Android应用程序。我需要从图库中选择图像或音频或视频。我使用了intent.setType(“image / ,video / ,audio / *”);.但没有解决方案。它只接受图像。没有视频或音频。

 Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                galleryIntent.setType("image/*,audio/*,video/*");
                startActivityForResult(galleryIntent, 0);
        btn_browser_id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                galleryIntent.setType("image/*,audio/*,video/*");
                startActivityForResult(galleryIntent, 0);
            }
        });


         @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            // When an Image is picked
            if (requestCode == 0 && resultCode == RESULT_OK && null != data) {

                Uri selectedMediaUri = data.getData();

                if (selectedMediaUri.toString().contains("image")) {
                   // Log.d(logmessage,"Entered into the image************************* "+selectedMediaUri);
                    //handle image
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    file_type="Image";
                    Cursor cursor = getContentResolver().query(selectedMediaUri, filePathColumn, null, null, null);
                    assert cursor != null;
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    mediaPath = cursor.getString(columnIndex);
                    uploadfile.setText(mediaPath);
                    fileupload(mediaPath);
                    //imgView.setImageBitmap(BitmapFactory.decodeFile(mediaPath));
                    cursor.close();


                } else  if (selectedMediaUri.toString().contains("video")) {
                    //handle video
                    Log.d(logmessage,"Entered into the Video************************* "+selectedMediaUri);
                    file_type="Video";
                    String[] filePathColumn = {MediaStore.Video.Media.DATA};

                    Cursor cursor = getContentResolver().query(selectedMediaUri, filePathColumn, null, null, null);
                    assert cursor != null;
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

                    mediaPath = cursor.getString(columnIndex);
                    uploadfile.setText(mediaPath);
                    fileupload(mediaPath);
                    // Set the Video Thumb in ImageView Previewing the Media
                    //imgView.setImageBitmap(getThumbnailPathForLocalFile(MainActivity.this, selectedMediaUri));
                    cursor.close();

                } else  if (selectedMediaUri.toString().contains("audio")) {
                    //handle video
                    Log.d(logmessage,"Entered into the Video************************* "+selectedMediaUri);
                    file_type="Audio";
                    String[] filePathColumn = {MediaStore.Audio.Media.DATA};

                    Cursor cursor = getContentResolver().query(selectedMediaUri, filePathColumn, null, null, null);
                    assert cursor != null;
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

                    mediaPath = cursor.getString(columnIndex);
                    uploadfile.setText(mediaPath);
                    fileupload(mediaPath);
                    // Set the Video Thumb in ImageView Previewing the Media
                    //imgView.setImageBitmap(getThumbnailPathForLocalFile(MainActivity.this, selectedMediaUri));
                    cursor.close();

                }


            }  else {
                Toast.makeText(this, "You haven't picked Image/Video", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
        }

    }

0 个答案:

没有答案