public ByteBuffer map (FileChannel.MapMode mode) { if (type == FileType.Internal) { FileInputStream input = null; try { AssetFileDescriptor fd = getAssetFileDescriptor(); long startOffset = fd.getStartOffset(); long declaredLength = fd.getDeclaredLength(); input = new FileInputStream(fd.getFileDescriptor()); ByteBuffer map = input.getChannel().map(mode, startOffset, declaredLength); map.order(ByteOrder.nativeOrder()); return map; } catch (Exception ex) { throw new GdxRuntimeException("Error memory mapping file: " + this + " (" + type + ")", ex); } finally { StreamUtils.closeQuietly(input); } } return super.map(mode); }
public ByteBuffer map (FileChannel.MapMode mode) { if (type == FileType.Internal) { FileInputStream input = null; try { AssetFileDescriptor fd = getAssetFileDescriptor(); long startOffset = fd.getStartOffset(); long declaredLength = fd.getDeclaredLength(); input = new FileInputStream(fd.getFileDescriptor()); ByteBuffer map = input.getChannel().map(mode, startOffset, declaredLength); map.order(ByteOrder.nativeOrder()); return map; } catch (Exception ex) { throw new GdxRuntimeException("Error memory mapping file: " + this + " (" + type + ")", ex); } finally { StreamUtils.closeQuietly(input); } } return super.map(mode); }
/** * Play a sample with the Android MediaPLayer. * * @param resid Resource ID if the sample to play. */ private void playSample(int resid) { AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid); try { mediaPlayer.reset(); mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength()); mediaPlayer.prepare(); mediaPlayer.start(); afd.close(); } catch (IllegalArgumentException e) { Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e); } catch (IllegalStateException e) { Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e); } catch (IOException e) { Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e); }
@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); } }
AssetFileDescriptor fd = null; try { ContentResolver resolver = context.getContentResolver(); fd = resolver.openAssetFileDescriptor(uri, "r"); // : // : // : if (fd.getDeclaredLength() < 0) { setDataSource(fd.getFileDescriptor()); } else { setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getDeclaredLength()); }
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);
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey); AssetFileDescriptor fd = contentResolver.openAssetFileDescriptor(uri, "r"); FileInputStream fis = fd.createInputStream(); byte[] buf = new byte[(int)fd.getDeclaredLength()]; if (0 < fis.read(buf)) { String vCard = new String(buf); }
private void get(Cursor cursor2) { String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey); AssetFileDescriptor fd; try { fd = this.getContentResolver().openAssetFileDescriptor(uri, "r"); FileInputStream fis = fd.createInputStream(); byte[] buf = new byte[(int) fd.getDeclaredLength()]; fis.read(buf); String vcardstring= new String(buf); vCard.add(vcardstring); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }
try { AssetFileDescriptor afd = this.getResources().openRawResourceFd(R.raw.splashmusic); song = new MediaPlayer(); song.reset(); song.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength()); song.setAudioStreamType(AudioManager.STREAM_MUSIC); song.prepare(); // might take long! (for buffering, etc) song.start(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
private final String vfile = "POContactsRestore.vcf"; Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); phones.moveToFirst(); String lookupKey = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey); AssetFileDescriptor fd; try { fd = this.getContentResolver().openAssetFileDescriptor(uri, "r"); FileInputStream fis = fd.createInputStream(); byte[] buf = new byte[(int) fd.getDeclaredLength()]; fis.read(buf); String vCard = new String(buf); String path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile; FileOutputStream mFileOutputStream = new FileOutputStream(path, false); mFileOutputStream.write(vCard.toString().getBytes()); Log.d("Vcard", vCard); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); }
public string vCard(Intent data){ Uri contactData = data.getData(); Cursor cursor = managedQuery(contactData, null, null, null, null); cursor.moveToFirst(); String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey); AssetFileDescriptor fd; String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String nummer = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)); try { fd = getContentResolver().openAssetFileDescriptor(uri, "r"); FileInputStream fis = fd.createInputStream(); byte[] buf = new byte[(int) fd.getDeclaredLength()]; fis.read(buf); return new String(buf); //vCard from buffer } catch (Exception e) { Toast.makeText(this, "Failed to load Contact: " + name, Toast.LENGTH_SHORT).show(); e.printStackTrace(); } return null; }
private MediaPlayer getMediaPlayer(MediaPlayer mp, int resID, OnCompletionListener listener){ mp.release(); mp = null; mp = new MediaPlayer(); AssetFileDescriptor afd = getResources().openRawResourceFd(resID); try { mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength()); mp.prepare(); mp.setOnCompletionListener(listener); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return mp; }
/** Memory-map the model file in Assets. */ private static MappedByteBuffer loadModelFile(AssetManager assets, String modelFilename) throws IOException { AssetFileDescriptor fileDescriptor = assets.openFd(modelFilename); FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor()); FileChannel fileChannel = inputStream.getChannel(); long startOffset = fileDescriptor.getStartOffset(); long declaredLength = fileDescriptor.getDeclaredLength(); return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength); }
int rawresid = getResources().getIdentifier("FILENAME_WITHOUT_EXTENSION", "raw", getPackageName()); AssetFileDescriptor afd = getResources().openRawResourceFd(rawresid); mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
AssetFileDescriptor afd = ctx.getResources().openRawResourceFd(params[0]); try { //final MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.reset(); mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength()); mediaPlayer.prepare(); mediaPlayer.setOnPreparedListener(new OnPreparedListener() { public void onPrepared(MediaPlayer arg0) { mediaPlayer.seekTo(0); mediaPlayer.start(); }); } }); afd.close();
AssetFileDescriptor fd = context.getAssets().openFd("pathInsideAssets/fileName"); mp.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getDeclaredLength());
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.file_name); mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
/** * Memory-map the model file in Assets. */ private MappedByteBuffer loadModelFile(Activity activity) throws IOException { AssetFileDescriptor fileDescriptor = activity.getAssets().openFd(getModelPath()); FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor()); FileChannel fileChannel = inputStream.getChannel(); long startOffset = fileDescriptor.getStartOffset(); long declaredLength = fileDescriptor.getDeclaredLength(); return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength); }
// This one works with an image file and a media file. // Get a ZipResourceFile representing a specific expansion file // mainContext, version number of your main expansion, version number of your patch ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(this, 1, 0); AssetFileDescriptor afd_img1 = expansionFile.getAssetFileDescriptor("your_path/your_file.jpg"); AssetFileDescriptor afd_mpbg = expansionFile.getAssetFileDescriptor("your_path/your_file.mp3"); // To set image ImageView imageView1 = (ImageView)findViewById(R.id.iv1); imageView1.setImageBitmap(BitmapFactory.decodeFileDescriptor(afd_iv1.getFileDescriptor())); // To set sound try { mpbg.setDataSource(afd_mpbg.getFileDescriptor(), afd_mpbg.getStartOffset(), afd_mpbg.getDeclaredLength()); mpbg.prepareAsync(); mpbg.start(); } catch (IllegalStateException e) { Log.w("Error=====", "Failed to prepare media player", e); }
// this one work with image file, media file // Get a ZipResourceFile representing a specific expansion file // mainContext, version no. of your main expansion, version no of your patch ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(this, 1, 0); AssetFileDescriptor afd_img1 = expansionFile.getAssetFileDescriptor("your_path/your_file.jpg"); AssetFileDescriptor afd_mpbg = expansionFile.getAssetFileDescriptor("your_path/your_file.mp3"); // to set image ImageView imageView1 = (ImageView)findViewById(R.id.iv1); imageView1.setImageBitmap(BitmapFactory.decodeFileDescriptor(afd_iv1.getFileDescriptor())); // to set sound try { mpbg.setDataSource(afd_mpbg.getFileDescriptor(), afd_mpbg.getStartOffset(), afd_mpbg.getDeclaredLength()); mpbg.prepareAsync(); mpbg.start(); } catch (IllegalStateException e) { Log.w("Error=====", "Failed to prepare media player", e); }