Create a file Uri for saving an image or video - Android Network

Android examples for Network:Uri

Description

Create a file Uri for saving an image or video

Demo Code


//package com.java2s;

import java.io.File;

import android.net.Uri;
import android.os.Environment;

import android.util.Log;

public class Main {
    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;
    public static final String TEMP_PIC_PATH = "asdf/PhotoTemp";

    public static Uri getOutputMediaFileUri(int type) {
        return Uri.fromFile(getOutputMediaFile(type));
    }/* ww w  . j  av a  2  s . c om*/

    public static File getOutputMediaFile(int type) {

        File mediaStorageDir = new File(
                Environment.getExternalStorageDirectory(), TEMP_PIC_PATH);

        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("DriverConnex", "Failed to create directory!");
                return null;
            }
        }

        File mediaFile = null;

        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_TEMP.jpg");
        } else if (type == MEDIA_TYPE_VIDEO) {
              mediaFile = new File(mediaStorageDir.getPath() + File.separator
              + "VID_" + timeStamp + ".mp4");
        } else
            return null;

        return mediaFile;
    }
}

Related Tutorials