List of usage examples for android.content ContentResolver openInputStream
public final @Nullable InputStream openInputStream(@NonNull Uri uri) throws FileNotFoundException
From source file:com.example.cuisoap.agrimac.machineRegister.machineInfoFragment.java
public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { Uri uri = data.getData();// w w w . j av a2 s . co m Log.e("uri", uri.toString()); ContentResolver cr = context.getContentResolver(); try { Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri)); bitmap = ImageUtils.comp(bitmap); switch (requestCode) { case 1: license_pic1.setImageBitmap(bitmap); license_path1 = ImageUtils.saveMyBitmap(bitmap, "machineinfo"); license_pic1.setVisibility(View.VISIBLE); license1.setVisibility(View.GONE); license_pic1.setOnClickListener(myOnClickListener); break; case 2: license_pic2.setImageBitmap(bitmap); license_path2 = ImageUtils.saveMyBitmap(bitmap, "machineinfo"); license_pic2.setVisibility(View.VISIBLE); license2.setVisibility(View.GONE); license_pic2.setOnClickListener(myOnClickListener); break; } } catch (FileNotFoundException e) { Log.e("Exception", e.getMessage(), e); } } else { Toast.makeText(context, "?", Toast.LENGTH_SHORT).show(); } super.onActivityResult(requestCode, resultCode, data); }
From source file:com.ruesga.rview.fragments.SnippetFragment.java
@Override public void onContentReady() { //noinspection ConstantConditions Uri snippetUri = getArguments().getParcelable(EXTRA_SNIPPET_URI); if (snippetUri == null) { snippetUri = getArguments().getParcelable(EXTRA_TEMP_SNIPPET_URI); }/* ww w . j a v a2 s. co m*/ if (snippetUri != null) { try { //noinspection ConstantConditions ContentResolver cr = getContext().getContentResolver(); try (InputStream is = cr.openInputStream(snippetUri)) { if (is != null) { byte[] data = new byte[is.available()]; IOUtils.read(is, data); loadContent(data); return; } } } catch (IOException ex) { Log.e(TAG, "Cannot load content stream: " + snippetUri, ex); } // Display an advise CoordinatorLayout decorator = ViewHelper.findFirstParentViewOfType(getContentView(), CoordinatorLayout.class); if (decorator != null) { AndroidHelper.showErrorSnackbar(getContext(), decorator, R.string.snippet_dialog_error); } } }
From source file:mil.nga.giat.mage.sdk.utils.MediaUtility.java
public static File copyMediaFromUri(Context context, Uri uri) throws IOException { InputStream is = null;//from www . j a va 2s. c o m OutputStream os = null; try { ContentResolver contentResolver = context.getContentResolver(); String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "MAGE_" + timeStamp; MimeTypeMap mime = MimeTypeMap.getSingleton(); String extension = "." + mime.getExtensionFromMimeType(contentResolver.getType(uri)); File directory = context.getExternalFilesDir("media"); File file = File.createTempFile(imageFileName, /* prefix */ extension, /* suffix */ directory /* directory */ ); is = contentResolver.openInputStream(uri); os = new FileOutputStream(file); ByteStreams.copy(is, os); return file; } finally { if (is != null) { try { is.close(); } catch (IOException e) { } } if (os != null) { try { os.close(); } catch (IOException e) { } } } }
From source file:br.com.viniciuscr.notification2android.mediaPlayer.MusicUtils.java
/** * Get album art for specified album. You should not pass in the album id * for the "unknown" album here (use -1 instead) *//*from w ww . j a v a 2 s. co m*/ public static Bitmap getArtwork(Context context, long song_id, long album_id, boolean allowdefault) { if (album_id < 0) { // This is something that is not in the database, so get the album art directly // from the file. if (song_id >= 0) { Bitmap bm = getArtworkFromFile(context, song_id, -1); if (bm != null) { return bm; } } if (allowdefault) { return getDefaultArtwork(context); } return null; } ContentResolver res = context.getContentResolver(); Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id); if (uri != null) { InputStream in = null; try { in = res.openInputStream(uri); return BitmapFactory.decodeStream(in, null, sBitmapOptions); } catch (FileNotFoundException ex) { // The album art thumbnail does not actually exist. Maybe the user deleted it, or // maybe it never existed to begin with. Bitmap bm = getArtworkFromFile(context, song_id, album_id); if (bm != null) { if (bm.getConfig() == null) { bm = bm.copy(Bitmap.Config.RGB_565, false); if (bm == null && allowdefault) { return getDefaultArtwork(context); } } } else if (allowdefault) { bm = getDefaultArtwork(context); } return bm; } finally { try { if (in != null) { in.close(); } } catch (IOException ex) { } } } return null; }
From source file:org.wbt11a.nativecamera.NativeCameraLauncher.java
public void onActivityResult(int requestCode, int resultCode, Intent intent) { // If image available if (resultCode == Activity.RESULT_OK) { int rotate = 0; try {// www.ja v a2s .c o m // Create an ExifHelper to save the exif data that is lost // during compression ExifHelper exif = new ExifHelper(); exif.createInFile(getTempDirectoryPath(this.cordova.getActivity().getApplicationContext()) + "/Pic-" + this.date + ".jpg"); exif.readExifData(); rotate = exif.getOrientation(); // Read in bitmap of captured image Bitmap bitmap; try { bitmap = android.provider.MediaStore.Images.Media .getBitmap(this.cordova.getActivity().getContentResolver(), imageUri); } catch (FileNotFoundException e) { Uri uri = intent.getData(); android.content.ContentResolver resolver = this.cordova.getActivity().getContentResolver(); bitmap = android.graphics.BitmapFactory.decodeStream(resolver.openInputStream(uri)); } // If bitmap cannot be decoded, this may return null if (bitmap == null) { this.failPicture("Error decoding image."); return; } bitmap = scaleBitmap(bitmap); // Add compressed version of captured image to returned media // store Uri bitmap = getRotatedBitmap(rotate, bitmap, exif); Log.i(LOG_TAG, "URI: " + this.imageUri.toString()); OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(this.imageUri); bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os); os.close(); // Restore exif data to file exif.createOutFile(this.imageUri.getPath()); exif.writeExifData(); // Send Uri back to JavaScript for viewing image this.callbackContext .sendPluginResult(new PluginResult(PluginResult.Status.OK, this.imageUri.toString())); bitmap.recycle(); bitmap = null; System.gc(); } catch (IOException e) { e.printStackTrace(); this.failPicture("Error capturing image."); } } // If cancelled else if (resultCode == Activity.RESULT_CANCELED) { this.failPicture("Camera cancelled."); } // If something else else { this.failPicture("Did not complete!"); } }
From source file:com.example.cuisoap.agrimac.homePage.machineDetail.driverInfoFragment.java
public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { license_uri = data.getData();//from ww w .j a va2s . c om Log.e("uri", license_uri.toString()); ContentResolver cr = context.getContentResolver(); try { Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(license_uri)); if (requestCode == 1) { //String path=getRealPathFromURI(uri); //System.out.println(path); bitmap = ImageUtils.comp(bitmap); license_pic.setImageBitmap(bitmap); license_pic.setVisibility(View.VISIBLE); license.setVisibility(View.GONE); license_pic.setOnClickListener(myOnClickListener); machineDetailData.driver_license_filepath = ImageUtils.saveMyBitmap(bitmap, null); } } catch (FileNotFoundException e) { Log.e("Exception", e.getMessage(), e); } } else { Toast.makeText(context, "?", Toast.LENGTH_SHORT).show(); } super.onActivityResult(requestCode, resultCode, data); }
From source file:com.narkii.security.info.LicenseInfoFragment.java
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); Log.d(TAG, "on activity result"); ///* w ww . j av a 2 s .c o m*/ if (requestCode == Constants.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { Log.d(TAG, "on activity result camera"); if (resultCode == Activity.RESULT_OK) { //data.getData()NullPointer // Toast.makeText(this, "Image saved to:\n" + // data.getData(), Toast.LENGTH_LONG).show(); ContentResolver cr = getActivity().getContentResolver(); try { preBitmap = BitmapFactory.decodeStream(cr.openInputStream(fileUri)); previewImage.setImageBitmap(preBitmap); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } isImage = true; } else if (resultCode == Activity.RESULT_CANCELED) { Toast.makeText(getActivity(), "cancel camera", Toast.LENGTH_LONG).show(); } else { Toast.makeText(getActivity(), "failed", Toast.LENGTH_LONG).show(); } } // if (requestCode == Constants.CONTENT_GET_ACTIVITY_REQUEST_CODE) { // if (resultCode == Activity.RESULT_OK) { fileUri = data.getData(); String path = fileUri.getPath(); Log.d(TAG, "uri= " + fileUri.getPath()); Log.d(TAG, "uri2= " + fileUri); if (path.contains("/external/images/media")) {// isImage = true; ContentResolver cr = getActivity().getContentResolver(); try { preBitmap = BitmapFactory.decodeStream(cr.openInputStream(fileUri)); previewImage.setImageBitmap(preBitmap); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else {// isImage = false; } } else if (resultCode == Activity.RESULT_CANCELED) { Toast.makeText(getActivity(), "cancel file select", Toast.LENGTH_LONG).show(); } else { Toast.makeText(getActivity(), "failed", Toast.LENGTH_LONG).show(); } } }
From source file:com.galois.qrstream.MainActivity.java
private byte[] readFileUri(Uri uri) throws IOException { ContentResolver contentResolver = getContentResolver(); AssetFileDescriptor fd = contentResolver.openAssetFileDescriptor(uri, "r"); long fileLength = fd.getLength(); Log.d(Constants.APP_TAG, "fd length " + fileLength); DataInputStream istream = new DataInputStream(contentResolver.openInputStream(uri)); byte[] buf = new byte[(int) fileLength]; istream.readFully(buf);/*from w ww . j av a 2 s. c o m*/ return buf; }
From source file:com.phonegap.customcamera.NativeCameraLauncher.java
@Override public void run() { WindowManager windowManager = (WindowManager) this.cordova.getActivity().getApplicationContext() .getSystemService(this.cordova.getActivity().getApplicationContext().WINDOW_SERVICE); int rotation = windowManager.getDefaultDisplay().getRotation(); // Read in bitmap of captured image Bitmap bitmap;//from www . j a va 2 s. c om ExifHelper exif = new ExifHelper(); //Log.i("orientation", rotation+" rotation"); int rotate = rotation; try { // Create an ExifHelper to save the exif data that is lost // during compression exif.createInFile(getTempDirectoryPath(this.cordova.getActivity().getApplicationContext()) + "/Pic-" + this.date + ".jpg"); exif.readExifData(); /*Auskommentiert weil es immer auf Querformat gedreht hat*/ // rotate = exif.getOrientation(); Log.i("orientation", rotate + " "); // rotate = 90; try { bitmap = android.provider.MediaStore.Images.Media .getBitmap(this.cordova.getActivity().getContentResolver(), imageUri); Log.i("orientation", bitmap.getWidth() + " " + bitmap.getHeight()); if (bitmap.getWidth() > bitmap.getHeight()) { rotate = rotate + 90; } } catch (FileNotFoundException e) { Uri uri = resultIntent.getData(); android.content.ContentResolver resolver = this.cordova.getActivity().getContentResolver(); bitmap = android.graphics.BitmapFactory.decodeStream(resolver.openInputStream(uri)); } // If bitmap cannot be decoded, this may return null if (bitmap == null) { this.failPicture("Error decoding image."); return; } bitmap = scaleBitmap(bitmap); // Add compressed version of captured image to returned media // store Uri bitmap = getRotatedBitmap(rotate, bitmap, exif); //Log.i(LOG_TAG, "URI: " + this.imageUri.toString()); OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(this.imageUri); // ByteArrayOutputStream outStream = new ByteArrayOutputStream(); // bitmap.compress(CompressFormat.JPEG, this.mQuality, outStream); bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os); // String imgString = Base64.encodeToString(outStream.toByteArray(), Base64.NO_WRAP); os.close(); // Restore exif data to file exif.createOutFile(this.imageUri.getPath()); exif.writeExifData(); // Send Uri back to JavaScript for viewing image this.callbackContext .sendPluginResult(new PluginResult(PluginResult.Status.OK, this.imageUri.getPath())); // this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, imgString)); bitmap.recycle(); bitmap = null; System.gc(); } catch (IOException e) { e.printStackTrace(); this.failPicture("Error capturing image."); } }
From source file:net.mutina.uclimb.ForegroundCameraLauncher.java
/** * Called when the camera view exits. /*from w w w . jav a 2 s. c o m*/ * * @param requestCode The request code originally supplied to startActivityForResult(), * allowing you to identify who this result came from. * @param resultCode The integer result code returned by the child activity through its setResult(). * @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras"). */ public void onActivityResult(int requestCode, int resultCode, Intent intent) { // If image available if (resultCode == Activity.RESULT_OK) { try { // Create an ExifHelper to save the exif data that is lost during compression ExifHelper exif = new ExifHelper(); exif.createInFile(getTempDirectoryPath(ctx.getContext()) + "/Pic.jpg"); exif.readExifData(); // Read in bitmap of captured image Bitmap bitmap; try { bitmap = android.provider.MediaStore.Images.Media.getBitmap(this.ctx.getContentResolver(), imageUri); } catch (FileNotFoundException e) { Uri uri = intent.getData(); android.content.ContentResolver resolver = this.ctx.getContentResolver(); bitmap = android.graphics.BitmapFactory.decodeStream(resolver.openInputStream(uri)); } bitmap = scaleBitmap(bitmap); // Create entry in media store for image // (Don't use insertImage() because it uses default compression setting of 50 - no way to change it) ContentValues values = new ContentValues(); values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); Uri uri = null; try { uri = this.ctx.getContentResolver() .insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); } catch (UnsupportedOperationException e) { LOG.d(LOG_TAG, "Can't write to external media storage."); try { uri = this.ctx.getContentResolver() .insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values); } catch (UnsupportedOperationException ex) { LOG.d(LOG_TAG, "Can't write to internal media storage."); this.failPicture("Error capturing image - no media storage found."); return; } } // Add compressed version of captured image to returned media store Uri OutputStream os = this.ctx.getContentResolver().openOutputStream(uri); bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os); os.close(); // Restore exif data to file exif.createOutFile(getRealPathFromURI(uri, this.ctx)); exif.writeExifData(); // Send Uri back to JavaScript for viewing image this.success(new PluginResult(PluginResult.Status.OK, uri.toString()), this.callbackId); bitmap.recycle(); bitmap = null; System.gc(); checkForDuplicateImage(); } catch (IOException e) { e.printStackTrace(); this.failPicture("Error capturing image."); } } // If cancelled else if (resultCode == Activity.RESULT_CANCELED) { this.failPicture("Camera cancelled."); } // If something else else { this.failPicture("Did not complete!"); } }