Example usage for android.provider OpenableColumns SIZE

List of usage examples for android.provider OpenableColumns SIZE

Introduction

In this page you can find the example usage for android.provider OpenableColumns SIZE.

Prototype

String SIZE

To view the source code for android.provider OpenableColumns SIZE.

Click Source Link

Document

The number of bytes in the file identified by the openable URI.

Usage

From source file:com.amytech.android.library.views.imagechooser.threads.MediaProcessorThread.java

public String checkExtension(Uri uri) {

    String extension = "";

    // The query, since it only applies to a single document, will only
    // return/*from w  ww .  ja  v a  2  s  .  c  om*/
    // one row. There's no need to filter, sort, or select fields, since we
    // want
    // all fields for one document.
    Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);

    try {
        // moveToFirst() returns false if the cursor has 0 rows. Very handy
        // for
        // "if there's anything to look at, look at it" conditionals.
        if (cursor != null && cursor.moveToFirst()) {

            // Note it's called "Display Name". This is
            // provider-specific, and might not necessarily be the file
            // name.
            String displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            int position = displayName.indexOf(".");
            extension = displayName.substring(position + 1);
            Log.i(TAG, "Display Name: " + displayName);

            int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
            // If the size is unknown, the value stored is null. But since
            // an
            // int can't be null in Java, the behavior is
            // implementation-specific,
            // which is just a fancy term for "unpredictable". So as
            // a rule, check if it's null before assigning to an int. This
            // will
            // happen often: The storage API allows for remote files, whose
            // size might not be locally known.
            String size = null;
            if (!cursor.isNull(sizeIndex)) {
                // Technically the column stores an int, but
                // cursor.getString()
                // will do the conversion automatically.
                size = cursor.getString(sizeIndex);
            } else {
                size = "Unknown";
            }
            Log.i(TAG, "Size: " + size);
        }
    } finally {
        cursor.close();
    }
    return extension;
}

From source file:com.hp.mss.printsdksample.fragment.TabFragmentPrintLayout.java

private void showFileInfo(Uri uri) {
    Cursor returnCursor = getActivity().getContentResolver().query(uri, null, null, null, null);

    int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
    int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
    returnCursor.moveToFirst();//from  w  ww .  j  a  va2 s  . c o m

    Toast.makeText(getActivity(), "File " + returnCursor.getString(nameIndex) + "("
            + Long.toString(returnCursor.getLong(sizeIndex)) + "0", Toast.LENGTH_LONG).show();

}

From source file:com.mattprecious.telescope.FileProvider.java

/**
 * Use a content URI returned by//  w w w.  j  a va  2s  . c  o  m
 * {@link #getUriForFile(Context, String, File) getUriForFile()} to get information about a file
 * managed by the FileProvider.
 * FileProvider reports the column names defined in {@link android.provider.OpenableColumns}:
 * <ul>
 * <li>{@link android.provider.OpenableColumns#DISPLAY_NAME}</li>
 * <li>{@link android.provider.OpenableColumns#SIZE}</li>
 * </ul>
 * For more information, see
 * {@link ContentProvider#query(Uri, String[], String, String[], String)
 * ContentProvider.query()}.
 *
 * @param uri A content URI returned by {@link #getUriForFile}.
 * @param projection The list of columns to put into the {@link Cursor}. If null all columns are
 * included.
 * @param selection Selection criteria to apply. If null then all data that matches the content
 * URI is returned.
 * @param selectionArgs An array of {@link java.lang.String}, containing arguments to bind to
 * the <i>selection</i> parameter. The <i>query</i> method scans <i>selection</i> from left to
 * right and iterates through <i>selectionArgs</i>, replacing the current "?" character in
 * <i>selection</i> with the value at the current position in <i>selectionArgs</i>. The
 * values are bound to <i>selection</i> as {@link java.lang.String} values.
 * @param sortOrder A {@link java.lang.String} containing the column name(s) on which to sort
 * the resulting {@link Cursor}.
 * @return A {@link Cursor} containing the results of the query.
 */
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    // ContentProvider has already checked granted permissions
    final File file = mStrategy.getFileForUri(uri);

    if (projection == null) {
        projection = COLUMNS;
    }

    String[] cols = new String[projection.length];
    Object[] values = new Object[projection.length];
    int i = 0;
    for (String col : projection) {
        if (OpenableColumns.DISPLAY_NAME.equals(col)) {
            cols[i] = OpenableColumns.DISPLAY_NAME;
            values[i++] = file.getName();
        } else if (OpenableColumns.SIZE.equals(col)) {
            cols[i] = OpenableColumns.SIZE;
            values[i++] = file.length();
        }
    }

    cols = copyOf(cols, i);
    values = copyOf(values, i);

    final MatrixCursor cursor = new MatrixCursor(cols, 1);
    cursor.addRow(values);
    return cursor;
}

From source file:com.navjagpal.fileshare.WebServer.java

private void addFileEntity(final Uri uri, HttpResponse response) throws IOException {
    if (mTransferStartedListener != null) {
        mTransferStartedListener.started(uri);
    }/*from w ww .j  av a2s  .co  m*/

    Cursor c = mContext.getContentResolver().query(uri, null, null, null, null);
    c.moveToFirst();
    int nameIndex = c.getColumnIndexOrThrow(FileSharingProvider.Files.Columns.DISPLAY_NAME);
    String name = c.getString(nameIndex);
    int dataIndex = c.getColumnIndexOrThrow(FileSharingProvider.Files.Columns._DATA);
    Uri data = Uri.parse(c.getString(dataIndex));

    c = mContext.getContentResolver().query(data, null, null, null, null);
    c.moveToFirst();
    int sizeIndex = c.getColumnIndexOrThrow(OpenableColumns.SIZE);
    int sizeBytes = c.getInt(sizeIndex);
    c.close();

    InputStream input = mContext.getContentResolver().openInputStream(data);

    String contentType = "application/octet-stream";
    if (name.endsWith(".jpg")) {
        contentType = "image/jpg";
    }

    response.addHeader("Content-Type", contentType);
    response.addHeader("Content-Length", "" + sizeBytes);
    response.setEntity(new InputStreamEntity(input, sizeBytes));
}

From source file:com.wiret.arbrowser.AbstractArchitectCamActivity.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == KmlFileBrowserActivity.FILE_SELECT_CODE) {
        if (resultCode == RESULT_OK) {
            try {
                String filename = "";
                Uri uri = data.getData();

                if (false) {
                    Toast.makeText(this,
                            "The selected file is too large. Selet a new file with size less than 2mb",
                            Toast.LENGTH_LONG).show();
                } else {
                    String mimeType = getContentResolver().getType(uri);
                    if (mimeType == null) {
                        String path = getPath(this, uri);
                        if (path == null) {
                            filename = FilenameUtils.getName(uri.toString());
                        } else {
                            File file = new File(path);
                            filename = file.getName();
                        }/*from   ww w.  j  a  va2 s . c  o  m*/
                    } else {
                        Uri returnUri = data.getData();
                        Cursor returnCursor = getContentResolver().query(returnUri, null, null, null, null);
                        int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                        int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
                        returnCursor.moveToFirst();
                        filename = returnCursor.getString(nameIndex);
                        String size = Long.toString(returnCursor.getLong(sizeIndex));
                    }
                    String sourcePath = getExternalFilesDir(null).toString();
                    try {
                        File outputPath = new File(sourcePath + "/" + filename);
                        copyFileStream(outputPath, uri, this);
                        importData(outputPath.getAbsolutePath());

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.applozic.mobicommons.file.FileUtils.java

public static String getSize(Context context, Uri uri) {

    String sizeInMB = null;//from  w ww. j a  v a2 s . co m
    Cursor returnCursor = context.getContentResolver().query(uri, null, null, null, null);

    if (returnCursor != null && returnCursor.moveToFirst()) {

        int columnIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
        Long fileSize = returnCursor.getLong(columnIndex);
        if (fileSize < 1024) {
            sizeInMB = (int) (fileSize / (1024 * 1024)) + " B";

        } else if (fileSize < 1024 * 1024) {
            sizeInMB = (int) (fileSize / (1024)) + " KB";
        } else {
            sizeInMB = (int) (fileSize / (1024 * 1024)) + " MB";
        }
    }

    return sizeInMB;
}

From source file:com.applozic.mobicommons.file.FileUtils.java

public static boolean isMaxUploadSizeReached(Context context, Uri uri, int maxFileSize) {
    try {/*from  w w w .  j a v  a2 s . c o m*/
        Cursor returnCursor = context.getContentResolver().query(uri, null, null, null, null);
        if (returnCursor != null) {
            int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
            returnCursor.moveToFirst();
            Long fileSize = returnCursor.getLong(sizeIndex);
            returnCursor.close();
            return fileSize > maxFileSize;
        }

    } catch (Exception e) {
    }
    return false;
}

From source file:com.xperia64.timidityae.TimidityActivity.java

public void handleIntentData(Intent in) {
    if (in.getData() != null) {
        String data;//  w  w w . ja  va 2s. c om
        if ((data = in.getData().getPath()) != null && in.getData().getScheme() != null) {
            if (in.getData().getScheme().equals("file")) {
                if (new File(data).exists()) {
                    File f = new File(data.substring(0, data.lastIndexOf('/') + 1));
                    if (f.exists()) {
                        if (f.isDirectory()) {
                            ArrayList<String> files = new ArrayList<String>();
                            int position = -1;
                            int goodCounter = 0;
                            for (File ff : f.listFiles()) {
                                if (ff != null && ff.isFile()) {
                                    int dotPosition = ff.getName().lastIndexOf('.');
                                    String extension = "";
                                    if (dotPosition != -1) {
                                        extension = (ff.getName().substring(dotPosition))
                                                .toLowerCase(Locale.US);
                                        if (extension != null) {
                                            if ((Globals.showVideos ? Globals.musicVideoFiles
                                                    : Globals.musicFiles).contains("*" + extension + "*")) {

                                                files.add(ff.getPath());
                                                if (ff.getPath().equals(data))
                                                    position = goodCounter;
                                                goodCounter++;
                                            }
                                        }
                                    }
                                }
                            }
                            if (position == -1)
                                Toast.makeText(this, getResources().getString(R.string.intErr1),
                                        Toast.LENGTH_SHORT).show();
                            else {
                                stop();
                                selectedSong(files, position, true, false, false);
                                fileFrag.getDir(data.substring(0, data.lastIndexOf('/') + 1));
                            }
                        }
                    }
                } else {
                    Toast.makeText(this, getResources().getString(R.string.srv_fnf), Toast.LENGTH_SHORT).show();
                }
            } else if (in.getData().getScheme().equals("http") || in.getData().getScheme().equals("https")) {
                if (!data.endsWith("/")) {
                    if (!Globals.getExternalCacheDir(this).exists()) {
                        Globals.getExternalCacheDir(this).mkdirs();
                    }
                    final Globals.DownloadTask downloadTask = new Globals.DownloadTask(this);
                    downloadTask.execute(in.getData().toString(), in.getData().getLastPathSegment());
                    in.setData(null);
                } else {
                    Toast.makeText(this, "This is a directory, not a file", Toast.LENGTH_SHORT).show();
                }
            } else if (in.getData().getScheme().equals("content") && (data.contains("downloads"))) {
                String filename = null;
                Cursor cursor = null;
                try {
                    cursor = this.getContentResolver().query(in.getData(),
                            new String[] { OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE }, null, null,
                            null);
                    if (cursor != null && cursor.moveToFirst()) {
                        filename = cursor.getString(0);
                    }
                } finally {
                    if (cursor != null)
                        cursor.close();
                }
                try {
                    InputStream input = getContentResolver().openInputStream(in.getData());
                    if (new File(Globals.getExternalCacheDir(this).getAbsolutePath() + '/' + filename)
                            .exists()) {
                        new File(Globals.getExternalCacheDir(this).getAbsolutePath() + '/' + filename).delete();
                    }
                    OutputStream output = new FileOutputStream(
                            Globals.getExternalCacheDir(this).getAbsolutePath() + '/' + filename);

                    byte[] buffer = new byte[4096];
                    int count;
                    while ((count = input.read(buffer)) != -1) {
                        output.write(buffer, 0, count);
                    }
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    return;
                }

                File f = new File(Globals.getExternalCacheDir(this).getAbsolutePath() + '/');
                if (f.exists()) {
                    if (f.isDirectory()) {
                        ArrayList<String> files = new ArrayList<String>();
                        int position = -1;
                        int goodCounter = 0;
                        for (File ff : f.listFiles()) {
                            if (ff != null && ff.isFile()) {
                                int dotPosition = ff.getName().lastIndexOf('.');
                                String extension = "";
                                if (dotPosition != -1) {
                                    extension = (ff.getName().substring(dotPosition)).toLowerCase(Locale.US);
                                    if (extension != null) {
                                        if ((Globals.showVideos ? Globals.musicVideoFiles : Globals.musicFiles)
                                                .contains("*" + extension + "*")) {

                                            files.add(ff.getPath());
                                            if (ff.getPath()
                                                    .equals(Globals.getExternalCacheDir(this).getAbsolutePath()
                                                            + '/' + filename))
                                                position = goodCounter;
                                            goodCounter++;
                                        }
                                    }
                                }
                            }
                        }
                        if (position == -1)
                            Toast.makeText(this, getResources().getString(R.string.intErr1), Toast.LENGTH_SHORT)
                                    .show();
                        else {
                            stop();
                            selectedSong(files, position, true, false, false);
                            fileFrag.getDir(Globals.getExternalCacheDir(this).getAbsolutePath());
                        }
                    }
                }

            } else {
                System.out.println(in.getDataString());
                Toast.makeText(this,
                        getResources().getString(R.string.intErr2) + " (" + in.getData().getScheme() + ")",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }
}

From source file:org.exoplatform.utils.ExoDocumentUtils.java

/**
 * Gets a DocumentInfo with info coming from the document at the given URI.
 * //from  www. j  a v  a2s.c om
 * @param contentUri the content URI of the document (content:// ...)
 * @param context
 * @return a DocumentInfo or null if an error occurs
 */
public static DocumentInfo documentFromContentUri(Uri contentUri, Context context) {
    if (contentUri == null)
        return null;

    try {
        ContentResolver cr = context.getContentResolver();
        Cursor c = cr.query(contentUri, null, null, null, null);
        int sizeIndex = c.getColumnIndex(OpenableColumns.SIZE);
        int nameIndex = c.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        int orientIndex = c.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION);
        c.moveToFirst();

        DocumentInfo document = new DocumentInfo();
        document.documentName = c.getString(nameIndex);
        document.documentSizeKb = c.getLong(sizeIndex) / 1024;
        document.documentData = cr.openInputStream(contentUri);
        document.documentMimeType = cr.getType(contentUri);
        if (orientIndex != -1) { // if found orientation column
            document.orientationAngle = c.getInt(orientIndex);
        }
        return document;
    } catch (FileNotFoundException e) {
        Log.d(LOG_TAG, e.getClass().getSimpleName(), e.getLocalizedMessage());
    } catch (Exception e) {
        Log.e(LOG_TAG, "Cannot retrieve the content at " + contentUri);
        if (Log.LOGD)
            Log.d(LOG_TAG, e.getMessage() + "\n" + Log.getStackTraceString(e));
    }
    return null;
}

From source file:com.keylesspalace.tusky.activity.ComposeActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == MEDIA_PICK_RESULT && resultCode == RESULT_OK && data != null) {
        Uri uri = data.getData();//w w w. j a  v  a2  s.co m
        long mediaSize;
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if (cursor != null) {
            int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
            cursor.moveToFirst();
            mediaSize = cursor.getLong(sizeIndex);
            cursor.close();
        } else {
            mediaSize = MEDIA_SIZE_UNKNOWN;
        }
        pickMedia(uri, mediaSize);
    }
}