arnaucube
arnaucube/openEventsPlatformAppPublic Self-hosted repository
openEventsPlatformApp/plugins/cordova-plugin-camera/src/android/CordovaUri.java
plugins/cordova-plugin-camera/src/android/CordovaUri.javaRaw
  1/*
  2       Licensed to the Apache Software Foundation (ASF) under one
  3       or more contributor license agreements.  See the NOTICE file
  4       distributed with this work for additional information
  5       regarding copyright ownership.  The ASF licenses this file
  6       to you under the Apache License, Version 2.0 (the
  7       "License"); you may not use this file except in compliance
  8       with the License.  You may obtain a copy of the License at
  9
 10         http://www.apache.org/licenses/LICENSE-2.0
 11
 12       Unless required by applicable law or agreed to in writing,
 13       software distributed under the License is distributed on an
 14       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15       KIND, either express or implied.  See the License for the
 16       specific language governing permissions and limitations
 17       under the License.
 18*/
 19
 20package org.apache.cordova.camera;
 21
 22import android.net.Uri;
 23import android.os.Build;
 24import android.os.Environment;
 25import android.support.v4.content.FileProvider;
 26
 27import java.io.File;
 28
 29/*
 30 * This class exists because Andorid FilesProvider doesn't work on Android 4.4.4 and below and throws
 31 * weird errors.  I'm not sure why writing to shared cache directories is somehow verboten, but it is
 32 * and this error is irritating for a Compatibility library to have.
 33 *
 34 */
 35
 36public class CordovaUri {
 37
 38    private Uri androidUri;
 39    private String fileName;
 40    private Uri fileUri;
 41
 42    /*
 43     * We always expect a FileProvider string to be passed in for the file that we create
 44     *
 45     */
 46    CordovaUri (Uri inputUri)
 47    {
 48        //Determine whether the file is a content or file URI
 49        if(inputUri.getScheme().equals("content"))
 50        {
 51            androidUri = inputUri;
 52            fileName = getFileNameFromUri(androidUri);
 53            fileUri = Uri.parse("file://" + fileName);
 54        }
 55        else
 56        {
 57            fileUri = inputUri;
 58            fileName = FileHelper.stripFileProtocol(inputUri.toString());
 59        }
 60    }
 61
 62    public Uri getFileUri()
 63    {
 64        return fileUri;
 65    }
 66
 67    public String getFilePath()
 68    {
 69        return fileName;
 70    }
 71
 72    /*
 73     * This only gets called by takePicture
 74     */
 75
 76    public Uri getCorrectUri()
 77    {
 78        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
 79            return androidUri;
 80        else
 81            return fileUri;
 82    }
 83
 84 /*
 85  * This is dirty, but it does the job.
 86  *
 87  * Since the FilesProvider doesn't really provide you a way of getting a URL from the file,
 88  * and since we actually need the Camera to create the file for us most of the time, we don't
 89  * actually write the file, just generate the location based on a timestamp, we need to get it
 90  * back from the Intent.
 91  *
 92  * However, the FilesProvider preserves the path, so we can at least write to it from here, since
 93  * we own the context in this case.
 94 */
 95
 96    private String getFileNameFromUri(Uri uri) {
 97        String fullUri = uri.toString();
 98        String partial_path = fullUri.split("external_files")[1];
 99        File external_storage = Environment.getExternalStorageDirectory();
100        String path = external_storage.getAbsolutePath() + partial_path;
101        return path;
102
103    }
104}