Save Media content

Description

The following code shows how to Save Media content.

Example

Register Media content


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.examples.mediastore"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
</manifest>

Main layout xml file


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <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/textButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Text Document" />

</LinearLayout>

Main Activity Java code


import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
/*from  ww  w  .j  a  v a 2  s  .c om*/
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

public class MainActivity extends Activity implements View.OnClickListener {

    private static final int REQUEST_CAPTURE = 100;
    private static final int REQUEST_DOCUMENT = 101;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.save);
        
        Button images = (Button) findViewById(R.id.imageButton);
        images.setOnClickListener(this);
        Button videos = (Button) findViewById(R.id.videoButton);
        videos.setOnClickListener(this);
        Button text = (Button) findViewById(R.id.textButton);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            text.setOnClickListener(this);
        } else {
            text.setVisibility(View.GONE);
        }
    }
    
    @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();
        }
        if (requestCode == REQUEST_DOCUMENT && resultCode == Activity.RESULT_OK) {
            Uri document = data.getData();
            writeDocument(document);
        }
    }
    
    private void writeDocument(Uri document) {
        try {
            ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(document, "w");
            FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());
            StringBuilder sb = new StringBuilder();
            sb.append("Last Written at: ");
            sb.append(DateFormat.getLongDateFormat(this).format(new Date()));

            out.write(sb.toString().getBytes());
            out.flush();
            out.close();
            pfd.close();
        } catch (Exception e) {
            Log.w("Android", e);
        }
    }
    
    @Override
    public void onClick(View v) {
        ContentValues values;
        Intent intent;
        Uri storeLocation;
        final long nowMillis = System.currentTimeMillis();
        
        switch(v.getId()) {
        case R.id.imageButton:
            values = new ContentValues(5);
            values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, nowMillis);
            values.put(MediaStore.Images.ImageColumns.DATE_ADDED, nowMillis / 1000);
            values.put(MediaStore.Images.ImageColumns.DATE_MODIFIED, nowMillis / 1000);
            values.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, "Test Image Sample");
            values.put(MediaStore.Images.ImageColumns.TITLE, "Test Image Sample");
            
            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(7);
            values.put(MediaStore.Video.VideoColumns.DATE_TAKEN, nowMillis);
            values.put(MediaStore.Video.VideoColumns.DATE_ADDED, nowMillis / 1000);
            values.put(MediaStore.Video.VideoColumns.DATE_MODIFIED, nowMillis / 1000);
            values.put(MediaStore.Video.VideoColumns.DISPLAY_NAME, "Test Video Sample");
            values.put(MediaStore.Video.VideoColumns.TITLE, "Test Video Sample");
            values.put(MediaStore.Video.VideoColumns.ARTIST, "Yours Truly");
            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;
        case R.id.textButton:
            intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TITLE, "Test");
            startActivityForResult(intent, REQUEST_DOCUMENT);
        default:
            return;
        }
    }
}
Save Media content




















Home »
  Android »
    Android Basics »




Hello Android
Resources
Activity
Calendar
Camera
Contact
Content Provider
Database
Hardware
Intent
Location
Media
Network
Notification
Preference
Sensor
Service
SMS