List of usage examples for android.content ContentResolver openOutputStream
public final @Nullable OutputStream openOutputStream(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException
From source file:org.sufficientlysecure.keychain.util.FileHelper.java
/** * Deletes data at a URI securely by overwriting it with random data * before deleting it. This method is fail-fast - if we can't securely * delete the file, we don't delete it at all. *///w w w .j av a 2 s . co m public static int deleteFileSecurely(Context context, Uri uri) throws IOException { ContentResolver resolver = context.getContentResolver(); long lengthLeft = FileHelper.getFileSize(context, uri); if (lengthLeft == -1) { throw new IOException("Error opening file!"); } SecureRandom random = new SecureRandom(); byte[] randomData = new byte[1024]; OutputStream out = resolver.openOutputStream(uri, "w"); if (out == null) { throw new IOException("Error opening file!"); } out = new BufferedOutputStream(out); while (lengthLeft > 0) { random.nextBytes(randomData); out.write(randomData, 0, lengthLeft > randomData.length ? randomData.length : (int) lengthLeft); lengthLeft -= randomData.length; } out.close(); if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) { return new File(uri.getPath()).delete() ? 1 : 0; } else { return resolver.delete(uri, null, null); } }
From source file:org.deviceconnect.android.deviceplugin.theta.fragment.ThetaVRModeFragment.java
/** * Save File./*from w w w . java 2 s . com*/ * @param filename absolute path * @param data binary * @throws IOException Failed Save */ private void saveFile(final String filename, final byte[] data) throws IOException { Uri u = Uri.parse("file://" + filename); ContentResolver contentResolver = getActivity().getContentResolver(); OutputStream out = null; try { out = contentResolver.openOutputStream(u, "w"); out.write(data); out.flush(); } catch (Exception e) { throw new IOException("Failed to save a file." + filename); } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:com.chen.mail.providers.Attachment.java
/** * Constructor for use when creating attachments in eml files. *//* ww w . j ava 2s . co m*/ public Attachment(Context context, Part part, Uri emlFileUri, String messageId, String partId) { try { // Transfer fields from mime format to provider format final String contentTypeHeader = MimeUtility.unfoldAndDecode(part.getContentType()); name = MimeUtility.getHeaderParameter(contentTypeHeader, "name"); if (name == null) { final String contentDisposition = MimeUtility.unfoldAndDecode(part.getDisposition()); name = MimeUtility.getHeaderParameter(contentDisposition, "filename"); } contentType = MimeType.inferMimeType(name, part.getMimeType()); uri = EmlAttachmentProvider.getAttachmentUri(emlFileUri, messageId, partId); contentUri = uri; thumbnailUri = uri; previewIntentUri = null; state = UIProvider.AttachmentState.SAVED; providerData = null; supportsDownloadAgain = false; destination = UIProvider.AttachmentDestination.CACHE; type = UIProvider.AttachmentType.STANDARD; flags = 0; // insert attachment into content provider so that we can open the file final ContentResolver resolver = context.getContentResolver(); resolver.insert(uri, toContentValues()); // save the file in the cache try { final InputStream in = part.getBody().getInputStream(); final OutputStream out = resolver.openOutputStream(uri, "rwt"); size = IOUtils.copy(in, out); downloadedSize = size; in.close(); out.close(); } catch (FileNotFoundException e) { LogUtils.e(LOG_TAG, e, "Error in writing attachment to cache"); } catch (IOException e) { LogUtils.e(LOG_TAG, e, "Error in writing attachment to cache"); } // perform a second insert to put the updated size and downloaded size values in resolver.insert(uri, toContentValues()); } catch (MessagingException e) { LogUtils.e(LOG_TAG, e, "Error parsing eml attachment"); } }
From source file:com.android.mail.providers.Attachment.java
/** * Constructor for use when creating attachments in eml files. *///from ww w . j a v a 2 s.co m public Attachment(Context context, Part part, Uri emlFileUri, String messageId, String cid, boolean inline) { try { // Transfer fields from mime format to provider format final String contentTypeHeader = MimeUtility.unfoldAndDecode(part.getContentType()); name = MimeUtility.getHeaderParameter(contentTypeHeader, "name"); if (name == null) { final String contentDisposition = MimeUtility.unfoldAndDecode(part.getDisposition()); name = MimeUtility.getHeaderParameter(contentDisposition, "filename"); } contentType = MimeType.inferMimeType(name, part.getMimeType()); uri = EmlAttachmentProvider.getAttachmentUri(emlFileUri, messageId, cid); contentUri = uri; thumbnailUri = uri; previewIntentUri = null; state = AttachmentState.SAVED; providerData = null; supportsDownloadAgain = false; destination = AttachmentDestination.CACHE; type = inline ? AttachmentType.INLINE_CURRENT_MESSAGE : AttachmentType.STANDARD; partId = cid; flags = 0; // insert attachment into content provider so that we can open the file final ContentResolver resolver = context.getContentResolver(); resolver.insert(uri, toContentValues()); // save the file in the cache try { final InputStream in = part.getBody().getInputStream(); final OutputStream out = resolver.openOutputStream(uri, "rwt"); size = IOUtils.copy(in, out); downloadedSize = size; in.close(); out.close(); } catch (FileNotFoundException e) { LogUtils.e(LOG_TAG, e, "Error in writing attachment to cache"); } catch (IOException e) { LogUtils.e(LOG_TAG, e, "Error in writing attachment to cache"); } // perform a second insert to put the updated size and downloaded size values in resolver.insert(uri, toContentValues()); } catch (MessagingException e) { LogUtils.e(LOG_TAG, e, "Error parsing eml attachment"); } }
From source file:com.tct.mail.providers.Attachment.java
/** * Constructor for use when creating attachments in eml files. *///from w ww . j a va2s . c o m public Attachment(Context context, Part part, Uri emlFileUri, String messageId, String cid, boolean inline, String subject) { try { // Transfer fields from mime format to provider format final String contentTypeHeader = MimeUtility.unfoldAndDecode(part.getContentType()); name = MimeUtility.getHeaderParameter(contentTypeHeader, "name"); if (name == null) { final String contentDisposition = MimeUtility.unfoldAndDecode(part.getDisposition()); name = MimeUtility.getHeaderParameter(contentDisposition, "filename"); } //TS: Gantao 2016-01-13 EMAIL BUGFIX_1292212 MOD_S //If we still don't get eml files's name , set it as subject+.eml if (name == null && MimeUtility.isEmlMimeType(part.getMimeType())) { name = subject + ".eml"; } //TS: Gantao 2016-01-13 EMAIL BUGFIX_1292212 MOD_E contentType = MimeType.inferMimeType(name, part.getMimeType()); //TS: Gantao 2015-07-03 EMAIL BUGFIX_957636 MOD_S if (inline) { // TS: Gantao 2015-09-19 EMAIL BUGFIX_570084 ADD_S isInline = 1; // TS: Gantao 2015-09-19 EMAIL BUGFIX_570084 ADD_E uri = EmlAttachmentProvider.getAttachmentByCidUri(emlFileUri, messageId); uri = uri.buildUpon().appendPath(cid).build(); } else { // TS: Gantao 2015-09-19 EMAIL BUGFIX_570084 ADD_S isInline = 0; // TS: Gantao 2015-09-19 EMAIL BUGFIX_570084 ADD_E uri = EmlAttachmentProvider.getAttachmentUri(emlFileUri, messageId, cid); } //TS: Gantao 2015-07-03 EMAIL BUGFIX_957636 MOD_S contentUri = uri; thumbnailUri = uri; previewIntentUri = null; state = AttachmentState.SAVED; providerData = null; supportsDownloadAgain = false; destination = AttachmentDestination.CACHE; type = inline ? AttachmentType.INLINE_CURRENT_MESSAGE : AttachmentType.STANDARD; partId = cid; flags = 0; //TS: zhonghua.tuo 2015-3-3 EMAIL BUGFIX_936728 ADD_S realUri = null; //TS: zhonghua.tuo 2015-3-3 EMAIL BUGFIX_936728 ADD_E // insert attachment into content provider so that we can open the file final ContentResolver resolver = context.getContentResolver(); resolver.insert(uri, toContentValues()); // save the file in the cache try { final InputStream in = part.getBody().getInputStream(); final OutputStream out = resolver.openOutputStream(uri, "rwt"); size = IOUtils.copy(in, out); downloadedSize = size; in.close(); out.close(); } catch (FileNotFoundException e) { LogUtils.e(LOG_TAG, e, "Error in writing attachment to cache"); } catch (IOException e) { LogUtils.e(LOG_TAG, e, "Error in writing attachment to cache"); } // perform a second insert to put the updated size and downloaded size values in resolver.insert(uri, toContentValues()); } catch (MessagingException e) { LogUtils.e(LOG_TAG, e, "Error parsing eml attachment"); } }