Example usage for android.os ParcelFileDescriptor close

List of usage examples for android.os ParcelFileDescriptor close

Introduction

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

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Close the ParcelFileDescriptor.

Usage

From source file:us.theparamountgroup.android.inventory.EditorActivity.java

private Bitmap getBitmapFromUri(Uri uri) {
    if (uri == null) {
        return null;
    }//  www.  j  a  va  2s .  c o m
    int targetW = mImageView.getWidth();
    int targetH = mImageView.getHeight();
    ParcelFileDescriptor parcelFileDescriptor = null;
    try {
        parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();

        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeFileDescriptor(fileDescriptor, null, opts);
        int photoW = opts.outWidth;
        int photoH = opts.outHeight;

        int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

        opts.inJustDecodeBounds = false;
        opts.inSampleSize = scaleFactor;
        opts.inPurgeable = true;
        Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, opts);
        return image;

    } catch (Exception e) {
        Log.e(LOG_TAG, "Failed to load image.", e);
        return null;
    } finally {
        try {
            if (parcelFileDescriptor != null) {
                parcelFileDescriptor.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(LOG_TAG, "Error closing ParcelFile Descriptor");
        }
    }
}

From source file:com.stfalcon.contentmanager.ContentManager.java

protected String getFileFromContentProvider(String uri) {

    BufferedInputStream inputStream = null;
    BufferedOutputStream outStream = null;
    ParcelFileDescriptor parcelFileDescriptor = null;
    try {/*from  w  ww  . ja  v  a  2s  . co  m*/
        String localFilePath = generateFileName(uri);
        parcelFileDescriptor = activity.getContentResolver().openFileDescriptor(Uri.parse(uri), "r");

        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();

        inputStream = new BufferedInputStream(new FileInputStream(fileDescriptor));
        BufferedInputStream reader = new BufferedInputStream(inputStream);

        outStream = new BufferedOutputStream(new FileOutputStream(localFilePath));
        byte[] buf = new byte[2048];
        int len;
        while ((len = reader.read(buf)) > 0) {
            outStream.write(buf, 0, len);
        }
        outStream.flush();
        uri = localFilePath;
    } catch (IOException e) {
        return uri;
    } finally {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            try {
                parcelFileDescriptor.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            outStream.flush();
            outStream.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return uri;
}

From source file:br.com.viniciuscr.notification2android.mediaPlayer.MusicUtils.java

private static Bitmap getArtworkQuick(Context context, long album_id, int w, int h) {
    // NOTE: There is in fact a 1 pixel border on the right side in the ImageView
    // used to display this drawable. Take it into account now, so we don't have to
    // scale later.
    w -= 1;//from   ww w.  ja v  a2 s  . co  m
    ContentResolver res = context.getContentResolver();
    Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
    if (uri != null) {
        ParcelFileDescriptor fd = null;
        try {
            fd = res.openFileDescriptor(uri, "r");
            int sampleSize = 1;

            // Compute the closest power-of-two scale factor
            // and pass that to sBitmapOptionsCache.inSampleSize, which will
            // result in faster decoding and better quality
            sBitmapOptionsCache.inJustDecodeBounds = true;
            BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, sBitmapOptionsCache);
            int nextWidth = sBitmapOptionsCache.outWidth >> 1;
            int nextHeight = sBitmapOptionsCache.outHeight >> 1;
            while (nextWidth > w && nextHeight > h) {
                sampleSize <<= 1;
                nextWidth >>= 1;
                nextHeight >>= 1;
            }

            sBitmapOptionsCache.inSampleSize = sampleSize;
            sBitmapOptionsCache.inJustDecodeBounds = false;
            Bitmap b = BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, sBitmapOptionsCache);

            if (b != null) {
                // finally rescale to exactly the size we need
                if (sBitmapOptionsCache.outWidth != w || sBitmapOptionsCache.outHeight != h) {
                    Bitmap tmp = Bitmap.createScaledBitmap(b, w, h, true);
                    // Bitmap.createScaledBitmap() can return the same bitmap
                    if (tmp != b)
                        b.recycle();
                    b = tmp;
                }
            }

            return b;
        } catch (FileNotFoundException e) {
        } finally {
            try {
                if (fd != null)
                    fd.close();
            } catch (IOException e) {
            }
        }
    }
    return null;
}

From source file:eu.faircode.adblocker.ServiceSinkhole.java

private void stopVPN(ParcelFileDescriptor pfd) {
    Log.i(TAG, "Stopping");
    try {/*from w w w .java 2 s  . c om*/
        pfd.close();
    } catch (IOException ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
        Util.sendCrashReport(ex, this);
    }
}

From source file:com.netcompss.ffmpeg4android_client.BaseVideo.java

public String getFileNameByUri(Context context, Uri uri) {
    String fileName = "unknown";// default fileName
    Uri filePathUri = uri;/*from www . j  ava 2  s  .com*/
    if (uri.getScheme().toString().compareTo("content") == 0) {
        ParcelFileDescriptor parcelFileDescriptor;
        String filename = null;
        try {
            FileOutputStream fos = null;
            parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r");
            FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
            Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
            String extr = Environment.getExternalStorageDirectory().toString();
            File mFolder = new File(extr + "/CHURCH");
            if (!mFolder.exists()) {
                mFolder.mkdir();
            } else {
                mFolder.delete();
                mFolder.mkdir();
            }

            String s = "rough.png";

            File f = new File(mFolder.getAbsolutePath(), s);

            filename = f.getAbsolutePath();
            Log.d("f", filename);
            try {
                fos = new FileOutputStream(f);
                image.compress(Bitmap.CompressFormat.PNG, 100, fos);
                fos.flush();
                fos.close();
            } catch (FileNotFoundException e) {

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

                e.printStackTrace();
            }
            parcelFileDescriptor.close();
            return filename;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else if (uri.getScheme().compareTo("file") == 0) {
        fileName = filePathUri.getPath();
        File fill = new File(fileName);
        if (fill.exists()) {
            Log.d("exitst", "exist");
            ParcelFileDescriptor parcelFileDescriptor;
            String filename = null;
            FileOutputStream fos = null;
            Bitmap image = BitmapFactory.decodeFile(fileName);
            String extr = Environment.getExternalStorageDirectory().toString();
            File mFolder = new File(extr + "/CHURCH");
            if (!mFolder.exists()) {
                mFolder.mkdir();
            } else {
                mFolder.delete();
                mFolder.mkdir();
            }

            String s = "rough.png";

            File f = new File(mFolder.getAbsolutePath(), s);

            filename = f.getAbsolutePath();
            Log.d("f", filename);
            try {
                fos = new FileOutputStream(f);
                image.compress(Bitmap.CompressFormat.PNG, 100, fos);
                fos.flush();
                fos.close();
            } catch (FileNotFoundException e) {

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

                e.printStackTrace();
            }
        } else {
            Log.d("not file exitst", "not exist");
        }
        Log.d("file", "file");
    } else {
        fileName = filePathUri.getPath();
        Log.d("else", "else");
    }

    return fileName;

}

From source file:im.vector.activity.RoomActivity.java

private void writeMediaUrl(Uri destUri) {
    try {/*from  ww  w . jav a  2 s .  com*/
        ParcelFileDescriptor pfd = this.getContentResolver().openFileDescriptor(destUri, "w");

        FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());

        File sourceFile = mMediasCache.mediaCacheFile(mPendingMediaUrl, mPendingMimeType);

        FileInputStream inputStream = new FileInputStream(sourceFile);

        byte[] buffer = new byte[1024 * 10];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, len);
        }

        fileOutputStream.close();
        pfd.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.drrickorang.loopback.LoopbackActivity.java

/** Save a screenshot of the main activity. */
void saveScreenShot(Uri uri) {
    ParcelFileDescriptor parcelFileDescriptor = null;
    FileOutputStream outputStream;
    try {//from   www.  j  a  v  a  2s  .  c  o  m
        parcelFileDescriptor = getApplicationContext().getContentResolver().openFileDescriptor(uri, "w");

        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
        outputStream = new FileOutputStream(fileDescriptor);

        log("Done creating output stream");

        LinearLayout LL = (LinearLayout) findViewById(R.id.linearLayoutMain);

        View v = LL.getRootView();
        v.setDrawingCacheEnabled(true);
        Bitmap b = v.getDrawingCache();

        //save
        b.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
        parcelFileDescriptor.close();
        v.setDrawingCacheEnabled(false);
    } catch (Exception e) {
        log("Failed to open png file " + e);
    } finally {
        try {
            if (parcelFileDescriptor != null) {
                parcelFileDescriptor.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log("Error closing ParcelFile Descriptor");
        }
    }
}

From source file:org.drrickorang.loopback.LoopbackActivity.java

/**
 * Save a .txt file of the given buffer period's data.
 * First column is time, second column is count.
 */// ww w. j  a  va2 s .c  om
void saveBufferPeriod(Uri uri, int[] bufferPeriodArray, int maxBufferPeriod) {
    ParcelFileDescriptor parcelFileDescriptor = null;
    FileOutputStream outputStream;
    if (bufferPeriodArray != null) {
        try {
            parcelFileDescriptor = getApplicationContext().getContentResolver().openFileDescriptor(uri, "w");

            FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
            outputStream = new FileOutputStream(fileDescriptor);
            log("Done creating output stream for saving buffer period");

            int usefulDataRange = Math.min(maxBufferPeriod + 1, bufferPeriodArray.length);
            int[] usefulBufferData = Arrays.copyOfRange(bufferPeriodArray, 0, usefulDataRange);

            String endline = "\n";
            String tab = "\t";
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < usefulBufferData.length; i++) {
                sb.append(i + tab + usefulBufferData[i] + endline);
            }

            outputStream.write(sb.toString().getBytes());
            parcelFileDescriptor.close();

        } catch (Exception e) {
            log("Failed to open text file " + e);
        } finally {
            try {
                if (parcelFileDescriptor != null) {
                    parcelFileDescriptor.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
                log("Error closing ParcelFile Descriptor");
            }
        }
    }

}

From source file:eu.faircode.netguard.ServiceSinkhole.java

private void stopVPN(ParcelFileDescriptor pfd) {
    Log.i(TAG, "Stopping");
    try {//w w  w .j  a  v  a 2  s. c o m
        pfd.close();
    } catch (IOException ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }
}

From source file:android.webkit.cts.WebViewTest.java

private void savePrintedPage(final PrintDocumentAdapter adapter, final ParcelFileDescriptor descriptor,
        final FutureTask<Boolean> result) {
    adapter.onWrite(new PageRange[] { PageRange.ALL_PAGES }, descriptor, new CancellationSignal(),
            new WriteResultCallback() {
                @Override// w ww.j ava  2s .c o  m
                public void onWriteFinished(PageRange[] pages) {
                    try {
                        descriptor.close();
                        result.run();
                    } catch (IOException ex) {
                        fail("Failed file operation: " + ex.toString());
                    }
                }
            });
}