get Image Uri - Android Graphics

Android examples for Graphics:Image Download

Description

get Image Uri

Demo Code


//package com.java2s;
import java.io.File;
import android.content.ContentValues;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;

public class Main {
    private static final Uri external_content_uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

    public static Uri getImageUri(Context context, Bitmap bmp) {
        if (bmp != null) {
            Uri localUri = Uri.parse(MediaStore.Images.Media.insertImage(
                    context.getContentResolver(), bmp, null, null));
            if (localUri != null) {
                ContentValues cv = new ContentValues();
                cv.put(MediaStore.Images.ImageColumns.DATE_TAKEN,
                        Long.valueOf(System.currentTimeMillis()));
                cv.put(MediaStore.Images.ImageColumns.DATE_ADDED,
                        Long.valueOf(System.currentTimeMillis()));
                context.getContentResolver().update(localUri, cv, null,
                        null);//w  ww.j  av a  2  s  .  c  om
            }
            bmp.recycle();
            return localUri;
        }
        return null;
    }

    public static Uri getImageUri(Context context, File file) {
        if ((file.exists()) && (file.isFile()))
            try {
                ContentValues cv = new ContentValues();
                cv.put(MediaStore.Images.ImageColumns.MIME_TYPE,
                        "image/jpeg");
                cv.put(MediaStore.Images.ImageColumns.DATA,
                        file.getAbsolutePath());
                cv.put(MediaStore.Images.ImageColumns.DATE_TAKEN,
                        Long.valueOf(System.currentTimeMillis()));
                cv.put(MediaStore.Images.ImageColumns.DATE_ADDED,
                        Long.valueOf(System.currentTimeMillis()));
                Uri localUri = context.getContentResolver().insert(
                        external_content_uri, cv);
                return localUri;
            } catch (Exception e) {
                e.printStackTrace();
            }
        return null;
    }
}

Related Tutorials