@Override public long getSize() throws IOException { long length = mDescriptor.getLength(); if(mMediaBytes == null){ InputStream inputStream = mDescriptor.createInputStream(); mMediaBytes = readBytes(inputStream); } return length; }
File newSoundFile = new File("/sdcard/media/ringtone", "myringtone.oog"); Uri mUri = Uri.parse("android.resource://com.your.package/R.raw.your_resource_id"); ContentResolver mCr = app.getContentResolver(); AssetFileDescriptor soundFile; try { soundFile= mCr.openAssetFileDescriptor(mUri, "r"); } catch (FileNotFoundException e) { soundFile=null; } try { byte[] readData = new byte[1024]; FileInputStream fis = soundFile.createInputStream(); FileOutputStream fos = new FileOutputStream(newSoundFile); int i = fis.read(readData); while (i != -1) { fos.write(readData, 0, i); i = fis.read(readData); } fos.close(); } catch (IOException io) { }
/** * getInputStream returns an AssetFileDescriptor.AutoCloseInputStream * associated with the asset that is contained in the Zip file, or a * standard ZipInputStream if necessary to uncompress the file * * @param assetPath * @return an input stream for the named asset path, or null if not found * @throws IOException */ public InputStream getInputStream(String assetPath) throws IOException { ZipEntryRO entry = mHashMap.get(assetPath); if (null != entry) { if (entry.isUncompressed()) { return entry.getAssetFileDescriptor().createInputStream(); } else { ZipFile zf = mZipFiles.get(entry.getZipFile()); /** read compressed files **/ if (null == zf) { zf = new ZipFile(entry.getZipFile(), ZipFile.OPEN_READ); mZipFiles.put(entry.getZipFile(), zf); } ZipEntry zi = zf.getEntry(assetPath); if (null != zi) return zf.getInputStream(zi); } } return null; }
/** * getInputStream returns an AssetFileDescriptor.AutoCloseInputStream * associated with the asset that is contained in the Zip file, or a * standard ZipInputStream if necessary to uncompress the file * * @param assetPath * @return an input stream for the named asset path, or null if not found * @throws IOException */ public InputStream getInputStream(String assetPath) throws IOException { ZipEntryRO entry = mHashMap.get(assetPath); if (null != entry) { if (entry.isUncompressed()) { return entry.getAssetFileDescriptor().createInputStream(); } else { ZipFile zf = mZipFiles.get(entry.getZipFile()); /** read compressed files **/ if (null == zf) { zf = new ZipFile(entry.getZipFile(), ZipFile.OPEN_READ); mZipFiles.put(entry.getZipFile(), zf); } ZipEntry zi = zf.getEntry(assetPath); if (null != zi) return zf.getInputStream(zi); } } return null; }
@Override public long getSize() throws IOException { long length = mDescriptor.getLength(); if(mMediaBytes == null){ InputStream inputStream = mDescriptor.createInputStream(); mMediaBytes = readBytes(inputStream); } return length; }
private static Drawable loadDrawableForCookie(TypedValue value, int id, Resources res, Resources.Theme theme) { if (value.string == null) { throw new Resources.NotFoundException("Resource \"" + res.getResourceName(id) + "\" (" + Integer.toHexString(id) + ") is not a Drawable (color or path): " + value); } String file = value.string.toString(); final Drawable dr; if (file.endsWith(".xml")) { try { XmlResourceParser rp = res.getAssets().openXmlResourceParser(value.assetCookie, file); dr = LollipopDrawablesCompat.createFromXml(res, rp, theme); rp.close(); } catch (Exception e) { Log.w(LollipopDrawablesCompat.class.getSimpleName(), "Failed to load drawable resource, " + "using a fallback...", e); return res.getDrawable(value.resourceId); } } else { try { InputStream is = res.getAssets().openNonAssetFd(value.assetCookie, file).createInputStream(); dr = LollipopDrawablesCompat.createFromResourceStream(res, value, is, file, null); is.close(); } catch (Exception e) { Log.w(LollipopDrawablesCompat.class.getSimpleName(), "Failed to load drawable resource, " + "using a fallback...", e); return res.getDrawable(value.resourceId); } } return dr; }
stream = descr.createInputStream(); InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
InputStream is = getAssets().openNonAssetFd(value.assetCookie, file).createInputStream(); dr = createFromResourceStream(value, is, file, null); is.close();
@Test public void gifDrawableCreatedFromInputStream() throws Exception { final AssetFileDescriptor assetFileDescriptor = InstrumentationRegistry.getInstrumentation() .getContext().getResources().openRawResourceFd(R.raw.test); final byte[] buffer = new byte[(int) assetFileDescriptor.getDeclaredLength()]; final FileInputStream inputStream = assetFileDescriptor.createInputStream(); final int bufferedByteCount = inputStream.read(buffer); inputStream.close(); assetFileDescriptor.close(); assertThat(bufferedByteCount).isEqualTo(buffer.length); final InputStream responseStream = new ByteArrayInputStream(buffer); final GifDrawable gifDrawable = new GifDrawable(responseStream); assertThat(gifDrawable.getError()).isEqualTo(GifError.NO_ERROR); assertThat(gifDrawable.getIntrinsicWidth()).isEqualTo(278); assertThat(gifDrawable.getIntrinsicHeight()).isEqualTo(183); } }
@Test @Ignore("TODO(xian): re-enable; see https://github.com/robolectric/robolectric/issues/4091") public void openFd_shouldProvideFileDescriptorForAsset() throws Exception { AssetFileDescriptor assetFileDescriptor = assetManager.openFd("assetsHome.txt"); assertThat(CharStreams.toString(new InputStreamReader(assetFileDescriptor.createInputStream(), UTF_8))) .isEqualTo("assetsHome!"); assertThat(assetFileDescriptor.getLength()).isEqualTo(11); }
@Test public void open_shouldProvideFileDescriptor() throws Exception { File file = new File( getTargetContext().getFilesDir() + File.separator + "open_shouldProvideFileDescriptor.txt"); FileOutputStream output = new FileOutputStream(file); output.write("hi".getBytes()); ParcelFileDescriptor parcelFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); AssetFileDescriptor assetFileDescriptor = new AssetFileDescriptor(parcelFileDescriptor, 0, "hi".getBytes().length); assertThat( CharStreams.toString( new InputStreamReader(assetFileDescriptor.createInputStream(), UTF_8))) .isEqualTo("hi"); assertThat(assetFileDescriptor.getLength()).isEqualTo(2); assetFileDescriptor.close(); } }
protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (resultCode != RESULT_OK) return; try { AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(intent.getData(), "r"); FileInputStream fis = videoAsset.createInputStream(); File tmpFile = new File(Environment.getExternalStorageDirectory(),"VideoFile.3gp"); FileOutputStream fos = new FileOutputStream(tmpFile); byte[] buf = new byte[1024]; int len; while ((len = fis.read(buf)) > 0) { fos.write(buf, 0, len); } fis.close(); fos.close(); } catch (IOException io_e) { // TODO: handle error }
protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (resultCode == RESULT_OK) { try { AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(intent.getData(), "r"); FileInputStream fis = videoAsset.createInputStream(); File videoFile = new File(Environment.getExternalStorageDirectory(),"<VideoFileName>.mp4"); FileOutputStream fos = new FileOutputStream(videoFile); byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { fos.write(buffer, 0, length); } fis.close(); fos.close(); } catch (IOException e) { // TODO: handle error } } }
FileInputStream fis = videoAsset.createInputStream(); File root=new File(Environment.getExternalStorageDirectory(),"Directory Name");
try { AssetFileDescriptor assetFileDescriptor = context.getContentResolver().openTypedAssetFileDescriptor(uri, "text/*", null); stream = assetFileDescriptor.createInputStream(); reader = new InputStreamReader(stream, "UTF-8");
public InputStream openDisplayPhoto(long photoFileId) { Uri displayPhotoUri = ContentUris.withAppendedId(DisplayPhoto.CONTENT_URI, photoKey); try { AssetFileDescriptor fd = getContentResolver().openAssetFileDescriptor( displayPhotoUri, "r"); return fd.createInputStream(); } catch (IOException e) { return null; } }
public InputStream openDisplayPhoto(long contactId) { Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId); Uri displayPhotoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.DISPLAY_PHOTO); try { AssetFileDescriptor fd = getContentResolver().openAssetFileDescriptor(displayPhotoUri, "r"); return fd.createInputStream(); } catch (IOException e) { return null; } }
ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey); AssetFileDescriptor fd = this.getContentResolver().openAssetFileDescriptor(uri, "r"); FileInputStream fis = fd.createInputStream();
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey); AssetFileDescriptor fd = resolver.openAssetFileDescriptor(uri, "r"); FileInputStream fis = fd.createInputStream(); byte[] b = new byte[(int)fd.getDeclaredLength()]; fis.read(b); String vCard = new String(b); sb.append(vCard);
@Override public long getSize() throws IOException { long length = mDescriptor.getLength(); if (mMediaBytes == null) { InputStream inputStream = mDescriptor.createInputStream(); mMediaBytes = readBytes(inputStream); } return length; }