Example usage for android.graphics BitmapFactory decodeByteArray

List of usage examples for android.graphics BitmapFactory decodeByteArray

Introduction

In this page you can find the example usage for android.graphics BitmapFactory decodeByteArray.

Prototype

public static Bitmap decodeByteArray(byte[] data, int offset, int length) 

Source Link

Document

Decode an immutable bitmap from the specified byte array.

Usage

From source file:com.dv.sharer.mobile.controllers.ImageController.java

public Bitmap convertImageToBitmap(Image image) {
    byte[] imageData = image.getData();
    Bitmap bmp = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
    return bmp;//from ww  w  .j  a va  2s  .c o m
}

From source file:mobisocial.musubi.ui.fragments.ChooseImageDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new AlertDialog.Builder(getActivity()).setTitle("Choose an Image...")
            .setItems(new String[] { "From Camera", "From Gallery" }, new DialogInterface.OnClickListener() {
                @SuppressWarnings("deprecation")
                @Override/* ww  w. java 2 s  .  c  o  m*/
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                    case 0:
                        final Activity activity = getActivity();
                        Toast.makeText(activity, "Loading camera...", Toast.LENGTH_SHORT).show();
                        ((InstrumentedActivity) activity)
                                .doActivityForResult(new PhotoTaker(activity, new PhotoTaker.ResultHandler() {
                                    @Override
                                    public void onResult(Uri imageUri) {
                                        Log.d(getClass().getSimpleName(), "Updating thumbnail...");

                                        try {
                                            UriImage image = new UriImage(activity, imageUri);
                                            byte[] data = image.getResizedImageData(512, 512,
                                                    PictureObj.MAX_IMAGE_SIZE / 2);
                                            // profile
                                            Bitmap sourceBitmap = BitmapFactory.decodeByteArray(data, 0,
                                                    data.length);
                                            int width = sourceBitmap.getWidth();
                                            int height = sourceBitmap.getHeight();
                                            int cropSize = Math.min(width, height);
                                            Bitmap cropped = Bitmap.createBitmap(sourceBitmap, 0, 0, cropSize,
                                                    cropSize);
                                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                            cropped.compress(Bitmap.CompressFormat.JPEG, 90, baos);
                                            cropped.recycle();
                                            sourceBitmap.recycle();

                                            Bundle bundle = new Bundle();
                                            bundle.putByteArray(EXTRA_THUMBNAIL, baos.toByteArray());
                                            Intent res = new Intent();
                                            res.putExtras(bundle);
                                            getTargetFragment().onActivityResult(REQUEST_PROFILE_PICTURE,
                                                    Activity.RESULT_OK, res);
                                        } catch (Throwable t) {
                                            Log.e("ViewProfile", "failed to generate thumbnail of profile", t);
                                            Toast.makeText(activity,
                                                    "Profile picture capture failed.  Try again.",
                                                    Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                }, 200, false));
                        break;
                    case 1:
                        Intent gallery = new Intent(Intent.ACTION_GET_CONTENT);
                        gallery.setType("image/*");
                        // god damn fragments.
                        getTargetFragment().startActivityForResult(Intent.createChooser(gallery, null),
                                REQUEST_GALLERY_THUMBNAIL);
                        break;
                    }
                }
            }).create();
}

From source file:com.keepassdroid.icons.DrawableFactory.java

public Drawable getIconDrawable(Resources res, PwIconCustom icon) {
    initBlank(res);/*ww  w . j  av a 2 s  . com*/
    if (icon == null) {
        return blank;
    }

    Drawable draw = (Drawable) customIconMap.get(icon.uuid);

    if (draw == null) {
        if (icon.imageData == null) {
            return blank;
        }

        Bitmap bitmap = BitmapFactory.decodeByteArray(icon.imageData, 0, icon.imageData.length);

        // Could not understand custom icon
        if (bitmap == null) {
            return blank;
        }

        bitmap = resize(bitmap);

        draw = BitmapDrawableCompat.getBitmapDrawable(res, bitmap);
        customIconMap.put(icon.uuid, draw);
    }

    return draw;
}

From source file:com.hackeruproj.android.havatzfit.general_utilities.GenUtils.java

public static Bitmap string2Bitmap(String encodeString) {
    try {//from   w  w w.  java 2s  .  c  om
        byte[] encodeByte = Base64.decode(encodeString, Base64.DEFAULT);
        Bitmap mBitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
        return mBitmap;
    } catch (Exception e) {
        e.getMessage();
        return null;
    }
}

From source file:net.potterpcs.recipebook.DownloadImageTask.java

private Bitmap downloadImage(String... urls) {
    Bitmap bitmap = null;//from   w  w  w.  ja  va 2  s .  c om
    RecipeData data = ((RecipeBook) parent.getApplication()).getData();

    AndroidHttpClient client = AndroidHttpClient.newInstance("A to Z Recipes for Android");

    if (data.isCached(urls[0])) {
        // Retrieve a cached image if we have one
        String pathName = data.findCacheEntry(urls[0]);
        Uri pathUri = Uri.fromFile(new File(parent.getCacheDir(), pathName));
        try {
            bitmap = RecipeBook.decodeScaledBitmap(parent, pathUri);
        } catch (IOException e) {
            e.printStackTrace();
            bitmap = null;
        }
    } else {
        try {
            // If the image isn't in the cache, we have to go and get it.
            // First, we set up the HTTP request.
            HttpGet request = new HttpGet(urls[0]);
            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setSoTimeout(params, 60000);
            request.setParams(params);

            // Let the UI know we're working.
            publishProgress(25);

            // Retrieve the image from the network.
            HttpResponse response = client.execute(request);
            publishProgress(50);

            // Create a bitmap to put in the ImageView.
            byte[] image = EntityUtils.toByteArray(response.getEntity());
            bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
            publishProgress(75);

            // Cache the file for offline use, and to lower data usage.
            File cachePath = parent.getCacheDir();
            String cacheFile = "recipecache-" + Long.toString(System.currentTimeMillis());
            if (bitmap.compress(Bitmap.CompressFormat.PNG, 0,
                    new FileOutputStream(new File(cachePath, cacheFile)))) {
                RecipeData appData = ((RecipeBook) parent.getApplication()).getData();
                appData.insertCacheEntry(urls[0], cacheFile);
            }
            //            Log.v(TAG, cacheFile);

            // We're done!
            publishProgress(100);
        } catch (IOException e) {
            // TODO Maybe a dialog?
        }

    }
    client.close();
    return bitmap;
}

From source file:com.lillicoder.newsblurry.feeds.FeedParser.java

/**
 * Decodes the given base-64 favicon string into a {@link Favicon}.
 * @param encodedFavicon Base-64 encoded favicon to decode.
 * @return Decoded {@link Favicon},/*from  w  w w . j  a v a  2 s  .  c o m*/
 *          <code>null</code> if the encoded string could not be decoded.
 */
private Favicon decodeFavicon(String encodedFavicon) {
    Favicon favicon = null;

    if (!TextUtils.isEmpty(encodedFavicon)) {
        try {
            byte[] rawFavicon = Base64.decode(encodedFavicon, Base64.DEFAULT);
            Bitmap faviconImage = BitmapFactory.decodeByteArray(rawFavicon, 0, rawFavicon.length);

            if (faviconImage != null)
                favicon = new Favicon(faviconImage);
        } catch (IllegalArgumentException e) {
            Log.w(TAG, WARNING_FAILED_TO_DECODE_FAVICON, e);
        }
    }

    return favicon;
}

From source file:io.card.development.recording.ManifestEntry.java

private static byte[] decompress(byte[] compressed) {
    /*/*from w  ww  .  j  a v  a2 s.  c  o  m*/
     * DO NOT EVER USE THIS METHOD IN PRODUCTION This is horribly inefficient, but written only
     * for testing purposes.
     */

    Bitmap b = BitmapFactory.decodeByteArray(compressed, 0, compressed.length);
    ByteBuffer bb = ByteBuffer.allocate(b.getWidth() * b.getHeight() * 4);
    b.copyPixelsToBuffer(bb);
    b.recycle();

    byte[] ba = bb.array();
    byte[] singleChannel = new byte[ba.length / 4]; // 4 channels
    for (int i = 0; i < singleChannel.length; i++) {
        singleChannel[i] = ba[i * 4 + 1];
    }

    return singleChannel;
}

From source file:ca.uwaterloo.magic.goodhikes.ProfileActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);/*w ww  . j av  a 2 s  . c om*/
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    /** Define variables **/
    userManager = new UserManager(getApplicationContext());
    user = userManager.getUser();
    profile_image = (ImageView) findViewById(R.id.profile_image);
    profile_user_name = (TextView) findViewById(R.id.profile_user_name);
    profile_email = (TextView) findViewById(R.id.profile_email);
    upload_image = (Button) findViewById(R.id.upload_picture);

    profile_user_name.setText(user.getUsername());
    profile_email.setText(user.getEmail());
    image_str = user.getImage();
    if (image_str != "") {
        /** decode image string and set profile image **/
        byte[] image_arr = Base64.decode(image_str, 0);
        profile_image.setImageBitmap(BitmapFactory.decodeByteArray(image_arr, 0, image_arr.length));

    }

    upload_image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            uploadProfileImage();

        }
    });
}

From source file:fr.outadev.skinswitch.network.SkinManagerConnectionHandler.java

public Bitmap fetchSkinBitmap(int id) {
    byte[] response = HttpRequest.get(BASE_URL + "?method=getSkin&id=" + id).userAgent(getUserAgent())
            .trustAllHosts().useCaches(true).bytes();
    return BitmapFactory.decodeByteArray(response, 0, response.length);
}

From source file:com.cnm.cnmrc.fragment.vodtvch.VodDetail.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View layout = inflater.inflate(R.layout.search_vod_detail, container, false);

    mContext = getActivity();/*w w  w.  j a  v a  2  s  .c  o  m*/

    isFirstDepth = getArguments().getBoolean("isFirstDepth");
    Bundle bundle = getArguments().getBundle("bundle");

    vodAssetId = bundle.getString("vodAssetId");
    byte[] logoImage = bundle.getByteArray("logoImg");
    String title = bundle.getString("title");
    String hd = bundle.getString("hd");
    String grade = bundle.getString("grade");
    String director = bundle.getString("director");
    String actor = bundle.getString("actor");
    String price = bundle.getString("price");
    String contents = bundle.getString("contents");

    Bitmap bmp = BitmapFactory.decodeByteArray(logoImage, 0, logoImage.length);
    ImageView logoImg = (ImageView) layout.findViewById(R.id.logo_img);
    logoImg.setImageBitmap(bmp);

    if (title != null)
        ((TextView) layout.findViewById(R.id.title)).setText(title);
    if (hd != null) {
        if (hd.equalsIgnoreCase("yes"))
            ((ImageView) layout.findViewById(R.id.hd_icon)).setVisibility(View.VISIBLE);
    }
    if (grade != null)
        ((ImageView) layout.findViewById(R.id.grade_icon)).setBackgroundResource(Util.getGrade(grade));
    if (director != null)
        ((TextView) layout.findViewById(R.id.director_name)).setText(" " + director);
    if (actor != null)
        ((TextView) layout.findViewById(R.id.actor_name)).setText(" " + actor);
    if (grade != null)
        ((TextView) layout.findViewById(R.id.grade_text)).setText(" " + grade);
    if (price != null)
        ((TextView) layout.findViewById(R.id.price_amount)).setText(" " + price);
    if (contents != null)
        ((TextView) layout.findViewById(R.id.contents)).setText(contents);

    // vod 
    ImageButton vod = (ImageButton) layout.findViewById(R.id.vod_zzim);
    vod.setOnClickListener(this);

    // tv? 
    ImageButton tv = (ImageButton) layout.findViewById(R.id.vod_tv_watching);
    tv.setOnClickListener(this);

    return layout;
}