Example usage for android.os ParcelFileDescriptor getStatSize

List of usage examples for android.os ParcelFileDescriptor getStatSize

Introduction

In this page you can find the example usage for android.os ParcelFileDescriptor getStatSize.

Prototype

public long getStatSize() 

Source Link

Document

Return the total size of the file representing this fd, as determined by stat() .

Usage

From source file:Main.java

public static long getFileSize(Context context, Uri uri) {
    try {//w w w  . j a  v  a 2  s .c o m
        ParcelFileDescriptor fd = context.getContentResolver().openFileDescriptor(uri, "r");
        long size = fd.getStatSize();
        fd.close();
        return size;
    } catch (Throwable exception) {
        return 0;
    }
}

From source file:com.android.bluetooth.map.BluetoothMapContent.java

private void setSize(BluetoothMapMessageListingElement e, Cursor c, FilterInfo fi, BluetoothMapAppParams ap) {
    if ((ap.getParameterMask() & MASK_SIZE) != 0) {
        int size = 0;
        if (fi.msgType == FilterInfo.TYPE_SMS) {
            String subject = c.getString(c.getColumnIndex(Sms.BODY));
            size = subject.length();//from w w  w .  ja  v a2  s .  com
        } else if (fi.msgType == FilterInfo.TYPE_MMS) {
            size = c.getInt(c.getColumnIndex(Mms.MESSAGE_SIZE));
        } else if (fi.msgType == FilterInfo.TYPE_EMAIL) {
            long msgId = Long.valueOf(c.getString(c.getColumnIndex("_id")));
            String textContent, htmlContent;
            Uri uriAddress = Uri.parse("content://com.android.email.provider/body");
            Cursor cr = mResolver.query(uriAddress, Body.CONTENT_PROJECTION, BodyColumns.MESSAGE_KEY + "=?",
                    new String[] { String.valueOf(msgId) }, null);
            if (cr != null && cr.moveToFirst()) {
                ParcelFileDescriptor fd = null;
                String textContentURI = cr.getString(cr.getColumnIndex(BodyColumns.TEXT_CONTENT_URI));
                if (textContentURI != null) {
                    try {
                        Log.v(TAG, " TRY EMAIL BODY textURI " + textContentURI);
                        fd = mResolver.openFileDescriptor(Uri.parse(textContentURI), "r");
                    } catch (FileNotFoundException ex) {
                        if (V)
                            Log.w(TAG, ex);
                    }
                }
                if (fd == null) {
                    String htmlContentURI = cr.getString(cr.getColumnIndex(BodyColumns.HTML_CONTENT_URI));
                    if (htmlContentURI != null) {
                        try {
                            Log.v(TAG, " TRY EMAIL BODY htmlURI " + htmlContentURI);
                            fd = mResolver.openFileDescriptor(Uri.parse(htmlContentURI), "r");
                        } catch (FileNotFoundException ex) {
                            if (V)
                                Log.w(TAG, ex);
                        }
                    }
                }
                if (fd != null) {
                    //Size in bytes
                    size = (Long.valueOf(fd.getStatSize()).intValue());
                    try {
                        fd.close();
                    } catch (IOException ex) {
                        if (V)
                            Log.w(TAG, ex);
                    }

                } else {
                    Log.e(TAG, "MessageSize Email NOT AVAILABLE");
                }
                cr.close();
            }
        }
        if (D)
            Log.d(TAG, "setSize: " + size);
        e.setSize(size);
    }
}