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;

import java.io.File;

public class Main {
    /**
     * Return file size from Uri
     *
     * @param uri file URI
     * @return return file size
     */
    public static long getFileSizeFromUri(Context context, Uri uri) {
        long size = 0;
        if (uri.getScheme().toString().compareTo("content") == 0) {
            Cursor cursor = null;
            try {
                cursor = context.getApplicationContext().getContentResolver().query(uri, null, null, null, null);
                if (cursor != null && cursor.moveToFirst()) {
                    final int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE);
                    size = cursor.getInt(column_index);
                }
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
        } else if (uri.getScheme().toString().compareTo("file") == 0) {
            final File file = new File(uri.getPath());
            size = file.length();
        }
        return size;
    }
}