insert External file to MediaStore - Android Media

Android examples for Media:Media File

Description

insert External file to MediaStore

Demo Code


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import java.util.ArrayList;

public class Main{
    public static MediaStoreObject insertExternal(Context ctx,
            MediaStoreObject o) {/*from   w w w . jav a  2 s.c o m*/
        return insert(ctx, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, o);
    }
    private static MediaStoreObject insert(Context ctx, Uri tableUri,
            MediaStoreObject o) {
        ContentValues values = buildValues(o);

        Uri u = ctx.getContentResolver().insert(tableUri, values);

        if (u == null) {
            return null;
        } else {
            o.uri = u;
            return o;
        }
    }
    private static ContentValues buildValues(MediaStoreObject o) {
        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, o.data);
        values.put(MediaStore.MediaColumns.SIZE, o.size);
        values.put(MediaStore.MediaColumns.DISPLAY_NAME, o.displayName);
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg"); // consider application/ogg
        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
        values.put(MediaStore.Audio.Media.TITLE, "RingPack Tone");

        return values;
    }
}

Related Tutorials