Lets retrive image from Video in Android.
activity_main.xml
Write permission to AndroidManifest.xml
MainActivity.java
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/openfile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Open Video File " />
<ImageView
android:id="@+id/img"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Write permission to AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
MainActivity.java
package com.example.getvideoframe;
import java.net.URISyntaxException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int FILE_SELECT_CODE=11;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.openfile);
imageView=(ImageView)findViewById(R.id.img);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showFileChooser();
}
});
}
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"), FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==FILE_SELECT_CODE&&resultCode == RESULT_OK){
// Get the Uri of the selected file
Uri uri = data.getData();
try{
String path = getPath(this, uri);
if(path==null) return;
Bitmap map= retriveVideoFrameFromVideo(path);
imageView.setImageBitmap(map);
}
catch(Exception e){}
}
}
public static Bitmap retriveVideoFrameFromVideo(String p_videoPath)
{
Bitmap m_bitmap = null;
MediaMetadataRetriever m_mediaMetadataRetriever = null;
try
{
m_mediaMetadataRetriever = new MediaMetadataRetriever();
m_mediaMetadataRetriever.setDataSource(p_videoPath);
m_bitmap = m_mediaMetadataRetriever.getFrameAtTime();
}
catch (Exception m_e)
{
}
finally
{
if (m_mediaMetadataRetriever != null)
{
m_mediaMetadataRetriever.release();
}
}
return m_bitmap;
}
public String getPath(Context context, Uri uri) throws URISyntaxException {
if ("content".equalsIgnoreCase(uri.getScheme())) {
String[] projection = { "_data" };
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow("_data");
if (cursor.moveToFirst()) {
return cursor.getString(column_index);
}
} catch (Exception e) {
// Eat it
}
}
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
}
No comments:
Post a Comment