Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.ContentResolver;
import android.content.Context;

import android.database.Cursor;

import android.net.Uri;

import android.provider.MediaStore;

public class Main {

    public static String getRealPathByUri(Context context, Uri uri) {
        if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
            return uri.getPath();
        }

        try {
            ContentResolver resolver = context.getContentResolver();
            String[] proj = new String[] { MediaStore.Images.Media.DATA };
            Cursor cursor = MediaStore.Images.Media.query(resolver, uri, proj);
            String realPath = null;
            if (cursor != null) {
                int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                if (cursor.getCount() > 0 && cursor.moveToFirst()) {
                    realPath = cursor.getString(columnIndex);
                }
                cursor.close();
            }
            return realPath;
        } catch (Exception e) {
            return uri.getPath();
        }
    }

    public static String getString(Context context, String key, String def) {
        return context.getSharedPreferences("MeiqiaSDK", Context.MODE_PRIVATE).getString(key, def);
    }
}