Android,对图像,视频和音频使用相同的onActivityResult

时间:2016-12-02 09:23:09

标签: android image audio video media

我想对图像,视频和音频使用相同的onActivityResult。

问题。
当我选择音频时,它会保存视频和图像中的音频路径。 我在分离图像,视频和音频方面遇到了问题。

我的代码。

private static final int SELECT_PICTURE = 1;
private static final int SELECT_AUDIO = 1;
private static final int REQUEST_TAKE_GALLERY_VIDEO = 1;

音频,图片,视频

addVideo.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("video/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,"Select Video"),REQUEST_TAKE_GALLERY_VIDEO);
        }
    });

    addAudio.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("audio/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,"Select Audio"),SELECT_AUDIO);
        }
    });

    addImage.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
        }
    });

onActivityResult

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_TAKE_GALLERY_VIDEO) {
            Uri selectedVideoUri = data.getData();
            selectedVideoPath = getPath(selectedVideoUri);
        }
        if (requestCode == SELECT_AUDIO) {
            Uri selectedAudioUri = data.getData();
            selectedAudioPath = getPath(selectedAudioUri);
        }
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            File f = new File(selectedImagePath);
            if(f.exists()){
                //w, h
                Bitmap myBitmap = resizeImage.getCompressedBitmap(200, 200, f);
                img.setImageBitmap(myBitmap);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:5)

您对所有请求代码使用相同的ID

private static final int SELECT_PICTURE = 1;
private static final int SELECT_AUDIO = 1;
private static final int REQUEST_TAKE_GALLERY_VIDEO = 1;

然后OnActivityResult无法区分。你应该给他们一个独特的身份。

private static final int SELECT_PICTURE = 1;
private static final int SELECT_AUDIO = 2;
private static final int REQUEST_TAKE_GALLERY_VIDEO = 3;

答案 1 :(得分:1)

Charm Geldjust根据此更新你的静态值..

private static final int SELECT_PICTURE = 1;
private static final int SELECT_AUDIO = 2;
private static final int REQUEST_TAKE_GALLERY_VIDEO = 3;