List of usage examples for android.webkit URLUtil isValidUrl
public static boolean isValidUrl(String url)
From source file:de.vanita5.twittnuker.util.Utils.java
public static boolean isValidUrl(final CharSequence text) { if (TextUtils.isEmpty(text)) return false; return URLUtil.isValidUrl(text.toString()); }
From source file:self.philbrown.droidQuery.$.java
/** * For `ImageView`s, this will set the image to the given asset or url. Otherwise, it will set the * background image for the selected views. * @param source asset path, file path (starting with "file://") or URL to image * @param width specifies the output bitmap width * @param height specifies the output bitmap height * @param error if the given source is a file or asset, this receives a droidQuery wrapping the * current context and the {@code Throwable} error. Otherwise, this will receive an * Ajax error./*from w ww . j a v a2 s. co m*/ * @return this * @see AjaxOptions#error(Function) */ public $ image(final String source, int width, int height, final Function error) { if (source.startsWith("file://")) { try { BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inPreferredConfig = Bitmap.Config.ARGB_8888; if (width >= 0) opt.outWidth = width; if (height >= 0) opt.outHeight = height; Bitmap bitmap = BitmapFactory.decodeFile(source.substring(6), opt); for (View v : views) { if (v instanceof ImageView) { try { ((ImageView) v).setImageBitmap(Bitmap.createBitmap(bitmap)); } catch (Throwable t) { if (error != null) error.invoke($.with(context), t); } } else { v.setBackgroundDrawable(new BitmapDrawable(Bitmap.createBitmap(bitmap))); } } } catch (Throwable t) { if (error != null) { error.invoke($.with(context), t); } } } else if (URLUtil.isValidUrl(source)) { AjaxOptions options = new AjaxOptions().url(source).type("GET").dataType("image").context(context) .global(false).redundancy(Redundancy.RESPOND_TO_ALL_LISTENERS).success(new Function() { @Override public void invoke($ droidQuery, Object... params) { Bitmap bitmap = (Bitmap) params[0]; for (View v : views) { if (v instanceof ImageView) { try { ((ImageView) v).setImageBitmap(Bitmap.createBitmap(bitmap)); } catch (Throwable t) { if (error != null) error.invoke($.with(context), t); } } else { v.setBackgroundDrawable(new BitmapDrawable(Bitmap.createBitmap(bitmap))); } } } }); if (error != null) { options.error(error); } if (width >= 0) { options.imageWidth(width); } if (height >= 0) { options.imageHeight(height); } $.ajax(options); } else { try { BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inSampleSize = 1; opt.inPurgeable = true; opt.inInputShareable = false; if (width >= 0) opt.outWidth = width; if (height >= 0) opt.outHeight = height; Bitmap bitmap = BitmapFactory.decodeStream(context.getAssets().open(source), new Rect(0, 0, 0, 0), opt); for (View v : views) { if (v instanceof ImageView) { try { ((ImageView) v).setImageBitmap(Bitmap.createBitmap(bitmap)); } catch (Throwable t) { if (error != null) error.invoke($.with(context), t); } } else { v.setBackgroundDrawable(new BitmapDrawable(Bitmap.createBitmap(bitmap))); } } } catch (Throwable t) { if (error != null) { error.invoke($.with(context), t); } } } return this; }
From source file:self.philbrown.droidQuery.$.java
/** * Adds an Image over each selected View as a mask. * In most cases, this mask can be retrieved by querying siblings. For example: * <pre>/* w w w . j ava 2 s . com*/ * ImageView mask = (ImageView) $.with(myView).parent().selectChildren().selectImages().view(0); * </pre> * @param source asset path, file path (starting with "file://") or URL to image * @param width specifies the output bitmap width * @param height specifies the output bitmap height * @param error if the given source is a file or asset, this receives a droidQuery wrapping the * current context and the {@code Throwable} error. Otherwise, this will receive an * Ajax error. * @return this * @see AjaxOptions#error(Function) */ public $ mask(String source, int width, int height, Function error) { if (source.startsWith("file://")) { try { BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inPreferredConfig = Bitmap.Config.ARGB_8888; if (width >= 0) opt.outWidth = width; if (height >= 0) opt.outHeight = height; Bitmap bitmap = BitmapFactory.decodeFile(source.substring(6), opt); for (View v : views) { ImageView image = new ImageView(context); image.setImageBitmap(Bitmap.createBitmap(bitmap)); image.setScaleType(ScaleType.FIT_XY); ViewParent parent = v.getParent(); if (parent != null && parent instanceof ViewGroup) { image.setLayoutParams(v.getLayoutParams()); ((ViewGroup) parent).addView(image); } else if (v instanceof ViewGroup) { image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); ((ViewGroup) v).addView(image); } } } catch (Throwable t) { if (error != null) { error.invoke($.with(context), t); } } } else if (URLUtil.isValidUrl(source)) { AjaxOptions options = new AjaxOptions().url(source).type("GET").dataType("image").context(context) .global(false).success(new Function() { @Override public void invoke($ droidQuery, Object... params) { Bitmap bitmap = (Bitmap) params[0]; for (View v : views) { ImageView image = new ImageView(context); image.setImageBitmap(Bitmap.createBitmap(bitmap)); image.setScaleType(ScaleType.FIT_XY); ViewParent parent = v.getParent(); if (parent != null && parent instanceof ViewGroup) { image.setLayoutParams(v.getLayoutParams()); ((ViewGroup) parent).addView(image); } else if (v instanceof ViewGroup) { image.setLayoutParams( new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); ((ViewGroup) v).addView(image); } } } }); if (error != null) { options.error(error); } if (width >= 0) { options.imageWidth(width); } if (height >= 0) { options.imageHeight(height); } $.ajax(options); } else { try { BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inSampleSize = 1; opt.inPurgeable = true; opt.inInputShareable = false; if (width >= 0) opt.outWidth = width; if (height >= 0) opt.outHeight = height; Bitmap bitmap = BitmapFactory.decodeStream(context.getAssets().open(source), new Rect(0, 0, 0, 0), opt); for (View v : views) { ImageView image = new ImageView(context); image.setImageBitmap(Bitmap.createBitmap(bitmap)); image.setScaleType(ScaleType.FIT_XY); ViewParent parent = v.getParent(); if (parent != null && parent instanceof ViewGroup) { image.setLayoutParams(v.getLayoutParams()); ((ViewGroup) parent).addView(image); } else if (v instanceof ViewGroup) { image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); ((ViewGroup) v).addView(image); } } } catch (Throwable t) { if (error != null) { error.invoke($.with(context), t); } } } return this; }