0

I am trying to get video from device storage and set it to the imageview but when i am selecting video the video is not display to me in my imageview. and i want to share video to people like share it, whats app etc...when i am clicking on image it will get the video but not set the preview on imageview. here is my code any solution please provide

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

    }

}
Yogesh
  • 31
  • 8

1 Answers1

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..

ClassA
  • 2,480
  • 1
  • 26
  • 57