Example usage for android.net Uri EMPTY

List of usage examples for android.net Uri EMPTY

Introduction

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

Prototype

Uri EMPTY

To view the source code for android.net Uri EMPTY.

Click Source Link

Document

The empty URI, equivalent to "".

Usage

From source file:Main.java

private static Uri getUriFromAsset(Context mContext, String path) {
    File dir = mContext.getExternalCacheDir();

    if (dir == null) {
        Log.e(TAG, "Missing external cache dir");
        return Uri.EMPTY;
    }//from  www.j  a  v  a2 s . c  o  m
    String resPath = path.replaceFirst("file:/", "www");
    String fileName = resPath.substring(resPath.lastIndexOf('/') + 1);
    String storage = dir.toString() + STORAGE_FOLDER;

    if (fileName == null || fileName.isEmpty()) {
        Log.e(TAG, "Filename is missing");
        return Uri.EMPTY;
    }

    File file = new File(storage, fileName);
    FileOutputStream outStream = null;
    InputStream inputStream = null;

    try {
        File fileStorage = new File(storage);
        if (!fileStorage.mkdir())
            Log.e(TAG, "Storage directory could not be created: " + storage);

        AssetManager assets = mContext.getAssets();
        outStream = new FileOutputStream(file);
        inputStream = assets.open(resPath);

        copyFile(inputStream, outStream);
        outStream.flush();
        outStream.close();
        return Uri.fromFile(file);
    } catch (FileNotFoundException e) {
        Log.e(TAG, "File not found: assets/" + resPath);
    } catch (IOException ioe) {
        Log.e(TAG, "IOException occured");
    } catch (SecurityException secEx) {
        Log.e(TAG, "SecurityException: directory creation denied");
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outStream != null) {
                outStream.flush();
                outStream.close();
            }
        } catch (IOException ioe) {
            Log.e(TAG, "IOException occured while closing/flushing streams");
        }
    }
    return Uri.EMPTY;
}

From source file:org.andstatus.app.util.UriUtils.java

/**
 * @return true for null also
 */
public static boolean isEmpty(Uri uri) {
    return Uri.EMPTY.equals(notNull(uri));
}

From source file:org.andstatus.app.util.UriUtils.java

@NonNull
public static Uri fromJson(JSONObject jso, String urlTag) {
    if (jso != null && !TextUtils.isEmpty(urlTag) && jso.has(urlTag)) {
        return fromString(jso.optString(urlTag));
    }/*from   w  w w .  j a  va  2  s. c  o m*/
    return Uri.EMPTY;
}

From source file:org.andstatus.app.util.UriUtils.java

@NonNull
public static Uri fromString(String strUri) {
    return SharedPreferencesUtil.isEmpty(strUri) ? Uri.EMPTY : Uri.parse(strUri.trim());
}

From source file:org.andstatus.app.util.UriUtils.java

@NonNull
public static Uri notNull(Uri uri) {
    return uri == null ? Uri.EMPTY : uri;
}

From source file:org.andstatus.app.util.UriUtils.java

@NonNull
public static Uri fromUrl(URL url) {
    if (url == null) {
        return Uri.EMPTY;
    } else {//ww w .  j  a  v  a2s . c om
        return fromString(url.toExternalForm());
    }
}

From source file:org.andstatus.app.util.UrlUtils.java

public static URL fromUri(Uri uri) {
    if (uri == null || uri == Uri.EMPTY) {
        return null;
    } else {//  w ww .j a  v  a2s  .c o  m
        return fromString(uri.toString());
    }
}

From source file:Main.java

private static Uri getUriFromPath(String path) {
    String absPath = path.replaceFirst("file://", "");
    File file = new File(absPath);

    if (!file.exists()) {
        Log.e(TAG, "File not found: " + file.getAbsolutePath());
        return Uri.EMPTY;
    }// w ww.  j av a 2s.  co m

    return Uri.fromFile(file);
}

From source file:org.andstatus.app.util.UriUtilsTest.java

public void testNotNull() {
    assertEquals(UriUtils.notNull(null), Uri.EMPTY);
    Uri uri = Uri.parse("http://some.org/");
    assertEquals(UriUtils.notNull(uri), uri);
}

From source file:com.maskyn.fileeditorpro.util.AccessStorageApi.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.//ww w  .j  ava  2  s .  c  om
 *
 * @param context The context.
 * @param uri     The Uri to query.
 * @author paulburke
 */
@SuppressLint("NewApi")
public static String getPath(final Context context, final Uri uri) {

    String path = "";

    if (uri == null || uri.equals(Uri.EMPTY))
        return "";

    try {
        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
        // DocumentProvider
        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
            if (isTurboDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                path = "/" + split[1];
            }
            // 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)) {
                    path = 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));

                path = 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] };

                path = getDataColumn(context, contentUri, selection, selectionArgs);
            }
        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {
            path = getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            path = uri.getPath();
        }
    } catch (Exception ex) {
        return "";
    }

    return path;
}