Search in sources :

Example 76 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project AnExplorer by 1hakr.

the class DocumentsProvider method openAssetFile.

/**
 * Implementation is provided by the parent class. Cannot be overriden.
 *
 * @see #openDocument(String, String, CancellationSignal)
 */
@Override
public final AssetFileDescriptor openAssetFile(Uri uri, String mode, CancellationSignal signal) throws FileNotFoundException {
    enforceTree(uri);
    final ParcelFileDescriptor fd = openDocument(getDocumentId(uri), mode, signal);
    return fd != null ? new AssetFileDescriptor(fd, 0, -1) : null;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) ParcelFileDescriptor(android.os.ParcelFileDescriptor)

Example 77 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project AnExplorer by 1hakr.

the class DocumentsProvider method openAssetFile.

/**
 * Implementation is provided by the parent class. Cannot be overriden.
 *
 * @see #openDocumentThumbnail(String, Point, CancellationSignal)
 */
@Override
public final AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
    enforceTree(uri);
    final ParcelFileDescriptor fd = openDocument(getDocumentId(uri), mode, null);
    return fd != null ? new AssetFileDescriptor(fd, 0, -1) : null;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) ParcelFileDescriptor(android.os.ParcelFileDescriptor)

Example 78 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project AnExplorer by 1hakr.

the class DocumentArchive method openDocumentThumbnail.

/**
 * Opens a thumbnail of a file within an archive.
 */
public AssetFileDescriptor openDocumentThumbnail(String documentId, Point sizeHint, final CancellationSignal signal) throws FileNotFoundException {
    final ParsedDocumentId parsedId = ParsedDocumentId.fromDocumentId(documentId, mIdDelimiter);
    Preconditions.checkArgumentEquals(mDocumentId, parsedId.mArchiveId, "Mismatching document ID. Expected: %s, actual: %s.");
    Preconditions.checkArgumentNotNull(parsedId.mPath, "Not a document within an archive.");
    Preconditions.checkArgument(getDocumentType(documentId).startsWith("image/"), "Thumbnails only supported for image/* MIME type.");
    final ZipEntry entry = mEntries.get(parsedId.mPath);
    if (entry == null) {
        throw new FileNotFoundException();
    }
    if (!Utils.hasKitKat()) {
        return new AssetFileDescriptor(openDocument(documentId, "r", signal), 0, entry.getSize());
    }
    InputStream inputStream = null;
    try {
        inputStream = mZipFile.getInputStream(entry);
        final ExifInterface exif = new ExifInterface(inputStream);
        if (exif.hasThumbnail()) {
            Bundle extras = null;
            switch(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1)) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    extras = new Bundle(1);
                    extras.putInt(DocumentsContract.EXTRA_ORIENTATION, 90);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    extras = new Bundle(1);
                    extras.putInt(DocumentsContract.EXTRA_ORIENTATION, 180);
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    extras = new Bundle(1);
                    extras.putInt(DocumentsContract.EXTRA_ORIENTATION, 270);
                    break;
            }
            final long[] thumb = exif.getThumbnailRange();
            if (thumb != null) {
                return new AssetFileDescriptor(openDocument(documentId, "r", signal), thumb[0], thumb[1], extras);
            }
        }
    } catch (IOException e) {
        // Ignore the exception, as reading the EXIF may legally fail.
        Log.e(TAG, "Failed to obtain thumbnail from EXIF.", e);
        CrashReportingManager.logException(e);
    } finally {
        IoUtils.closeQuietly(inputStream);
    }
    return new AssetFileDescriptor(openDocument(documentId, "r", signal), 0, entry.getSize(), null);
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) InputStream(java.io.InputStream) Bundle(android.os.Bundle) ZipEntry(java.util.zip.ZipEntry) ExifInterface(android.support.media.ExifInterface) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 79 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project vialer-android by VoIPGRID.

the class DialerActivity method loadContactThumbnail.

/**
 * Function to get the bitmap from the uri.
 *
 * @param thumbnailUriString String uri of the file
 * @return Bitmap of the uri.
 */
private Bitmap loadContactThumbnail(String thumbnailUriString) {
    Uri thumbUri = Uri.parse(thumbnailUriString);
    AssetFileDescriptor afd = null;
    try {
        afd = getContentResolver().openAssetFileDescriptor(thumbUri, "r");
        FileDescriptor fileDescriptor = afd.getFileDescriptor();
        if (fileDescriptor != null) {
            // Decodes the bitmap
            return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, null);
        }
    } catch (FileNotFoundException e) {
    } finally {
        if (afd != null) {
            try {
                afd.close();
            } catch (IOException e) {
            }
        }
    }
    return null;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Uri(android.net.Uri) AssetFileDescriptor(android.content.res.AssetFileDescriptor) FileDescriptor(java.io.FileDescriptor)

Example 80 with AssetFileDescriptor

use of android.content.res.AssetFileDescriptor in project Osmand by osmandapp.

the class AudioVideoNotesPlugin method takePhotoWithCamera.

private void takePhotoWithCamera(final double lat, final double lon, final MapActivity mapActivity) {
    try {
        lastTakingPhoto = getBaseFileName(lat, lon, app, IMG_EXTENSION);
        final Camera.Size mPreviewSize;
        Parameters parameters = cam.getParameters();
        List<Camera.Size> psps = parameters.getSupportedPictureSizes();
        int camPicSizeIndex = AV_CAMERA_PICTURE_SIZE.get();
        // camera picture size
        log.debug("takePhotoWithCamera() index=" + camPicSizeIndex);
        if (camPicSizeIndex == AV_PHOTO_SIZE_DEFAULT) {
            camPicSizeIndex = cameraPictureSizeDefault;
            log.debug("takePhotoWithCamera() Default value of picture size. Set index to cameraPictureSizeDefault. Now index=" + camPicSizeIndex);
        }
        final Camera.Size selectedCamPicSize = psps.get(camPicSizeIndex);
        if (mSupportedPreviewSizes != null) {
            int width = selectedCamPicSize.width;
            int height = selectedCamPicSize.height;
            mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
        } else {
            mPreviewSize = null;
        }
        final SurfaceView view;
        if (mPreviewSize != null) {
            view = recordingMenu.prepareSurfaceView(mPreviewSize.width, mPreviewSize.height);
        } else {
            view = recordingMenu.prepareSurfaceView();
        }
        view.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        view.getHolder().addCallback(new Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try {
                    // load sound befor shot
                    if (AV_PHOTO_PLAY_SOUND.get()) {
                        if (sp == null)
                            sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
                        if (shotId == 0) {
                            try {
                                AssetFileDescriptor assetFileDescriptor = app.getAssets().openFd("sounds/camera_click.ogg");
                                shotId = sp.load(assetFileDescriptor, 1);
                                assetFileDescriptor.close();
                            } catch (Exception e) {
                                log.error("cannot get shotId for sounds/camera_click.ogg");
                            }
                        }
                    }
                    Parameters parameters = cam.getParameters();
                    parameters.setPictureSize(selectedCamPicSize.width, selectedCamPicSize.height);
                    log.debug("takePhotoWithCamera() set Picture size: width=" + selectedCamPicSize.width + " height=" + selectedCamPicSize.height);
                    // camera focus type
                    autofocus = true;
                    parameters.removeGpsData();
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                        parameters.setGpsLatitude(lat);
                        parameters.setGpsLongitude(lon);
                    }
                    switch(AV_CAMERA_FOCUS_TYPE.get()) {
                        case AV_CAMERA_FOCUS_HIPERFOCAL:
                            parameters.setFocusMode(Parameters.FOCUS_MODE_FIXED);
                            autofocus = false;
                            log.info("Osmand:AudioNotes set camera FOCUS_MODE_FIXED");
                            break;
                        case AV_CAMERA_FOCUS_EDOF:
                            parameters.setFocusMode(Parameters.FOCUS_MODE_EDOF);
                            autofocus = false;
                            log.info("Osmand:AudioNotes set camera FOCUS_MODE_EDOF");
                            break;
                        case AV_CAMERA_FOCUS_INFINITY:
                            parameters.setFocusMode(Parameters.FOCUS_MODE_INFINITY);
                            autofocus = false;
                            log.info("Osmand:AudioNotes set camera FOCUS_MODE_INFINITY");
                            break;
                        case AV_CAMERA_FOCUS_MACRO:
                            parameters.setFocusMode(Parameters.FOCUS_MODE_MACRO);
                            log.info("Osmand:AudioNotes set camera FOCUS_MODE_MACRO");
                            break;
                        case AV_CAMERA_FOCUS_CONTINUOUS:
                            parameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
                            log.info("Osmand:AudioNotes set camera FOCUS_MODE_CONTINUOUS_PICTURE");
                            break;
                        default:
                            parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);
                            log.info("Osmand:AudioNotes set camera FOCUS_MODE_AUTO");
                            break;
                    }
                    if (parameters.getSupportedWhiteBalance() != null && parameters.getSupportedWhiteBalance().contains(Parameters.WHITE_BALANCE_AUTO)) {
                        parameters.setWhiteBalance(Parameters.WHITE_BALANCE_AUTO);
                    }
                    if (parameters.getSupportedFlashModes() != null && parameters.getSupportedFlashModes().contains(Parameters.FLASH_MODE_AUTO)) {
                    // parameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
                    }
                    int cameraOrientation = getCamOrientation(mapActivity, Camera.CameraInfo.CAMERA_FACING_BACK);
                    cam.setDisplayOrientation(cameraOrientation);
                    parameters.set("rotation", cameraOrientation);
                    if (mPreviewSize != null) {
                        parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
                    }
                    cam.setParameters(parameters);
                    cam.setPreviewDisplay(holder);
                    cam.startPreview();
                    internalShoot();
                } catch (Exception e) {
                    logErr(e);
                    closeRecordingMenu();
                    closeCamera();
                    finishRecording();
                    e.printStackTrace();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            }
        });
        recordingMenu.show();
    } catch (RuntimeException e) {
        logErr(e);
        closeCamera();
    }
}
Also used : Parameters(android.hardware.Camera.Parameters) AssetFileDescriptor(android.content.res.AssetFileDescriptor) SoundPool(android.media.SoundPool) GeoParsedPoint(net.osmand.util.GeoPointParserUtil.GeoParsedPoint) IOException(java.io.IOException) SurfaceHolder(android.view.SurfaceHolder) PictureCallback(android.hardware.Camera.PictureCallback) Callback(android.view.SurfaceHolder.Callback) Camera(android.hardware.Camera) SurfaceView(android.view.SurfaceView)

Aggregations

AssetFileDescriptor (android.content.res.AssetFileDescriptor)262 IOException (java.io.IOException)147 ParcelFileDescriptor (android.os.ParcelFileDescriptor)57 FileNotFoundException (java.io.FileNotFoundException)56 MediaPlayer (android.media.MediaPlayer)44 ContentResolver (android.content.ContentResolver)36 Test (org.junit.Test)31 Uri (android.net.Uri)30 FileInputStream (java.io.FileInputStream)30 InputStream (java.io.InputStream)23 RemoteException (android.os.RemoteException)22 File (java.io.File)21 Bundle (android.os.Bundle)16 FileDescriptor (java.io.FileDescriptor)16 Nullable (android.annotation.Nullable)15 Bitmap (android.graphics.Bitmap)15 Parcel (android.os.Parcel)14 Resources (android.content.res.Resources)13 BitmapFactory (android.graphics.BitmapFactory)13 DeadObjectException (android.os.DeadObjectException)12