Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have successfully created for just capturing the Image and Displaying the Image on the Screen, But Please me the same way for Attaching Audio & Video too, And how do I get option of Attaching media file from Gallery onClick of Buttons.

What I have tried:

package inducesmile.com.androidcameraapplication;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.VideoView;


public class MainActivity extends ActionBarActivity {

    private ImageView imageHolder;
    private VideoView videoHolder;
    private final int requestCode = 20;

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

        imageHolder = (ImageView)findViewById(R.id.captured_photo);
        Button capturedImageButton = (Button)findViewById(R.id.photo_button);
        Button capturedVideoButton = (Button)findViewById(R.id.Video_button);
        Button capturedAudioButton = (Button)findViewById(R.id.Audio_button);
        capturedImageButton.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent photoCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(photoCaptureIntent, requestCode);
            }
        });

        capturedVideoButton.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent  videoCaptureIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                startActivityForResult(videoCaptureIntent, requestCode);
            }
        });

        capturedAudioButton.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent audioCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE_SECURE);
                startActivityForResult(audioCaptureIntent, requestCode);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(this.requestCode == requestCode && resultCode == RESULT_OK){
            Bitmap bitmap = (Bitmap)data.getExtras().get("data");
            imageHolder.setImageBitmap(bitmap);
        }
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
Posted
Updated 7-Sep-17 5:34am

1 solution

I would suggest using separate request codes for each of the calls to startActivityForResult(). Otherwise, there's no way to distinguish between them in onActivityResult().

If, for example, you are wanting to show the first frame of the captured video in the VideoView object, just call its seekTo(0) method.

Another idea would be to assign a thumbnail to it, something like:
Bitmap bmThumb = ThumbnailUtils.createVideoThumbnail("path", MediaStore.Images.Thumbnails.MINI_KIND);
BitmapDrawable bmDrawable = new BitmapDrawable(bmThumb);
videoHolder.setBackgroundDrawable(bmDrawable);

As far as audio goes, there's nothing to display. So even if you captured something using the Voice Recorder app, it's just audio -- no image(s).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900