Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import android.net.Uri;

import android.provider.MediaStore.Images.Media;

import java.io.File;

public class Main {
    public static Uri pathToContentUri(Context context, String imagePath) {
        Cursor cursor = context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[] { "_id" },
                "_data=? ", new String[] { imagePath }, null);
        if (cursor != null && cursor.moveToFirst()) {
            int imageFile1 = cursor.getInt(cursor.getColumnIndex("_id"));
            Uri values1 = Uri.parse("content://media/external/images/media");
            return Uri.withAppendedPath(values1, "" + imageFile1);
        } else {
            File imageFile = new File(imagePath);
            if (imageFile.exists()) {
                ContentValues values = new ContentValues();
                values.put("_data", imagePath);
                Uri baseUri = Media.EXTERNAL_CONTENT_URI;
                return context.getContentResolver().insert(baseUri, values);
            } else {
                return null;
            }
        }
    }
}