Example usage for android.os ParcelFileDescriptor getFileDescriptor

List of usage examples for android.os ParcelFileDescriptor getFileDescriptor

Introduction

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

Prototype

public FileDescriptor getFileDescriptor() 

Source Link

Document

Retrieve the actual FileDescriptor associated with this object.

Usage

From source file:com.uphyca.kitkat.storage.ui.MainFragment.java

/**
 * uri???????/*  ww w .  j a va  2s.  c o  m*/
 * 
 * @param uri
 * @throws IOException
 */
private void alterDocument(Uri uri) throws IOException {
    ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(uri, "w");
    FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
    fileOutputStream.write(("Overwritten by MyCloud at " + System.currentTimeMillis() + "\n").getBytes());
    // Let the document provider know you're done by closing the stream.
    fileOutputStream.close();
    pfd.close();
}

From source file:com.proalias.awesomestatus.MainActivity.java

private Bitmap getBitmapFromUri(Uri uri) throws IOException {
    ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
    FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
    Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
    parcelFileDescriptor.close();/*w  w  w .  java2  s  .c o m*/
    return image;
}

From source file:com.csipsimple.backup.SipProfilesHelper.java

@Override
public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) {
    boolean forceBackup = (oldState == null);

    long fileModified = databaseFile.lastModified();
    try {//from w  ww .j a v a 2s.  c  o m
        if (!forceBackup) {
            FileInputStream instream = new FileInputStream(oldState.getFileDescriptor());
            DataInputStream in = new DataInputStream(instream);
            long lastModified = in.readLong();
            in.close();

            if (lastModified < fileModified) {
                forceBackup = true;
            }
        }
    } catch (IOException e) {
        Log.e(THIS_FILE, "Cannot manage previous local backup state", e);
        forceBackup = true;
    }

    Log.d(THIS_FILE, "Will backup profiles ? " + forceBackup);
    if (forceBackup) {
        JSONArray accountsSaved = SipProfileJson.serializeSipProfiles(mContext);
        try {
            writeData(data, accountsSaved.toString());
        } catch (IOException e) {
            Log.e(THIS_FILE, "Cannot manage remote backup", e);
        }
    }

    try {
        FileOutputStream outstream = new FileOutputStream(newState.getFileDescriptor());
        DataOutputStream out = new DataOutputStream(outstream);
        out.writeLong(fileModified);
        out.close();
    } catch (IOException e) {
        Log.e(THIS_FILE, "Cannot manage final local backup state", e);
    }
}

From source file:org.fedorahosted.freeotp.MainActivity.java

private void exportToFile(final Uri uri) {
    new AsyncTask<Void, Void, Void>() {
        @Override/*w  w w . ja v a  2s  .c o  m*/
        protected Void doInBackground(Void... params) {
            try {
                ParcelFileDescriptor pfd = MainActivity.this.getContentResolver().openFileDescriptor(uri, "w");
                FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
                fileOutputStream.write(tokenPersistence.toJSON().getBytes());
                // Let the document provider know you're done by closing the stream.
                fileOutputStream.close();
                pfd.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            Toast.makeText(MainActivity.this, R.string.export_succeeded_text, Toast.LENGTH_LONG).show();
        }
    }.execute((Void) null);
}

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

public static String getFileNameByUri(Context context, Uri uri) {
    String fileName = "unknown";// default fileName
    Uri filePathUri = uri;// ww  w.  ja v a2 s  .co  m
    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:org.y20k.transistor.helpers.ImageHelper.java

private Bitmap decodeSampledBitmapFromUri(Uri imageUri, int reqWidth, int reqHeight) {

    Bitmap bitmap;/*from w w w.ja  va2s  . c  o  m*/
    ParcelFileDescriptor parcelFileDescriptor = null;

    try {
        parcelFileDescriptor = mActivity.getContentResolver().openFileDescriptor(imageUri, "r");
    } catch (FileNotFoundException e) {
        // TODO handle error
        e.printStackTrace();
    }

    if (parcelFileDescriptor != null) {
        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();

        // decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);

        // calculate inSampleSize
        options.inSampleSize = calculateSampleParameter(options, reqWidth, reqHeight);

        // decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);

        return bitmap;

    } else {
        return null;
    }

}

From source file:freed.cam.apis.camera2.modules.VideoModuleApi2.java

private void setRecorderFilePath() {
    if (!appSettingsManager.GetWriteExternal()) {
        mediaRecorder.setOutputFile(recordingFile.getAbsolutePath());
    } else {//from ww  w. ja  v  a 2s. com
        Uri uri = Uri.parse(appSettingsManager.GetBaseFolder());
        DocumentFile df = cameraUiWrapper.getActivityInterface().getFreeDcamDocumentFolder();
        DocumentFile wr = df.createFile("*/*", recordingFile.getName());
        ParcelFileDescriptor fileDescriptor = null;
        try {
            fileDescriptor = cameraUiWrapper.getContext().getContentResolver().openFileDescriptor(wr.getUri(),
                    "rw");
            mediaRecorder.setOutputFile(fileDescriptor.getFileDescriptor());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            try {
                fileDescriptor.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}

From source file:com.csipsimple.backup.SipSharedPreferencesHelper.java

@Override
public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState) {
    boolean forceBackup = (oldState == null);

    long fileModified = 1;
    if (prefsFiles != null) {
        fileModified = prefsFiles.lastModified();
    }/*from  w w w .  j a  v  a 2 s.  c o  m*/
    try {
        if (!forceBackup) {
            FileInputStream instream = new FileInputStream(oldState.getFileDescriptor());
            DataInputStream in = new DataInputStream(instream);
            long lastModified = in.readLong();
            in.close();

            if (lastModified < fileModified) {
                forceBackup = true;
            }
        }
    } catch (IOException e) {
        Log.e(THIS_FILE, "Cannot manage previous local backup state", e);
        forceBackup = true;
    }

    Log.d(THIS_FILE, "Will backup profiles ? " + forceBackup);
    if (forceBackup) {
        JSONObject settings = SipProfileJson.serializeSipSettings(mContext);
        try {
            writeData(data, settings.toString());
        } catch (IOException e) {
            Log.e(THIS_FILE, "Cannot manage remote backup", e);
        }
    }

    try {
        FileOutputStream outstream = new FileOutputStream(newState.getFileDescriptor());
        DataOutputStream out = new DataOutputStream(outstream);
        out.writeLong(fileModified);
        out.close();
    } catch (IOException e) {
        Log.e(THIS_FILE, "Cannot manage final local backup state", e);
    }

}

From source file:com.cars.manager.utils.imageChooser.threads.MediaProcessorThread.java

protected void processGooglePhotosMedia(String path, String extension) throws Exception {
    if (Config.DEBUG) {
        Log.i(TAG, "Google photos Started");
        Log.i(TAG, "URI: " + path);
        Log.i(TAG, "Extension: " + extension);
    }/*  w  w w  .  j  a  va 2s  .  co  m*/
    String retrievedExtension = checkExtension(Uri.parse(path));
    if (retrievedExtension != null && !TextUtils.isEmpty(retrievedExtension)) {
        extension = "." + retrievedExtension;
    }
    try {

        filePath = FileUtils.getDirectory(foldername) + File.separator
                + Calendar.getInstance().getTimeInMillis() + extension;

        ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver()
                .openFileDescriptor(Uri.parse(path), "r");

        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();

        InputStream inputStream = new FileInputStream(fileDescriptor);

        BufferedInputStream reader = new BufferedInputStream(inputStream);

        BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] buf = new byte[2048];
        int len;
        while ((len = reader.read(buf)) > 0) {
            outStream.write(buf, 0, len);
        }
        outStream.flush();
        outStream.close();
        inputStream.close();
        process();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    if (Config.DEBUG) {
        Log.i(TAG, "Picasa Done");
    }
}

From source file:com.haru.ui.image.workers.MediaProcessorThread.java

protected void processGooglePhotosMedia(String path, String extension) throws Exception {
    if (/* TODO: DEBUG */ true) {
        Log.i(TAG, "Google photos Started");
        Log.i(TAG, "URI: " + path);
        Log.i(TAG, "Extension: " + extension);
    }/*from w ww  . j a  va 2  s .c om*/
    String retrievedExtension = checkExtension(Uri.parse(path));
    if (retrievedExtension != null && !TextUtils.isEmpty(retrievedExtension)) {
        extension = "." + retrievedExtension;
    }
    try {

        filePath = foldername + File.separator + Calendar.getInstance().getTimeInMillis() + extension;

        ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver()
                .openFileDescriptor(Uri.parse(path), "r");

        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();

        InputStream inputStream = new FileInputStream(fileDescriptor);

        BufferedInputStream reader = new BufferedInputStream(inputStream);

        BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] buf = new byte[2048];
        int len;
        while ((len = reader.read(buf)) > 0) {
            outStream.write(buf, 0, len);
        }
        outStream.flush();
        outStream.close();
        inputStream.close();
        process();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    if (/* TODO: DEBUG */ true) {
        Log.i(TAG, "Picasa Done");
    }
}