Example usage for android.os ParcelFileDescriptor MODE_READ_ONLY

List of usage examples for android.os ParcelFileDescriptor MODE_READ_ONLY

Introduction

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

Prototype

int MODE_READ_ONLY

To view the source code for android.os ParcelFileDescriptor MODE_READ_ONLY.

Click Source Link

Document

For use with #open : open the file with read-only access.

Usage

From source file:com.adguard.android.contentblocker.FiltersContentProvider.java

@Override
public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException {
    ParcelFileDescriptor parcel = ParcelFileDescriptor.open(new File(filtersPath),
            ParcelFileDescriptor.MODE_READ_ONLY);
    Log.i("FiltersContentProvider", "Browser opened filters...");
    PreferencesService preferencesService = ServiceLocator.getInstance(getContext().getApplicationContext())
            .getPreferencesService();//from  w  w  w  . j a  v  a 2s. c o  m
    preferencesService.incBrowserConnectedCount();
    return parcel;
}

From source file:com.franctan.pdfviewpager.library.adapter.PDFPagerAdapter.java

protected ParcelFileDescriptor getSeekableFileDescriptor(String path) throws IOException {
    ParcelFileDescriptor pfd;/*  w ww.  java  2 s.c o  m*/

    File pdfCopy = new File(path);
    if (pdfCopy.exists()) {
        pfd = ParcelFileDescriptor.open(pdfCopy, ParcelFileDescriptor.MODE_READ_ONLY);
        return pfd;
    }

    if (isAnAsset(path)) {
        pdfCopy = new File(context.getCacheDir(), path);
        pfd = ParcelFileDescriptor.open(pdfCopy, ParcelFileDescriptor.MODE_READ_ONLY);
    } else {
        URI uri = URI.create(String.format("file://%s", path));
        pfd = context.getContentResolver().openFileDescriptor(Uri.parse(uri.toString()), "rw");
    }

    return pfd;
}

From source file:com.github.barteksc.pdfviewpager.adapter.PDFPagerAdapter.java

protected FileDescriptor getSeekableFileDescriptor(String path) throws IOException {
    ParcelFileDescriptor pfd;/*  w w w  . ja va2 s  .  c  o m*/

    File pdfCopy = new File(path);
    if (pdfCopy.exists()) {
        pfd = ParcelFileDescriptor.open(pdfCopy, ParcelFileDescriptor.MODE_READ_ONLY);
        return pfd.getFileDescriptor();
    }

    if (isAnAsset(path)) {
        pdfCopy = new File(context.getCacheDir(), path);
        pfd = ParcelFileDescriptor.open(pdfCopy, ParcelFileDescriptor.MODE_READ_ONLY);
    } else {
        URI uri = URI.create(String.format("file://%s", path));
        pfd = context.getContentResolver().openFileDescriptor(Uri.parse(uri.toString()), "rw");
    }

    if (pfd == null) {
        throw new IOException("Cannot get FileDescriptor for " + path);
    }

    return pfd.getFileDescriptor();
}

From source file:com.epubtest.hxfy.epubtest.BasePDFPagerAdapter.java

protected ParcelFileDescriptor getSeekableFileDescriptor(String path) throws IOException {
    ParcelFileDescriptor parcelFileDescriptor;

    File pdfCopy = new File(path);

    if (pdfCopy.exists()) {
        parcelFileDescriptor = ParcelFileDescriptor.open(pdfCopy, ParcelFileDescriptor.MODE_READ_ONLY);
        return parcelFileDescriptor;
    }/*  w  ww .j av  a 2s .  c  o  m*/

    if (isAnAsset(path)) {
        pdfCopy = new File(context.getCacheDir(), path);
        parcelFileDescriptor = ParcelFileDescriptor.open(pdfCopy, ParcelFileDescriptor.MODE_READ_ONLY);
    } else {
        URI uri = URI.create(String.format("file://%s", path));
        parcelFileDescriptor = context.getContentResolver().openFileDescriptor(Uri.parse(uri.toString()), "rw");
    }

    return parcelFileDescriptor;
}

From source file:fr.simon.marquis.secretcodes.util.ExportContentProvider.java

@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
    if (!CONTENT_URI.equals(uri)) {
        throw new FileNotFoundException(uri.getPath());
    }/*from  w w  w .  ja  v  a  2s  . c  om*/

    ArrayList<SecretCode> secretCodes = Utils.getSecretCodes(getContext());

    deletePreviousData();
    saveJsonFile(secretCodes);
    saveImageFiles(secretCodes);
    saveZipFile();

    File zipFile = new File(getContext().getFilesDir(), ZIP_FILE_NAME);
    if (zipFile.exists()) {
        return ParcelFileDescriptor.open(zipFile, ParcelFileDescriptor.MODE_READ_ONLY);
    }
    throw new FileNotFoundException(uri.getPath());
}

From source file:com.android.gallery3d.data.UriImage.java

private int openOrDownloadInner(JobContext jc) {
    String scheme = mUri.getScheme();
    if (ContentResolver.SCHEME_CONTENT.equals(scheme) || ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)
            || ContentResolver.SCHEME_FILE.equals(scheme)) {
        try {//  ww w .  j  a v  a 2 s .c  o m
            if (MIME_TYPE_JPEG.equalsIgnoreCase(mContentType)) {
                InputStream is = mApplication.getContentResolver().openInputStream(mUri);
                mRotation = Exif.getOrientation(is);
                Utils.closeSilently(is);
            }
            mFileDescriptor = mApplication.getContentResolver().openFileDescriptor(mUri, "r");
            if (jc.isCancelled())
                return STATE_INIT;
            return STATE_DOWNLOADED;
        } catch (FileNotFoundException e) {
            Log.w(TAG, "fail to open: " + mUri, e);
            return STATE_ERROR;
        }
    } else {
        try {
            URL url = new URI(mUri.toString()).toURL();
            mCacheEntry = mApplication.getDownloadCache().download(jc, url);
            if (jc.isCancelled())
                return STATE_INIT;
            if (mCacheEntry == null) {
                Log.w(TAG, "download failed " + url);
                return STATE_ERROR;
            }
            if (MIME_TYPE_JPEG.equalsIgnoreCase(mContentType)) {
                InputStream is = new FileInputStream(mCacheEntry.cacheFile);
                mRotation = Exif.getOrientation(is);
                Utils.closeSilently(is);
            }
            mFileDescriptor = ParcelFileDescriptor.open(mCacheEntry.cacheFile,
                    ParcelFileDescriptor.MODE_READ_ONLY);
            return STATE_DOWNLOADED;
        } catch (Throwable t) {
            Log.w(TAG, "download error", t);
            return STATE_ERROR;
        }
    }
}

From source file:net.sf.fakenames.fddemo.PermissionActivity.java

private String accessPattern(String access) {
    final int accessMode = ParcelFileDescriptor.parseMode(access);

    if ((accessMode & ParcelFileDescriptor.MODE_READ_ONLY) == accessMode) {
        return "read";
    }/*from  w w w  . j a  va  2 s . co  m*/

    if ((accessMode & ParcelFileDescriptor.MODE_WRITE_ONLY) == accessMode) {
        return "write";
    }

    return "full";
}

From source file:com.raspi.chatapp.util.storage.file.LocalStorageProvider.java

@Override
public AssetFileDescriptor openDocumentThumbnail(final String documentId, final Point sizeHint,
        final CancellationSignal signal) throws FileNotFoundException {
    if (LocalStorageProvider.isMissingPermission(getContext())) {
        return null;
    }// w  w w .j  a v a2 s. c o  m
    // Assume documentId points to an image file. Build a thumbnail no larger than twice the sizeHint
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(documentId, options);
    final int targetHeight = 2 * sizeHint.y;
    final int targetWidth = 2 * sizeHint.x;
    final int height = options.outHeight;
    final int width = options.outWidth;
    options.inSampleSize = 1;
    if (height > targetHeight || width > targetWidth) {
        final int halfHeight = height / 2;
        final int halfWidth = width / 2;
        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / options.inSampleSize) > targetHeight
                || (halfWidth / options.inSampleSize) > targetWidth) {
            options.inSampleSize *= 2;
        }
    }
    options.inJustDecodeBounds = false;
    Bitmap bitmap = BitmapFactory.decodeFile(documentId, options);
    // Write out the thumbnail to a temporary file
    File tempFile = null;
    FileOutputStream out = null;
    try {
        tempFile = File.createTempFile("thumbnail", null, getContext().getCacheDir());
        out = new FileOutputStream(tempFile);
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
    } catch (IOException e) {
        Log.e(LocalStorageProvider.class.getSimpleName(), "Error writing thumbnail", e);
        return null;
    } finally {
        if (out != null)
            try {
                out.close();
            } catch (IOException e) {
                Log.e(LocalStorageProvider.class.getSimpleName(), "Error closing thumbnail", e);
            }
    }
    // It appears the Storage Framework UI caches these results quite aggressively so there is little reason to
    // write your own caching layer beyond what you need to return a single AssetFileDescriptor
    return new AssetFileDescriptor(ParcelFileDescriptor.open(tempFile, ParcelFileDescriptor.MODE_READ_ONLY), 0,
            AssetFileDescriptor.UNKNOWN_LENGTH);
}

From source file:com.raspi.chatapp.util.storage.file.LocalStorageProvider.java

@Override
public ParcelFileDescriptor openDocument(final String documentId, final String mode,
        final CancellationSignal signal) throws FileNotFoundException {
    if (LocalStorageProvider.isMissingPermission(getContext())) {
        return null;
    }//from   ww w  .j  a  va  2 s.c o m
    File file = new File(documentId);
    final boolean isWrite = (mode.indexOf('w') != -1);
    if (isWrite) {
        return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
    } else {
        return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    }
}

From source file:de.tubs.ibr.dtn.dtalkie.service.TalkieService.java

@Override
protected void onHandleIntent(Intent intent) {
    String action = intent.getAction();

    if (de.tubs.ibr.dtn.Intent.RECEIVE.equals(action)) {
        try {//from   w w w. j a va  2  s.  co m
            while (mClient.getSession().queryNext())
                ;
        } catch (Exception e) {
        }
        ;
    } else if (ACTION_MARK_DELIVERED.equals(action)) {
        final BundleID received = intent.getParcelableExtra("bundleid");
        try {
            mClient.getSession().delivered(received);
        } catch (Exception e) {
            Log.e(TAG, "Can not mark bundle as delivered.", e);
        }
    } else if (ACTION_PLAY.equals(action)) {
        Folder f = Folder.valueOf(intent.getStringExtra("folder"));
        Long msgid = intent.getLongExtra("message", 0L);

        // unmark the message
        mDatabase.mark(f, msgid, false);

        try {
            // prepare player
            Message msg = mDatabase.get(f, msgid);
            mPlayer.setDataSource(msg.getFile().getAbsolutePath());
            mPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
            mPlayer.prepareAsync();

            synchronized (mPlayerLock) {
                // set the player to playing
                mPlaying = true;

                // wait until the play-back is done
                while (mPlaying) {
                    mPlayerLock.wait();
                }
            }
        } catch (InterruptedException e) {
            Log.e(TAG, null, e);
        } catch (IOException e) {
            Log.e(TAG, null, e);
        }

        // mark the message as played
        mDatabase.mark(f, msgid, true);

        // remove notification is there are no more pending
        // messages
        removeNotification();
    } else if (ACTION_PLAY_NEXT.equals(action)) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(TalkieService.this);
        if (prefs.getBoolean("autoplay", false) || HeadsetService.ENABLED) {
            Message next = mDatabase.nextMarked(Folder.INBOX, false);

            if (next != null) {
                Intent play_i = new Intent(TalkieService.this, TalkieService.class);
                play_i.setAction(TalkieService.ACTION_PLAY);
                play_i.putExtra("folder", Folder.INBOX.toString());
                play_i.putExtra("message", next.getId());
                startService(play_i);
            }
        }
    } else if (ACTION_RECORDED.equals(action)) {
        File recfile = (File) intent.getSerializableExtra("recfile");

        // create a new bundle
        Bundle b = new Bundle();

        // set destination
        b.setDestination((EID) intent.getSerializableExtra("destination"));

        // assign lifetime
        b.setLifetime(1800L);

        // request signing of the message
        b.set(ProcFlags.DTNSEC_REQUEST_SIGN, true);

        try {
            ParcelFileDescriptor fd = ParcelFileDescriptor.open(recfile, ParcelFileDescriptor.MODE_READ_ONLY);
            BundleID ret = mClient.getSession().send(b, fd);

            if (ret == null) {
                Log.e(TAG, "Recording sent failed");
            } else {
                Log.i(TAG, "Recording sent, BundleID: " + ret.toString());
            }

            recfile.delete();
        } catch (FileNotFoundException ex) {
            Log.e(TAG, "Can not open message file for transmission", ex);
        } catch (SessionDestroyedException ex) {
            Log.e(TAG, "DTN session has been destroyed.", ex);
        } catch (Exception e) {
            Log.e(TAG, "Unexpected exception while transmit message.", e);
        }
    } else if (ACTION_RECEIVED.equals(action)) {
        String source = intent.getStringExtra("source");
        String destination = intent.getStringExtra("destination");
        Date created = (Date) intent.getSerializableExtra("created");
        Date received = (Date) intent.getSerializableExtra("received");
        File file = (File) intent.getSerializableExtra("file");

        // add message to database
        Message msg = new Message(source, destination, file);
        msg.setCreated(created);
        msg.setReceived(received);
        mDatabase.put(Folder.INBOX, msg);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        if (prefs.getBoolean("notification", true)) {
            // create a notification for this message
            createNotification();
        }

        Intent play_i = new Intent(TalkieService.this, TalkieService.class);
        play_i.setAction(ACTION_PLAY_NEXT);
        startService(play_i);
    }
}