private void writeAttachment(Uri documentUri) throws IOException { ContentResolver contentResolver = context.getContentResolver(); InputStream in = contentResolver.openInputStream(attachment.internalUri); try { OutputStream out = contentResolver.openOutputStream(documentUri); try { IOUtils.copy(in, out); out.flush(); } finally { out.close(); } } finally { in.close(); } }
/** Write the given bitmap to the given uri using the given compression. */ static void writeBitmapToUri( Context context, Bitmap bitmap, Uri uri, Bitmap.CompressFormat compressFormat, int compressQuality) throws FileNotFoundException { OutputStream outputStream = null; try { outputStream = context.getContentResolver().openOutputStream(uri); bitmap.compress(compressFormat, compressQuality, outputStream); } finally { closeSafe(outputStream); } }
/** 将图片保存在本地 */ private void saveOutput(Bitmap croppedImage, Bitmap.CompressFormat outputFormat, File saveFile) { OutputStream outputStream = null; try { outputStream = getContext().getContentResolver().openOutputStream(Uri.fromFile(saveFile)); if (outputStream != null) croppedImage.compress(outputFormat, 90, outputStream); Message.obtain(mHandler, SAVE_SUCCESS, saveFile).sendToTarget(); } catch (IOException ex) { ex.printStackTrace(); Message.obtain(mHandler, SAVE_ERROR, saveFile).sendToTarget(); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } mSaving = false; croppedImage.recycle(); }
public static void exportToUri(Context context, boolean includeGlobals, Set<String> accountUuids, Uri uri) throws SettingsImportExportException { OutputStream os = null; try { os = context.getContentResolver().openOutputStream(uri); exportPreferences(context, os, includeGlobals, accountUuids); } catch (Exception e) { throw new SettingsImportExportException(e); } finally { closeOrThrow(os); } }
source = response.body().source(); OutputStream outputStream = mContext.getContentResolver().openOutputStream(outputUri); if (outputStream != null) { sink = Okio.sink(outputStream);
getContentResolver().openOutputStream(document, "w"); OutputStreamWriter osw=new OutputStreamWriter(os);
private Uri saveImage(Bitmap bitmap, final Uri uri) throws IOException, IllegalStateException { mSaveUri = uri; if (mSaveUri == null) { throw new IllegalStateException("Save uri must not be null."); } OutputStream outputStream = null; try { outputStream = getContext().getContentResolver().openOutputStream(uri); bitmap.compress(mCompressFormat, mCompressQuality, outputStream); Utils.copyExifInfo(getContext(), mSourceUri, uri, bitmap.getWidth(), bitmap.getHeight()); Utils.updateGalleryInfo(getContext(), uri); return uri; } finally { Utils.closeQuietly(outputStream); } }
@Override public void onActivityResult(int requestCode, int resultCode, Intent resultData) { if (requestCode == WRITE_REQUEST_CODE && resultCode == Activity.RESULT_OK) { if (resultData != null){ Uri uri = resultData.getData(); String filepath = getCacheDir() + "/" + mCacheFileName; File cachefile = new File(filepath); InputStream is = null; OutputStream os = null; ContentResolver resolver = getContentResolver(); try { is = new FileInputStream(cachefile); os = resolver.openOutputStream(uri); IOUtils.copy(is, os); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } cachefile.delete(); Toast.makeText(this, getString(R.string.image_saved, uri.getPath()), Toast.LENGTH_SHORT).show(); // Sync media store sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri)); } } }
@Test public void openOutputStream_shouldReturnAnOutputStream() throws Exception { assertThat(contentResolver.openOutputStream(uri21)).isInstanceOf(OutputStream.class); }
@Test public void openOutputStream_shouldReturnRegisteredStream() throws Exception { final Uri uri = Uri.parse("content://registeredProvider/path"); AtomicInteger callCount = new AtomicInteger(); OutputStream outputStream = new OutputStream() { @Override public void write(int arg0) throws IOException { callCount.incrementAndGet(); } @Override public String toString() { return "outputstream for " + uri; } }; shadowOf(contentResolver).registerOutputStream(uri, outputStream); assertThat(callCount.get()).isEqualTo(0); contentResolver.openOutputStream(uri).write(5); assertThat(callCount.get()).isEqualTo(1); contentResolver.openOutputStream(uri21).write(5); assertThat(callCount.get()).isEqualTo(1); }
@Override protected Throwable doInBackground(Void... params) { try { Bitmap bitmap = CropIwaBitmapManager.get().loadToMemory( context, srcUri, saveConfig.getWidth(), saveConfig.getHeight()); if (bitmap == null) { return new NullPointerException("Failed to load bitmap"); } Bitmap cropped = cropArea.applyCropTo(bitmap); cropped = mask.applyMaskTo(cropped); Uri dst = saveConfig.getDstUri(); OutputStream os = context.getContentResolver().openOutputStream(dst); cropped.compress(saveConfig.getCompressFormat(), saveConfig.getQuality(), os); CropIwaUtils.closeSilently(os); bitmap.recycle(); cropped.recycle(); } catch (IOException e) { return e; } return null; }
OutputStream outputStream = null; try { outputStream = getContentResolver().openOutputStream(saveUri); if (outputStream != null) { croppedImage.compress(saveAsPng ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG,
/** Write the given bitmap to the given uri using the given compression. */ static void writeBitmapToUri( Context context, Bitmap bitmap, Uri uri, Bitmap.CompressFormat compressFormat, int compressQuality) throws FileNotFoundException { OutputStream outputStream = null; try { outputStream = context.getContentResolver().openOutputStream(uri); bitmap.compress(compressFormat, compressQuality, outputStream); } finally { closeSafe(outputStream); } }
private void saveOutput(Bitmap croppedImage) { if (mSaveUri != null) { OutputStream outputStream = null; try { outputStream = mContentResolver.openOutputStream(mSaveUri); if (outputStream != null) { croppedImage.compress(mOutputFormat, 90, outputStream); } } catch (IOException ex) { Log.e(TAG, "Cannot open file: " + mSaveUri, ex); setResult(RESULT_CANCELED); finish(); return; } finally { Util.closeSilently(outputStream); } Bundle extras = new Bundle(); Intent intent = new Intent(mSaveUri.toString()); intent.putExtras(extras); intent.putExtra(IMAGE_PATH, mImagePath); intent.putExtra(ORIENTATION_IN_DEGREES, Util.getOrientationInDegree(this)); setResult(RESULT_OK, intent); } else { Log.e(TAG, "not defined image url"); } croppedImage.recycle(); finish(); }
private void saveOutput(Bitmap croppedImage) { if (mSaveUri != null) { OutputStream outputStream = null; try { outputStream = mContentResolver.openOutputStream(mSaveUri); if (outputStream != null) { croppedImage.compress(mOutputFormat, 90, outputStream); } } catch (IOException ex) { Log.e(TAG, "Cannot open file: " + mSaveUri, ex); setResult(RESULT_CANCELED); finish(); return; } finally { Util.closeSilently(outputStream); } Bundle extras = new Bundle(); Intent intent = new Intent(mSaveUri.toString()); intent.putExtras(extras); intent.putExtra(IMAGE_PATH, mImagePath); intent.putExtra(ORIENTATION_IN_DEGREES, Util.getOrientationInDegree(this)); setResult(RESULT_OK, intent); } else { Log.e(TAG, "not defined image url"); } croppedImage.recycle(); finish(); }
writer = new OutputStreamWriter(getContentResolver().openOutputStream(selectedfile)); config.write(writer); writer.close();
public boolean exportData(Uri uri) { try { OutputStream output = context.getContentResolver().openOutputStream(uri); CsvHelper.exportTo(new OutputStreamWriter(output), scaleMeasurementList); return true; } catch (IOException e) { Toast.makeText(context, context.getResources().getString(R.string.error_exporting) + " " + e.getMessage(), Toast.LENGTH_SHORT).show(); } return false; }
@Override public Single<Uri> write(final String report) { return(Single.create(e -> { OutputStream os=cr.openOutputStream(uri); OutputStreamWriter osw=new OutputStreamWriter(os); osw.write(report); osw.flush(); osw.close(); e.onSuccess(uri); })); } }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_SAVE_DIFF && resultCode == RESULT_OK) { Uri diffUri = data.getData(); try { saveDiff(getContentResolver().openOutputStream(diffUri)); } catch (IOException e) { showToastMessage(R.string.alert_file_creation_failure); } } }
private boolean saveBitmapToUri(JobContext jc, Bitmap bitmap, Uri uri) { try { return saveBitmapToOutputStream(jc, bitmap, convertExtensionToCompressFormat(getFileExtension()), getContentResolver().openOutputStream(uri)); } catch (FileNotFoundException e) { Log.w(TAG, "cannot write output", e); } return true; }