Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;

import android.net.Uri;

import android.util.Log;

public class Main {
    private static final String TAG = "ViqCommonUtilities";

    /**
     * 
     * @param context
     * @param uri
     *            uri of SCHEME_FILE or SCHEME_CONTENT
     * @return image path; uri will be changed to SCHEME_FILE
     */
    public static String uriToImagePath(Context context, Uri uri) {
        if (context == null || uri == null) {
            return null;
        }

        String imagePath = null;
        String uriString = uri.toString();
        String uriSchema = uri.getScheme();
        if (uriSchema.equals(ContentResolver.SCHEME_FILE)) {
            imagePath = uriString.substring("file://".length());
        } else {// uriSchema.equals(ContentResolver.SCHEME_CONTENT)
            ContentResolver resolver = context.getContentResolver();
            Cursor cursor = resolver.query(uri, null, null, null, null);
            if (cursor.getCount() == 0) {
                Log.e(TAG, "Uri(" + uri.toString() + ") not found!");
                return null;
            }
            cursor.moveToFirst();
            imagePath = cursor.getString(1);
            // Change the SCHEME_CONTENT uri to the SCHEME_FILE.
            uri = Uri.fromFile(new File(imagePath));
        }
        Log.v(TAG, "Final uri: " + uri.toString());
        return imagePath;
    }
}