Example usage for android.widget ImageView setImageBitmap

List of usage examples for android.widget ImageView setImageBitmap

Introduction

In this page you can find the example usage for android.widget ImageView setImageBitmap.

Prototype

@android.view.RemotableViewMethod
public void setImageBitmap(Bitmap bm) 

Source Link

Document

Sets a Bitmap as the content of this ImageView.

Usage

From source file:Main.java

public static boolean setAssetImage(ImageView imageView, String path, String filename) {
    if (filename == null || "".equals(filename) || "NULL".equalsIgnoreCase(filename)) {
        return false;
    }/*from  www.  j  a  v  a2s .c om*/
    String _path = (path == null) ? "" : path;

    InputStream is = null;
    try {
        is = imageView.getContext().getAssets().open(_path + filename);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    Bitmap bitmap = BitmapFactory.decodeStream(is);
    imageView.setImageBitmap(bitmap);

    return true;
}

From source file:com.example.util.ImageUtils.java

/**
 * ICON//from  www . j ava  2s .c o  m
 */
public static void download(Context context, String url, ImageView imageView) {

    CacheManager cache = CacheManager.getInstance();
    if (cache.existsDrawable(url)) {
        imageView.setImageBitmap(cache.getDrawableFromCache(url));
        return;
    }

    Drawable defaultDrawable = context.getResources().getDrawable(R.drawable.loading_icon);
    if (cancelPotentialBitmapDownload(url, imageView)) {
        BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
        DownloadedDrawable1 downloadedDrawable = new DownloadedDrawable1(defaultDrawable, task);
        imageView.setImageDrawable(downloadedDrawable);
        task.execute(context, url, TYPE_NORAML);
    }
}

From source file:nya.miku.wishmaster.http.cloudflare.CloudflareUIHandler.java

/**
 *  ?-?  Cloudflare.//from w  ww  .  j  ava2  s  .c  o m
 *    
 * @param e ? {@link CloudflareException}
 * @param chan  
 * @param activity ?,    ?  ( ?  ? ),
 *   ?   ? WebView ? Anti DDOS  ? javascript.
 * ???  ?  UI  ({@link Activity#runOnUiThread(Runnable)})
 * @param cfTask ?? 
 * @param callback ? {@link Callback}
 */
static void handleCloudflare(final CloudflareException e, final HttpChanModule chan, final Activity activity,
        final CancellableTask cfTask, final InteractiveException.Callback callback) {
    if (cfTask.isCancelled())
        return;

    if (!e.isRecaptcha()) { // ? anti DDOS 
        if (!CloudflareChecker.getInstance().isAvaibleAntiDDOS()) {
            //?  ?   ??  ,   ?  ? ?
            // ?, ?      ChanModule,    
            //  ?  ?  () cloudflare  ? ?
            //  ?  ? ? ?   ? CloudflareChecker
            while (!CloudflareChecker.getInstance().isAvaibleAntiDDOS())
                Thread.yield();
            if (!cfTask.isCancelled())
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        callback.onSuccess();
                    }
                });
            return;
        }

        Cookie cfCookie = CloudflareChecker.getInstance().checkAntiDDOS(e, chan.getHttpClient(), cfTask,
                activity);
        if (cfCookie != null) {
            chan.saveCookie(cfCookie);
            if (!cfTask.isCancelled()) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        callback.onSuccess();
                    }
                });
            }
        } else if (!cfTask.isCancelled()) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    callback.onError(activity.getString(R.string.error_cloudflare_antiddos));
                }
            });
        }
    } else { //  ? 
        final Recaptcha recaptcha;
        try {
            recaptcha = CloudflareChecker.getInstance().getRecaptcha(e, chan.getHttpClient(), cfTask);
        } catch (RecaptchaException recaptchaException) {
            if (!cfTask.isCancelled())
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        callback.onError(activity.getString(R.string.error_cloudflare_get_captcha));
                    }
                });
            return;
        }

        if (!cfTask.isCancelled())
            activity.runOnUiThread(new Runnable() {
                @SuppressLint("InflateParams")
                @Override
                public void run() {
                    Context dialogContext = Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB
                            ? new ContextThemeWrapper(activity, R.style.Neutron_Medium)
                            : activity;
                    View view = LayoutInflater.from(dialogContext).inflate(R.layout.dialog_cloudflare_captcha,
                            null);
                    ImageView captchaView = (ImageView) view.findViewById(R.id.dialog_captcha_view);
                    final EditText captchaField = (EditText) view.findViewById(R.id.dialog_captcha_field);
                    captchaView.setImageBitmap(recaptcha.bitmap);

                    DialogInterface.OnClickListener process = new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (cfTask.isCancelled())
                                return;
                            PriorityThreadFactory.LOW_PRIORITY_FACTORY.newThread(new Runnable() {
                                @Override
                                public void run() {
                                    String answer = captchaField.getText().toString();
                                    Cookie cfCookie = CloudflareChecker.getInstance().checkRecaptcha(e,
                                            (ExtendedHttpClient) chan.getHttpClient(), cfTask,
                                            recaptcha.challenge, answer);
                                    if (cfCookie != null) {
                                        chan.saveCookie(cfCookie);
                                        if (!cfTask.isCancelled()) {
                                            activity.runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    callback.onSuccess();
                                                }
                                            });
                                        }
                                    } else {
                                        //   (?,  ,    )
                                        handleCloudflare(e, chan, activity, cfTask, callback);
                                    }
                                }
                            }).start();
                        }
                    };

                    DialogInterface.OnCancelListener onCancel = new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            callback.onError(activity.getString(R.string.error_cloudflare_cancelled));
                        }
                    };

                    if (cfTask.isCancelled())
                        return;

                    final AlertDialog recaptchaDialog = new AlertDialog.Builder(dialogContext).setView(view)
                            .setPositiveButton(R.string.dialog_cloudflare_captcha_check, process)
                            .setOnCancelListener(onCancel).create();
                    recaptchaDialog.getWindow()
                            .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    recaptchaDialog.setCanceledOnTouchOutside(false);
                    recaptchaDialog.show();

                    captchaView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            recaptchaDialog.dismiss();
                            if (cfTask.isCancelled())
                                return;
                            PriorityThreadFactory.LOW_PRIORITY_FACTORY.newThread(new Runnable() {
                                @Override
                                public void run() {
                                    handleCloudflare(e, chan, activity, cfTask, callback);
                                }
                            }).start();
                        }
                    });
                }
            });
    }
}

From source file:Main.java

public static void setImageViewWidthBitmap(String imgPath, ImageView imageView) throws IOException {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//  w  w  w . j  a v  a  2  s  . co m
    BitmapFactory.decodeFile(imgPath, options);
    options.inSampleSize = Math.min(options.outWidth / imageView.getWidth(),
            options.outHeight / imageView.getWidth());
    options.inJustDecodeBounds = false;
    options.inPurgeable = true;
    options.inInputShareable = true;

    FileInputStream fis = new FileInputStream(imgPath);
    imageView.setImageBitmap(BitmapFactory.decodeFileDescriptor(fis.getFD(), null, options));
}

From source file:com.micabyte.android.app.BaseActivity.java

private static void unbindViewReferences(View view) {
    // set all listeners to null
    try {//from  w  w  w .j a va 2  s  . com
        view.setOnClickListener(null);
    } catch (Throwable mayHappen) {
        // NOOP - not supported by all views/versions
    }
    try {
        view.setOnCreateContextMenuListener(null);
    } catch (Throwable mayHappen) {
        // NOOP - not supported by all views/versions
    }
    try {
        view.setOnFocusChangeListener(null);
    } catch (Throwable mayHappen) {
        // NOOP - not supported by all views/versions
    }
    try {
        view.setOnKeyListener(null);
    } catch (Throwable mayHappen) {
        // NOOP - not supported by all views/versions
    }
    try {
        view.setOnLongClickListener(null);
    } catch (Throwable mayHappen) {
        // NOOP - not supported by all views/versions
    }
    try {
        view.setOnClickListener(null);
    } catch (Throwable mayHappen) {
        // NOOP - not supported by all views/versions
    }
    // set background to null
    Drawable d = view.getBackground();
    if (d != null) {
        d.setCallback(null);
    }
    if (view instanceof ImageView) {
        final ImageView imageView = (ImageView) view;
        d = imageView.getDrawable();
        if (d != null) {
            d.setCallback(null);
        }
        imageView.setImageDrawable(null);
        imageView.setImageBitmap(null);
    }
    if (view instanceof ImageButton) {
        final ImageButton imageB = (ImageButton) view;
        d = imageB.getDrawable();
        if (d != null) {
            d.setCallback(null);
        }
        imageB.setImageDrawable(null);
    }
    // destroy WebView
    if (view instanceof WebView) {
        view.destroyDrawingCache();
        ((WebView) view).destroy();
    }
}

From source file:com.lovejoy777sarootool.rootool.preview.IconPreview.java

private static void queueJob(final File uri, final ImageView imageView) {
    /* Create handler in UI thread. */
    final Handler handler = new Handler() {
        @Override/*from w  ww  .  ja  v  a  2 s  . c  o m*/
        public void handleMessage(Message msg) {
            String tag = imageViews.get(imageView);
            if (tag != null && tag.equals(uri.getAbsolutePath())) {
                if (msg.obj != null) {
                    imageView.setImageBitmap((Bitmap) msg.obj);
                } else {
                    imageView.setImageBitmap(null);
                }
            }
        }
    };

    pool.submit(new Runnable() {
        public void run() {
            final Bitmap bmp = getPreview(uri);
            Message message = Message.obtain();
            message.obj = bmp;

            handler.sendMessage(message);
        }
    });
}

From source file:Main.java

public static void freeImageView(ImageView imageView) {
    // This code behaves differently on various OS builds. That is why put into try catch.
    try {//from   w  ww  .  ja  v a 2  s  .  c om
        if (imageView != null) {
            Drawable dr = imageView.getDrawable();

            if (dr == null) {
                return;
            }

            if (!(dr instanceof BitmapDrawable)) {
                return;
            }
            BitmapDrawable bd = (BitmapDrawable) imageView.getDrawable();
            if (bd.getBitmap() != null) {
                bd.getBitmap().recycle();
                imageView.setImageBitmap(null);
            }
        }
    } catch (Exception e) {
        Log.e("free image view", e.getMessage());
    }
}

From source file:com.battlelancer.seriesguide.util.Utils.java

/**
 * Tries to load a down-sized, center cropped version of the given TVDb show poster into the
 * given {@link android.widget.ImageView}.
 *
 * <p> The resize dimensions are those used for posters in the show list.
 *//*from  w  ww .ja v a 2 s  .c  o  m*/
public static void loadPosterThumbnail(Context context, ImageView imageView, String posterPath) {
    if (TextUtils.isEmpty(posterPath)) {
        // there is no image available
        imageView.setImageBitmap(null);
        return;
    }

    ServiceUtils.getPicasso(context).load(TheTVDB.buildPosterUrl(posterPath)).centerCrop()
            .resizeDimen(R.dimen.show_poster_width, R.dimen.show_poster_height)
            .error(R.drawable.ic_image_missing).into(imageView);
}

From source file:Main.java

static public void setImageColorPixels(ImageView view, Bitmap myBitmap, int rgbcolor)// ,Bitmap sourceBitmap)
{

    int intArray[];

    intArray = new int[myBitmap.getWidth() * myBitmap.getHeight()];

    // copy pixel data from the Bitmap into the 'intArray' array
    myBitmap.getPixels(intArray, 0, myBitmap.getHeight(), 0, 0, myBitmap.getHeight(), myBitmap.getWidth());

    // replace the red pixels with yellow ones
    for (int i = 0; i < intArray.length; i++) {
        // System.out.println("color is--" + i + " " + intArray[i]);
        if (intArray[i] != Color.TRANSPARENT) {

            intArray[i] = rgbcolor;//w  w w  .  j  av  a2  s  .  c om
        }
    }
    myBitmap.setPixels(intArray, 0, myBitmap.getHeight(), 0, 0, myBitmap.getHeight(), myBitmap.getWidth());

    view.setImageBitmap(myBitmap);
}

From source file:com.todoroo.astrid.notes.EditNoteActivity.java

private static void setupImagePopupForCommentView(View view, ImageView commentPictureView,
        final Uri updateBitmap, final Fragment fragment) {
    if (updateBitmap != null) { //$NON-NLS-1$
        commentPictureView.setVisibility(View.VISIBLE);
        String path = getPathFromUri(fragment.getActivity(), updateBitmap);
        commentPictureView.setImageBitmap(sampleBitmap(path, commentPictureView.getLayoutParams().width,
                commentPictureView.getLayoutParams().height));

        view.setOnClickListener(new OnClickListener() {
            @Override/*from ww  w . jav a 2  s . c  o m*/
            public void onClick(View v) {
                fragment.startActivity(new Intent(Intent.ACTION_VIEW) {
                    {
                        setDataAndType(updateBitmap, "image/*");
                    }
                });
            }
        });
    } else {
        commentPictureView.setVisibility(View.GONE);
    }
}