Mega Code Archive

 
Categories / Android / Media
 

Store image and video

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:orientation="vertical"   android:layout_width="fill_parent"   android:layout_height="fill_parent">   <Button     android:id="@+id/imageButton"     android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="Images"   />   <Button     android:id="@+id/videoButton"     android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="Video"   />   <Button     android:id="@+id/audioButton"     android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="Audio"   /> </LinearLayout> package app.test; import android.app.Activity; import android.content.ContentValues; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.Button; import android.widget.Toast; public class Test extends Activity implements View.OnClickListener {     private static final int REQUEST_CAPTURE = 100;          @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);                  Button images = (Button)findViewById(R.id.imageButton);         images.setOnClickListener(this);         Button videos = (Button)findViewById(R.id.videoButton);         videos.setOnClickListener(this);     }          @Override     protected void onActivityResult(int requestCode, int resultCode, Intent data) {         if(requestCode == REQUEST_CAPTURE && resultCode == Activity.RESULT_OK) {             Toast.makeText(this, "All Done!", Toast.LENGTH_SHORT).show();         }     }     @Override     public void onClick(View v) {         ContentValues values;         Intent intent;         Uri storeLocation;                  switch(v.getId()) {         case R.id.imageButton:             values = new ContentValues(2);             values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, System.currentTimeMillis());             values.put(MediaStore.Images.ImageColumns.DESCRIPTION, "Sample Image");             storeLocation = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);             intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);             intent.putExtra(MediaStore.EXTRA_OUTPUT, storeLocation);             startActivityForResult(intent, REQUEST_CAPTURE);             return;         case R.id.videoButton:             values = new ContentValues(2);             values.put(MediaStore.Video.VideoColumns.ARTIST, "Artist");             values.put(MediaStore.Video.VideoColumns.DESCRIPTION, "Sample Video Clip");             storeLocation = getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);             intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);             intent.putExtra(MediaStore.EXTRA_OUTPUT, storeLocation);             startActivityForResult(intent, REQUEST_CAPTURE);             return;         default:             return;         }     } }