Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

import android.net.Uri;
import android.provider.MediaStore;

public class Main {
    public static long uriFileSize(Context context, String contentUri) {
        long result = 0;
        Uri uri = Uri.parse(contentUri);
        if (!uri.getScheme().equals("content")) {
            return (fileUriFileSize(context, contentUri));
        }
        String[] p = { MediaStore.MediaColumns.SIZE };
        Cursor cursor = context.getContentResolver().query(uri, p, // which columns
                null, // which rows (all rows)
                null, // selection args (none)
                null); // order-by clause (ascending by name)
        if (cursor != null) {
            int iColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE);
            if (cursor.moveToFirst()) {
                result = cursor.getLong(iColumn);
            }
        }
        return (result);
    }

    private static long fileUriFileSize(Context context, String contentUri) {
        long result = 0;
        String[] p = { MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.SIZE };
        Uri uri = Uri.parse(contentUri);
        String path = uri.getPath();
        String last = Uri.parse(path).getLastPathSegment();
        Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, p, // which columns
                MediaStore.MediaColumns.DISPLAY_NAME + "='" + last + "'", // which rows
                null, // selection args (none)
                null); // order-by clause (ascending by name)
        if (cursor != null) {
            int scol = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE);
            if (cursor.moveToFirst()) {
                result = cursor.getLong(scol);
            }
        }
        return (result);
    }
}