如何从设备获取视频并将其设置为android中的imageview

时间:2017-08-05 08:31:15

标签: android video android-videoview

我正在尝试从设备存储中获取视频并将其设置为 imageview但当我选择视频时,视频不会显示 我在我的imageview。我想分享视频给人们分享它,什么应用程序等...当我点击图像时,它将获得视频但不在imageview上设置预览。 这是我的代码任何解决方案请提供

package com.example.priya.electionmgntdemoapplication.Activity;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.priya.electionmgntdemoapplication.R;

public class VideoDemo extends AppCompatActivity implements View.OnClickListener {

    int SELECT_VIDEO_REQUEST = 100;
    String selectedVideoPath = "";


    private static final int SELECT_VIDEO = 2;
    String selectedPath = "";
    Uri selectedImageUri;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_demo);

        imageView = (ImageView) findViewById(R.id.video);

        imageView.setOnClickListener(this);
    }


    public void openGalleryAudio() {

        Intent intent = new Intent();
        intent.setType("video/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Video "), SELECT_VIDEO);
    }

    public void selectVideoFromGallery() {
        Intent intent;
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
            intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
        } else {
            intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.INTERNAL_CONTENT_URI);
        }
        intent.setType("video/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, SELECT_VIDEO_REQUEST);
    }


    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == RESULT_OK) {

            if (requestCode == SELECT_VIDEO_REQUEST) {
                if (data.getData() != null) {



                    selectedVideoPath = getPath(data.getData());
                    Bitmap bmThumbnail;

                    // MINI_KIND: 512 x 384 thumbnail
                    bmThumbnail = ThumbnailUtils.createVideoThumbnail(selectedVideoPath,
                            MediaStore.Images.Thumbnails.MINI_KIND);
                    imageView.setImageBitmap(bmThumbnail);
                    imageView.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            shareIt();
                        }
                    });

                } else {
                    Toast.makeText(getApplicationContext(), "Failed to select video", Toast.LENGTH_LONG).show();
                }
            }



         /*   if (requestCode == SELECT_VIDEO) {
                System.out.println("SELECT_VIDEO");
                Uri selectedImageUri = data.getData();
                selectedPath = getPath(selectedImageUri);
                System.out.println("SELECT_VIDEO Path : " + selectedPath);
                imageView.setImageURI(selectedImageUri);

            } */


        }
    }

    public String getPath(Uri uri) {
        String[] projection = {MediaStore.Video.Media.DATA};
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

    public void onClick(View view) {
        // openGalleryAudio();

        selectVideoFromGallery();

    }


    private void shareIt() {


        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("video");
        intent.putExtra(Intent.EXTRA_STREAM, selectedImageUri);
        intent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                "Check My Image ");
        startActivity(Intent.createChooser(intent, "Share video"));

    }

}

1 个答案:

答案 0 :(得分:0)

Here is a example how to get/create a thumbnail from a video:

 public static String getThumbnailPathForLocalFile(Context context, Uri fileUri) {
    long fileId = getFileId(context, fileUri);
    MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(), fileId, 3, null);
    Cursor thumbCursor = context.getContentResolver().query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, thumbColumns, "video_id = " + fileId, null, null);
    if (thumbCursor.moveToFirst()) {
        return thumbCursor.getString(thumbCursor.getColumnIndex("_data"));
    }
    return null;
}

public static long getFileId(Context context, Uri fileUri) {
    Cursor cursor = context.getContentResolver().query(fileUri, mediaColumns, null, null, null);
    if (cursor.moveToFirst()) {
        return (long) cursor.getInt(cursor.getColumnIndexOrThrow(SQLiteHelper._ID));
    }
    return 0;
}

then in onActivityResult:

String thumbPathString = getThumbnailPathForLocalFile(this, data.getData());
imageView.setImageURI(Uri.parse(thumbPathString));

I hope this helps you.

I haven't tested this because thumbnail path is stored in SQLite..