Example usage for android.net Uri getScheme

List of usage examples for android.net Uri getScheme

Introduction

In this page you can find the example usage for android.net Uri getScheme.

Prototype

@Nullable
public abstract String getScheme();

Source Link

Document

Gets the scheme of this URI.

Usage

From source file:com.ultramegasoft.flavordex2.util.PhotoUtils.java

/**
 * Rotate an image according to its EXIF data.
 *
 * @param context The Context/*from w ww  .  j  a  va2  s.  c om*/
 * @param uri     The Uri to the image file
 * @param bitmap  The Bitmap to rotate
 * @return The rotated Bitmap
 */
@NonNull
private static Bitmap rotatePhoto(@NonNull Context context, @NonNull Uri uri, @NonNull Bitmap bitmap) {
    uri = getImageUri(context, uri);
    int rotation = 0;

    if ("file".equals(uri.getScheme())) {
        try {
            final ExifInterface exif = new ExifInterface(uri.getPath());
            switch (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0)) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotation = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotation = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotation = 270;
            }
        } catch (IOException e) {
            Log.e(TAG, "Failed to read EXIF data for " + uri.toString(), e);
        }
    } else {
        final ContentResolver cr = context.getContentResolver();
        final String[] projection = new String[] { MediaStore.Images.ImageColumns.ORIENTATION };
        final Cursor cursor = cr.query(uri, projection, null, null, null);
        if (cursor != null) {
            try {
                if (cursor.moveToFirst() && cursor.getColumnCount() > 0) {
                    rotation = cursor.getInt(0);
                }
            } finally {
                cursor.close();
            }
        }
    }

    if (rotation != 0) {
        final Matrix matrix = new Matrix();
        matrix.postRotate(rotation);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    return bitmap;
}

From source file:mobisocial.musubi.objects.FileObj.java

public static Obj from(Context context, Uri dataUri) throws IOException {
    //TODO: is this the proper way to do it?
    if (dataUri == null) {
        throw new NullPointerException();
    }/*from   www  . ja v  a2s.  com*/
    ContentResolver cr = context.getContentResolver();
    InputStream in = cr.openInputStream(dataUri);
    long length = in.available();

    String ext;
    String mimeType;
    String filename;

    MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
    if ("content".equals(dataUri.getScheme())) {
        ContentResolver resolver = context.getContentResolver();
        mimeType = resolver.getType(dataUri);
        ext = mimeTypeMap.getExtensionFromMimeType(mimeType);
        filename = "Musubi-" + sDateFormat.format(new Date());
    } else {
        ext = MimeTypeMap.getFileExtensionFromUrl(dataUri.toString());
        mimeType = mimeTypeMap.getMimeTypeFromExtension(ext);
        filename = Uri.parse(dataUri.toString()).getLastPathSegment();
        if (filename == null) {
            filename = "Musubi-" + sDateFormat.format(new Date());
        }
    }

    if (mimeType == null || mimeType.isEmpty()) {
        throw new IOException("Unidentified mime type");
    }

    if (ext == null || ext.isEmpty()) {
        ext = mimeTypeMap.getExtensionFromMimeType(mimeType);
    }

    if (!ext.isEmpty() && !filename.endsWith(ext)) {
        filename = filename + "." + ext;
    }

    if (mimeType.startsWith("video/")) {
        return VideoObj.from(context, dataUri, mimeType);
    } else if (mimeType.startsWith("image/")) {
        return PictureObj.from(context, dataUri, true);
    }

    if (length > EMBED_SIZE_LIMIT) {
        if (length > CORRAL_SIZE_LIMIT) {
            throw new IOException("file too large for push");
        } else {
            return fromCorral(context, mimeType, filename, length, dataUri);
        }
    } else {
        in = cr.openInputStream(dataUri);
        return from(mimeType, filename, length, IOUtils.toByteArray(in));
    }
}

From source file:com.keepassdroid.PasswordActivity.java

public static void Launch(Activity act, String fileName, String keyFile) throws FileNotFoundException {
    if (EmptyUtils.isNullOrEmpty(fileName)) {
        throw new FileNotFoundException();
    }//  w ww .  ja v a2 s . c  o m

    Uri uri = UriUtil.parseDefaultFile(fileName);
    String scheme = uri.getScheme();

    if (!EmptyUtils.isNullOrEmpty(scheme) && scheme.equalsIgnoreCase("file")) {
        File dbFile = new File(uri.getPath());
        if (!dbFile.exists()) {
            throw new FileNotFoundException();
        }
    }

    Intent i = new Intent(act, PasswordActivity.class);
    i.putExtra(KEY_FILENAME, fileName);
    i.putExtra(KEY_KEYFILE, keyFile);

    act.startActivityForResult(i, 0);

}

From source file:fpt.isc.nshreport.utilities.FileUtils.java

/**
 * Get a file path from a Uri. This will get the the path for Storage Access
 * Framework Documents, as well as the _data field for the MediaStore and
 * other file-based ContentProviders.<br>
 * <br>//  ww  w.  j a v  a 2s.  c  o m
 * Callers should check whether the path is local before assuming it
 * represents a local file.
 *
 * @param context The context.
 * @param uri     The Uri to query.
 * @author paulburke
 * @see #isLocal(String)
 * @see #getFile(Context, Uri)
 */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static String getPath(final Context context, final Uri uri) {

    if (DEBUG)
        Log.d(TAG + " File -",
                "Authority: " + uri.getAuthority() + ", Fragment: " + uri.getFragment() + ", Port: "
                        + uri.getPort() + ", Query: " + uri.getQuery() + ", Scheme: " + uri.getScheme()
                        + ", Host: " + uri.getHost() + ", Segments: " + uri.getPathSegments().toString());

    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        // LocalStorageProvider
        //if (isLocalStorageDocument(uri)) {
        if (false) {
            // The path is the id
            return DocumentsContract.getDocumentId(uri);
        }
        // ExternalStorageProvider
        else if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }

            // TODO handle non-primary volumes
        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {

            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                    Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] { split[1] };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {

        // Return the remote address
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();

        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:com.applozic.mobicommons.file.FileUtils.java

/**
 * @param uri/* ww  w. j  a  v  a 2  s. co  m*/
 * @param context
 * @return
 */
public static String getFileName(Context context, Uri uri) {

    String fileName = null;
    if (uri.getScheme().equals("content")) {
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        if (cursor != null) {
            try {
                if (cursor.moveToFirst()) {
                    fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                cursor.close();
            }
        }
    }
    if (TextUtils.isEmpty(fileName)) {
        fileName = uri.getPath();
        int cut = fileName.lastIndexOf('/');
        if (cut != -1) {
            fileName = fileName.substring(cut + 1);
        }
    }
    return fileName;
}

From source file:com.pdftron.pdf.utils.Utils.java

public static String getUriDisplayName(Context context, Uri contentUri) {
    String displayName = null;//from   ww  w .  ja  v a 2  s . c o  m
    String[] projection = { OpenableColumns.DISPLAY_NAME };
    Cursor cursor = null;

    if (contentUri.getScheme().equalsIgnoreCase("file")) {
        return contentUri.getLastPathSegment();
    }
    try {
        cursor = context.getContentResolver().query(contentUri, projection, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            int nameIndex = cursor.getColumnIndexOrThrow(projection[0]);
            if (nameIndex >= 0) {
                displayName = cursor.getString(nameIndex);
            }
        }
    } catch (Exception e) {
        displayName = null;
    }
    if (cursor != null) {
        cursor.close();
    }
    return displayName;
}

From source file:com.applozic.mobicommons.file.FileUtils.java

public static boolean isContentScheme(Uri uri) {
    if (uri == null) {
        return false;
    }/*from  w  ww.  j av  a  2s .  c  o  m*/
    return "content".equalsIgnoreCase(uri.getScheme());
}

From source file:com.wuman.androidimageloader.ImageLoader.java

private static String getProtocol(String url) {
    Uri uri = Uri.parse(url);
    return uri.getScheme();
}

From source file:com.google.samples.apps.iosched.util.UIUtils.java

/**
 * If an activity's intent is for a Google I/O web URL that the app can handle natively, this
 * method translates the intent to the equivalent native intent.
 *///w  w  w.j  a  v  a 2 s.c  o m
public static void tryTranslateHttpIntent(Activity activity) {
    Intent intent = activity.getIntent();
    if (intent == null) {
        return;
    }

    Uri uri = intent.getData();
    if (uri == null || TextUtils.isEmpty(uri.getPath())) {
        return;
    }

    Uri sessionDetailWebUrlPrefix = Uri.parse(Config.SESSION_DETAIL_WEB_URL_PREFIX);
    String prefixPath = sessionDetailWebUrlPrefix.getPath();
    String path = uri.getPath();

    if (sessionDetailWebUrlPrefix.getScheme().equals(uri.getScheme())
            && sessionDetailWebUrlPrefix.getHost().equals(uri.getHost()) && path.startsWith(prefixPath)) {
        String sessionId = path.substring(prefixPath.length());
        activity.setIntent(
                new Intent(Intent.ACTION_VIEW, ScheduleContract.Sessions.buildSessionUri(sessionId)));
    }
}

From source file:com.forrestguice.suntimeswidget.LocationConfigView.java

public static Bundle bundleData(Uri data, String label) {
    String lat = "";
    String lon = "";

    if (data.getScheme().equals("geo")) {
        String dataString = data.getSchemeSpecificPart();
        String[] dataParts = dataString.split(Pattern.quote("?"));
        if (dataParts.length > 0) {
            String geoPath = dataParts[0];
            String[] geoParts = geoPath.split(Pattern.quote(","));
            if (geoParts.length >= 2) {
                lat = geoParts[0];//from  w  ww.ja v a2 s  .com
                lon = geoParts[1];
            }
        }
    }

    Bundle bundle = new Bundle();
    bundle.putString(KEY_DIALOGMODE, LocationViewMode.MODE_CUSTOM_ADD.name());
    bundle.putString(KEY_LOCATION_MODE, WidgetSettings.LocationMode.CUSTOM_LOCATION.name());
    bundle.putString(KEY_LOCATION_LATITUDE, lat);
    bundle.putString(KEY_LOCATION_LONGITUDE, lon);
    bundle.putString(KEY_LOCATION_LABEL, label);
    return bundle;
}