Example usage for android.content ContentResolver SCHEME_FILE

List of usage examples for android.content ContentResolver SCHEME_FILE

Introduction

In this page you can find the example usage for android.content ContentResolver SCHEME_FILE.

Prototype

String SCHEME_FILE

To view the source code for android.content ContentResolver SCHEME_FILE.

Click Source Link

Usage

From source file:com.ocp.media.UriTexture.java

public static final Bitmap createFromUri(Context context, String uri, int maxResolutionX, int maxResolutionY,
        long cacheId, ClientConnectionManager connectionManager)
        throws IOException, URISyntaxException, OutOfMemoryError {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;//from ww w . j a  v  a  2  s  .com
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inDither = true;
    long crc64 = 0;
    Bitmap bitmap = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT)) {
        // We need the filepath for the given content uri
        crc64 = cacheId;
    } else {
        crc64 = Utils.Crc64Long(uri);
    }
    bitmap = createFromCache(crc64, maxResolutionX);
    if (bitmap != null) {
        return bitmap;
    }
    final boolean local = uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith("file://");

    // Get the input stream for computing the sample size.
    BufferedInputStream bufferedInput = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) {
        // Get the stream from a local file.
        bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)),
                16384);
    } else {
        // Get the stream from a remote URL.
        bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager);
    }

    // Compute the sample size, i.e., not decoding real pixels.
    if (bufferedInput != null) {
        options.inSampleSize = computeSampleSize(bufferedInput, maxResolutionX, maxResolutionY);
    } else {
        return null;
    }

    // Get the input stream again for decoding it to a bitmap.
    bufferedInput = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) {
        // Get the stream from a local file.
        bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)),
                16384);
    } else {
        // Get the stream from a remote URL.
        bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager);
    }

    // Decode bufferedInput to a bitmap.
    if (bufferedInput != null) {
        options.inDither = false;
        options.inJustDecodeBounds = false;
        Thread timeoutThread = new Thread("BitmapTimeoutThread") {
            public void run() {
                try {
                    Thread.sleep(6000);
                    options.requestCancelDecode();
                } catch (InterruptedException e) {
                }
            }
        };
        timeoutThread.start();

        bitmap = BitmapFactory.decodeStream(bufferedInput, null, options);
    }

    if ((options.inSampleSize > 1 || !local) && bitmap != null) {
        writeToCache(crc64, bitmap, maxResolutionX / options.inSampleSize);
    }
    return bitmap;
}

From source file:prince.app.sphotos.Gallery.UriTexture.java

public static final Bitmap createFromUri(Context context, String uri, int maxResolutionX, int maxResolutionY,
        long cacheId, ClientConnectionManager connectionManager)
        throws IOException, URISyntaxException, OutOfMemoryError {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;/* www.j  a va2  s. c  o  m*/
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inDither = true;
    long crc64 = 0;
    Bitmap bitmap = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT)) {
        // We need the filepath for the given content uri
        crc64 = cacheId;
    } else {
        crc64 = Utils.Crc64Long(uri);
    }

    bitmap = createFromCache(crc64, maxResolutionX);
    if (bitmap != null) {
        Log.i(TAG, "!!! Bitmap in cache !!!");
        return bitmap;
    }

    final boolean local = uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith("file://");

    // Get the input stream for computing the sample size.
    BufferedInputStream bufferedInput = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) {
        // Get the stream from a local file.
        bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)),
                16384);
    } else {
        // Get the stream from a remote URL.
        bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager);
    }

    // Compute the sample size, i.e., not decoding real pixels.
    if (bufferedInput != null) {
        options.inSampleSize = computeSampleSize(bufferedInput, maxResolutionX, maxResolutionY);
    } else {
        return null;
    }

    // Get the input stream again for decoding it to a bitmap.
    bufferedInput = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) {
        // Get the stream from a local file.
        bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)),
                16384);
    } else {
        // Get the stream from a remote URL.
        bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager);
    }

    // Decode bufferedInput to a bitmap.
    if (bufferedInput != null) {
        options.inDither = false;
        options.inJustDecodeBounds = false;
        Thread timeoutThread = new Thread("BitmapTimeoutThread") {
            public void run() {
                try {
                    Thread.sleep(6000);
                    options.requestCancelDecode();
                } catch (InterruptedException e) {
                }
            }
        };
        timeoutThread.start();

        bitmap = BitmapFactory.decodeStream(bufferedInput, null, options);
    }

    if ((options.inSampleSize > 1 || !local) && bitmap != null) {
        //   writeToCache(crc64, bitmap, maxResolutionX / options.inSampleSize);
        writeToCache(crc64, bitmap, maxResolutionX);
    }
    return bitmap;
}

From source file:vn.mbm.phimp.me.gallery3d.media.UriTexture.java

public static final Bitmap createFromUri(Context context, String uri, int maxResolutionX, int maxResolutionY,
        long cacheId, ClientConnectionManager connectionManager)
        throws IOException, URISyntaxException, OutOfMemoryError {
    Log.i(TAG, "creatFromUri()");
    Log.i(TAG, "URI: " + uri);
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;/*w  ww .  jav  a2s  .c  o  m*/
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inDither = true;
    long crc64 = 0;
    Bitmap bitmap = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT)) {
        // We need the filepath for the given content uri
        crc64 = cacheId;
    } else {
        crc64 = Utils.Crc64Long(uri);
    }
    bitmap = createFromCache(crc64, maxResolutionX);
    if (bitmap != null) {
        return bitmap;
    }
    final boolean local = uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith("file://");

    // Get the input stream for computing the sample size.
    BufferedInputStream bufferedInput = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) {
        // Get the stream from a local file.
        bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)),
                16384);
    } else {
        // Get the stream from a remote URL.
        bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager);
    }

    // Compute the sample size, i.e., not decoding real pixels.
    if (bufferedInput != null) {
        options.inSampleSize = computeSampleSize(bufferedInput, maxResolutionX, maxResolutionY);
    } else {
        return null;
    }

    // Get the input stream again for decoding it to a bitmap.
    bufferedInput = null;
    if (uri.startsWith(ContentResolver.SCHEME_CONTENT) || uri.startsWith(ContentResolver.SCHEME_FILE)) {
        // Get the stream from a local file.
        bufferedInput = new BufferedInputStream(context.getContentResolver().openInputStream(Uri.parse(uri)),
                16384);
    } else {
        // Get the stream from a remote URL.
        bufferedInput = createInputStreamFromRemoteUrl(uri, connectionManager);
    }

    // Decode bufferedInput to a bitmap.
    if (bufferedInput != null) {
        options.inDither = false;
        options.inJustDecodeBounds = false;
        Thread timeoutThread = new Thread("BitmapTimeoutThread") {
            public void run() {
                try {
                    Thread.sleep(6000);
                    options.requestCancelDecode();
                } catch (InterruptedException e) {
                }
            }
        };
        timeoutThread.start();

        bitmap = BitmapFactory.decodeStream(bufferedInput, null, options);
    }

    if ((options.inSampleSize > 1 || !local) && bitmap != null) {
        writeToCache(crc64, bitmap, maxResolutionX / options.inSampleSize);
    }
    return bitmap;
}

From source file:org.mariotaku.twidere.loader.AbsImageLoader.java

@Override
public AbsImageLoader.Result loadInBackground() {
    if (mUri == null)
        return new Result(null, null, null);
    final String scheme = mUri.getScheme();
    if ("http".equals(scheme) || "https".equals(scheme)) {
        final String url = ParseUtils.parseString(mUri.toString());
        if (url == null)
            return new Result(null, null, null);
        if (mCacheDir == null || !mCacheDir.exists()) {
            init();//w  ww. ja  v a 2  s. c  o m
        }
        final File cache_file = mImageFile = new File(mCacheDir, mGenerator.generate(url));
        try {
            // from SD cache
            if (ImageValidator.checkImageValidity(cache_file))
                return decodeImageInternal(cache_file);
            final HttpResponse resp = getRedirectedHttpResponse(mClient, url);
            // from web
            if (resp == null)
                return null;
            final long length = resp.getContentLength();
            mHandler.post(new DownloadStartRunnable(this, mListener, length));
            final InputStream is = resp.asStream();
            final OutputStream os = new FileOutputStream(cache_file);
            try {
                dump(is, os);
                mHandler.post(new DownloadFinishRunnable(this, mListener));
            } finally {
                GalleryUtils.closeSilently(is);
                GalleryUtils.closeSilently(os);
            }
            if (!ImageValidator.checkImageValidity(cache_file)) {
                // The file is corrupted, so we remove it from
                // cache.
                if (cache_file.isFile()) {
                    cache_file.delete();
                }
                throw new IOException("Invalid image");
            }
            return decodeImageInternal(cache_file);
        } catch (final Exception e) {
            mHandler.post(new DownloadErrorRunnable(this, mListener, e));
            return new Result(null, null, e);
        }
    } else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
        mImageFile = new File(mUri.getPath());
        try {
            return decodeImage(mImageFile);
        } catch (final Exception e) {
            return new Result(null, null, e);
        }
    }
    return new Result(null, null, null);
}

From source file:org.mariotaku.twidere.loader.AbstractImageLoader.java

@Override
public AbstractImageLoader.Result loadInBackground() {
    if (mUri == null)
        return new Result(null, null, null);
    final String scheme = mUri.getScheme();
    if ("http".equals(scheme) || "https".equals(scheme)) {
        final String url = ParseUtils.parseString(mUri.toString());
        if (url == null)
            return new Result(null, null, null);
        if (mCacheDir == null || !mCacheDir.exists()) {
            init();/* www  . j a va 2  s .c om*/
        }
        final File cache_file = mImageFile = new File(mCacheDir, mGenerator.generate(url));
        try {
            // from SD cache
            if (ImageValidator.checkImageValidity(cache_file))
                return decodeImage(cache_file);
            final HttpResponse resp = getRedirectedHttpResponse(mClient, url);
            // from web
            if (resp == null)
                return null;
            final long length = resp.getContentLength();
            mHandler.post(new DownloadStartRunnable(this, mListener, length));
            final InputStream is = resp.asStream();
            final OutputStream os = new FileOutputStream(cache_file);
            try {
                dump(is, os);
                mHandler.post(new DownloadFinishRunnable(this, mListener));
            } finally {
                GalleryUtils.closeSilently(is);
                GalleryUtils.closeSilently(os);
            }
            if (!ImageValidator.checkImageValidity(cache_file)) {
                // The file is corrupted, so we remove it from
                // cache.
                if (cache_file.isFile()) {
                    cache_file.delete();
                }
                throw new IOException("Invalid image");
            }
            return decodeImage(cache_file);
        } catch (final Exception e) {
            mHandler.post(new DownloadErrorRunnable(this, mListener, e));
            return new Result(null, null, e);
        }
    } else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
        mImageFile = new File(mUri.getPath());
        try {
            return decodeImage(mUri);
        } catch (final Exception e) {
            return new Result(null, null, e);
        }
    }
    return new Result(null, null, null);
}

From source file:org.mariotaku.gallery3d.GLImageLoader.java

@Override
public GLImageLoader.Result loadInBackground() {
    if (mUri == null) {
        Result.nullInstance();/* ww  w  .  j  a  va2 s .c om*/
    }
    final String scheme = mUri.getScheme();
    if ("http".equals(scheme) || "https".equals(scheme)) {
        final String url = ParseUtils.parseString(mUri.toString());
        if (url == null)
            return Result.nullInstance();
        final File cacheFile = mDiscCache.get(url);
        if (cacheFile != null) {
            final File cacheDir = cacheFile.getParentFile();
            if (cacheDir != null && !cacheDir.exists()) {
                cacheDir.mkdirs();
            }
        } else
            return Result.nullInstance();
        try {
            // from SD cache
            if (ImageValidator.checkImageValidity(cacheFile))
                return decodeImageInternal(cacheFile);

            final InputStream is = mDownloader.getStream(url, null);
            if (is == null)
                return Result.nullInstance();
            final long length = is.available();
            mHandler.post(new DownloadStartRunnable(this, mListener, length));
            final OutputStream os = new FileOutputStream(cacheFile);
            try {
                dump(is, os);
                mHandler.post(new DownloadFinishRunnable(this, mListener));
            } finally {
                GalleryUtils.closeSilently(is);
                GalleryUtils.closeSilently(os);
            }
            if (!ImageValidator.checkImageValidity(cacheFile)) {
                // The file is corrupted, so we remove it from
                // cache.
                final Result result = decodeBitmapOnly(cacheFile);
                if (cacheFile.isFile()) {
                    cacheFile.delete();
                }
                return result;
            }
            return decodeImageInternal(cacheFile);
        } catch (final Exception e) {
            mHandler.post(new DownloadErrorRunnable(this, mListener, e));
            return Result.getInstance(cacheFile, e);
        }
    } else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
        final File file = new File(mUri.getPath());
        try {
            return decodeImage(file);
        } catch (final Exception e) {
            return Result.getInstance(file, e);
        }
    }
    return Result.nullInstance();
}

From source file:de.vanita5.twittnuker.loader.support.TileImageLoader.java

@Override
public TileImageLoader.Result loadInBackground() {
    if (mUri == null) {
        return Result.nullInstance();
    }//from  ww  w. ja va  2  s .  c  o  m
    final String scheme = mUri.getScheme();
    if ("http".equals(scheme) || "https".equals(scheme)) {
        final String url = ParseUtils.parseString(mUri.toString());
        if (url == null)
            return Result.nullInstance();
        final File cacheFile = mDiskCache.get(url);
        if (cacheFile != null) {
            final File cacheDir = cacheFile.getParentFile();
            if (cacheDir != null && !cacheDir.exists()) {
                cacheDir.mkdirs();
            }
        } else
            return Result.nullInstance();
        try {
            // from SD cache
            if (ImageValidator.checkImageValidity(cacheFile))
                return decodeBitmapOnly(cacheFile, true);

            final InputStream is = mDownloader.getStream(url, new AccountExtra(mAccountId));
            if (is == null)
                return Result.nullInstance();
            final long length = is.available();
            mHandler.post(new DownloadStartRunnable(this, mListener, length));
            final OutputStream os = new FileOutputStream(cacheFile);
            try {
                dump(is, os);
                mHandler.post(new DownloadFinishRunnable(this, mListener));
            } finally {
                IoUtils.closeSilently(is);
                IoUtils.closeSilently(os);
            }
            if (!ImageValidator.checkImageValidity(cacheFile)) {
                // The file is corrupted, so we remove it from
                // cache.
                final Result result = decodeBitmapOnly(cacheFile, false);
                if (cacheFile.isFile()) {
                    cacheFile.delete();
                }
                return result;
            }
            return decodeBitmapOnly(cacheFile, true);
        } catch (final Exception e) {
            mHandler.post(new DownloadErrorRunnable(this, mListener, e));
            return Result.getInstance(cacheFile, e);
        }
    } else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
        final File file = new File(mUri.getPath());
        try {
            return decodeBitmapOnly(file, true);
        } catch (final Exception e) {
            return Result.getInstance(file, e);
        }
    }
    return Result.nullInstance();
}

From source file:org.getlantern.firetweet.loader.support.TileImageLoader.java

@Override
public TileImageLoader.Result loadInBackground() {
    if (mUri == null) {
        return Result.nullInstance();
    }//from  w  w  w  . ja  va 2  s .co m
    final String scheme = mUri.getScheme();
    if ("http".equals(scheme) || "https".equals(scheme)) {
        final String url = mUri.toString();
        if (url == null)
            return Result.nullInstance();
        final File cacheFile = mDiskCache.get(url);
        if (cacheFile != null) {
            final File cacheDir = cacheFile.getParentFile();
            if (cacheDir != null && !cacheDir.exists()) {
                cacheDir.mkdirs();
            }
        } else
            return Result.nullInstance();
        try {
            // from SD cache
            final int cachedValidity = ImageValidator.checkImageValidity(cacheFile);
            if (ImageValidator.isValid(cachedValidity)) {
                // The file is corrupted, so we remove it from
                // cache.
                return decodeBitmapOnly(cacheFile, ImageValidator.isValidForRegionDecoder(cachedValidity));
            }
            final InputStream is = mDownloader.getStream(url, new AccountExtra(mAccountId));
            if (is == null)
                return Result.nullInstance();
            final long length = is.available();
            mHandler.post(new DownloadStartRunnable(this, mListener, length));
            final OutputStream os = new FileOutputStream(cacheFile);
            try {
                dump(is, os);
                mHandler.post(new DownloadFinishRunnable(this, mListener));
            } finally {
                IoUtils.closeSilently(is);
                IoUtils.closeSilently(os);
            }
            final int downloadedValidity = ImageValidator.checkImageValidity(cacheFile);
            if (ImageValidator.isValid(downloadedValidity)) {
                // The file is corrupted, so we remove it from
                // cache.
                return decodeBitmapOnly(cacheFile, ImageValidator.isValidForRegionDecoder(downloadedValidity));
            } else {
                cacheFile.delete();
                throw new IOException();
            }
        } catch (final Exception e) {
            mHandler.post(new DownloadErrorRunnable(this, mListener, e));
            return Result.getInstance(cacheFile, e);
        }
    } else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
        final File file = new File(mUri.getPath());
        try {
            return decodeBitmapOnly(file,
                    ImageValidator.isValidForRegionDecoder(ImageValidator.checkImageValidity(file)));
        } catch (final Exception e) {
            return Result.getInstance(file, e);
        }
    }
    return Result.nullInstance();
}

From source file:com.ubuntuone.android.files.activity.GalleryActivity.java

@Override
public void onDownloadSuccess(String key, String path) {
    if (path.startsWith(ContentResolver.SCHEME_FILE)) {
        path = path.substring(7); // "file://".length()
    }/*from w ww.  j a v  a  2  s. c o  m*/
    galleryFragment.onDownloadSuccess(key, path);
}

From source file:org.sufficientlysecure.keychain.util.FileHelper.java

public static long getFileSize(Context context, Uri uri, long def) {
    if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
        long size = new File(uri.getPath()).length();
        if (size == 0) {
            size = def;//from   www  .  j  a v  a2 s  . co m
        }
        return size;
    }

    long size = def;
    try {
        Cursor cursor = context.getContentResolver().query(uri, new String[] { OpenableColumns.SIZE }, null,
                null, null);

        if (cursor != null) {
            if (cursor.moveToNext()) {
                size = cursor.getLong(0);
            }
            cursor.close();
        }
    } catch (Exception ignored) {
        // This happens in rare cases (eg: document deleted since selection) and should not cause a failure
    }
    return size;
}