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.example.androidthings.doorbell.DoorbellEntryAdapter.java

@Override
protected void populateViewHolder(DoorbellEntryViewHolder viewHolder, DoorbellEntry model, int position) {
    // Display the timestamp
    CharSequence prettyTime = DateUtils.getRelativeDateTimeString(mApplicationContext, model.getTimestamp(),
            DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0);
    viewHolder.time.setText(prettyTime);

    // Display the image
    if (model.getImage() != null) {
        // Decode image data encoded by the Cloud Vision library
        byte[] imageBytes = Base64.decode(model.getImage(), Base64.NO_WRAP | Base64.URL_SAFE);
        Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
        if (bitmap != null) {
            viewHolder.image.setImageBitmap(bitmap);
        } else {/*w  w  w. j  a  va2s  .co m*/
            Drawable placeholder = ContextCompat.getDrawable(mApplicationContext, R.drawable.ic_image);
            viewHolder.image.setImageDrawable(placeholder);
        }
    }

    // Display the metadata
    if (model.getAnnotations() != null) {
        ArrayList<String> keywords = new ArrayList<>(model.getAnnotations().keySet());

        int limit = Math.min(keywords.size(), 3);
        viewHolder.metadata.setText(TextUtils.join("\n", keywords.subList(0, limit)));
    } else {
        viewHolder.metadata.setText("no annotations yet");
    }
}

From source file:com.example.administrator.winsoftsalesproject.activity.NavHomeActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nav_home);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);//from w w  w .  ja va  2  s  . c o m
    sessionManger = new SessionManger(this);
    sessionManger.checkLogin();
    HashMap<String, String> user = sessionManger.getUserDetails();
    home = new Home();
    fragmentTransaction(home);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    View header = navigationView.getHeaderView(0);
    TextView headerName = (TextView) header.findViewById(R.id.nav_name);
    TextView designation = (TextView) header.findViewById(R.id.nav_designation);
    ImageView headerImage = (ImageView) header.findViewById(R.id.nav_image);

    headerName.setText(user.get(sessionManger.KEY_EMPLOYEE_NAME).toUpperCase());
    designation.setText(user.get(sessionManger.KEY_DEPARTMENT).toUpperCase());

    byte[] decodeString = Base64.decode(user.get(sessionManger.KEY_EMPLOYEE_PHOTO).getBytes(), Base64.DEFAULT);
    Bitmap bitmap = BitmapFactory.decodeByteArray(decodeString, 0, decodeString.length);
    headerImage.setImageBitmap(bitmap);
}

From source file:com.it.selectphotodemo.PhotoUtils.java

/**
 * Given a uri pointing to a bitmap, reads it into a bitmap and returns it.
 * @throws FileNotFoundException/* www  .  j a v a2 s .c  o  m*/
 */
public static Bitmap getBitmapFromUri(Context context, Uri uri) throws FileNotFoundException {
    final InputStream imageStream = context.getContentResolver().openInputStream(uri);
    byte[] data = compressBitmap(BitmapFactory.decodeStream(imageStream));
    return BitmapFactory.decodeByteArray(data, 0, data.length);

}

From source file:jp.iftc.androidasset.db.AssetImage.java

/**
 * @return assetImageBitmap??
 */
public Bitmap getAssetImageBitmap() {
    return BitmapFactory.decodeByteArray(assetImage, 0, assetImage.length);
}

From source file:ca.christophersaunders.tutorials.sqlite.picasa.PicasaImage.java

public Bitmap getThumbnail() {
    if (thumbnail == null && thumbnailLocation != null) {
        getThumbnailBytes();/*  www  .  ja va 2  s. com*/
    }
    if (thumbnail == null && thumbnailBytes.length > 0) {
        thumbnail = BitmapFactory.decodeByteArray(thumbnailBytes, 0, thumbnailBytes.length);
    }
    return thumbnail;
}

From source file:grytsenko.coworkers.web.WebClient.java

public static byte[] toPng(byte[] raw) throws IOException {
    Bitmap bitmap = BitmapFactory.decodeByteArray(raw, 0, raw.length);
    if (bitmap == null) {
        throw new IOException("Not decoded.");
    }//  w  w  w . j av  a  2s  . c  o  m

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    boolean compressed = bitmap.compress(CompressFormat.PNG, PHOTO_QUALITY, output);
    if (!compressed) {
        throw new IOException("Not compressed.");
    }

    return output.toByteArray();
}

From source file:com.carlrice.reader.utils.UiUtils.java

static public Bitmap getScaledBitmap(byte[] iconBytes, int sizeInDp) {
    if (iconBytes != null && iconBytes.length > 0) {
        Bitmap bitmap = BitmapFactory.decodeByteArray(iconBytes, 0, iconBytes.length);
        if (bitmap != null && bitmap.getWidth() != 0 && bitmap.getHeight() != 0) {
            int bitmapSizeInDip = UiUtils.dpToPixel(sizeInDp);
            if (bitmap.getHeight() != bitmapSizeInDip) {
                Bitmap tmp = bitmap;/*from w  w w. j a  v a2 s.c om*/
                bitmap = Bitmap.createScaledBitmap(tmp, bitmapSizeInDip, bitmapSizeInDip, false);
                tmp.recycle();
            }

            return bitmap;
        }
    }

    return null;
}

From source file:com.example.kushagar.knockknock.FragmentMain.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    //TextView mTxtTitle = (TextView) rootView.findViewById(R.id.txtTitle);
    //mTxtTitle.setText(getArguments().getString(TEXT_FRAGMENT));
    mCardContainer = (CardContainer) rootView.findViewById(R.id.layoutview);

    SimpleCardStackAdapter adapter = new SimpleCardStackAdapter(getActivity());

    Resources r = getResources();

    //add the cards here by posting a get request.
    byte[] encodeByte = Base64.decode("Description goes here", Base64.DEFAULT);
    Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
    Drawable d = new BitmapDrawable(bitmap);

    adapter.add(new CardModel("Title1", "Description goes here", d));

    CardModel cardModel = new CardModel("Title1", "Description goes here", r.getDrawable(R.drawable.picture1));
    cardModel.setOnClickListener(new CardModel.OnClickListener() {
        @Override//from  w ww  . j  a v  a2  s . co m
        public void OnClickListener() {
            Log.i("Swipeable Cards", "I am pressing the card");
        }
    });

    cardModel.setOnCardDimissedListener(new CardModel.OnCardDimissedListener() {
        @Override
        public void onLike() {
            Log.i("Swipeable Cards", "I like the card");
        }

        @Override
        public void onDislike() {
            Log.i("Swipeable Cards", "I dislike the card");
        }
    });

    adapter.add(cardModel);

    mCardContainer.setAdapter(adapter);

    rootView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    return rootView;
}

From source file:com.gudong.appkit.ui.activity.AppActivity.java

private void fillData() {
    mAppEntity = (AppEntity) getIntent().getParcelableExtra(EXTRA_APP_ENTITY);
    Bitmap bitmap = BitmapFactory.decodeByteArray(mAppEntity.getAppIconData(), 0,
            mAppEntity.getAppIconData().length);
    mImageView.setImageBitmap(bitmap);//from   www .  ja  va  2 s. c  o m
    mTvAppName.setText(mAppEntity.getAppName());
    mTvAppVersion.setText(FormatUtil.formatVersionName(mAppEntity));
    mTvAppPackageName.setText(mAppEntity.getPackageName());
    mTvAppVersionCode.setText("VersionCode:" + mAppEntity.getVersionCode());
    if (Utils.isBriefMode()) {
        mTvAppVersionCode.setVisibility(View.GONE);
    }
}

From source file:com.ravi.apps.android.newsbytes.HeadlinesAdapter.java

@Override
public void bindView(View view, Context context, Cursor cursor) {
    // Extract the view holder.
    ViewHolder viewHolder = (ViewHolder) view.getTag();

    // Get the mark as favorite flag.
    int isFavorite = cursor.getInt(HeadlinesFragment.COL_IS_FAVORITE);

    // If it's marked as favorite load image from byte stream, else use Picasso for loading.
    if (isFavorite == 1) {
        // Get the thumbnail blob from cursor.
        byte[] thumbnailByteStream = cursor.getBlob(HeadlinesFragment.COL_THUMBNAIL);

        // Check if blob for thumbnail is non null.
        if (thumbnailByteStream != null) {
            // Get the thumbnail bitmap from byte array.
            Bitmap thumbnailBitmap = BitmapFactory.decodeByteArray(thumbnailByteStream, 0,
                    thumbnailByteStream.length);

            // Set the thumbnail bitmap into the image view.
            viewHolder.thumbnailView.setImageBitmap(thumbnailBitmap);
            viewHolder.thumbnailView.setScaleType(ImageView.ScaleType.FIT_XY);
        } else {// w  ww  .  j  a  va 2 s.  c o  m
            // Show thumbnail placeholder icon and log error message.
            viewHolder.thumbnailView.setImageResource(R.drawable.thumbnail_placeholder);
            Log.e(LOG_TAG, context.getString(R.string.msg_err_no_thumbnail));
        }
    } else {
        // Get the thumbnail uri.
        String urlThumbnail = cursor.getString(HeadlinesFragment.COL_URI_THUMBNAIL);

        // Check if url for thumbnail is non null.
        if (urlThumbnail != null) {
            // Remove escape characters from the string.
            String url = Utility.removeCharsFromString(urlThumbnail, "\\");

            // Load the thumbnail into image view using Picasso.
            Picasso.with(context).load(url).placeholder(R.drawable.thumbnail_placeholder).fit()
                    .into(viewHolder.thumbnailView);
        } else {
            // Show thumbnail placeholder icon and log error message.
            viewHolder.thumbnailView.setImageResource(R.drawable.thumbnail_placeholder);
            Log.e(LOG_TAG, context.getString(R.string.msg_err_no_thumbnail));
        }
    }

    // Get the headline and set it onto the text view.
    String headline = cursor.getString(HeadlinesFragment.COL_HEADLINE);

    // Check if headline is non null and non empty.
    if (headline != null && !headline.isEmpty()) {
        viewHolder.headlineView.setText(headline);
        viewHolder.headlineView.setContentDescription(headline);
    } else {
        // Display headline not available message and log error message.
        viewHolder.headlineView.setText(context.getString(R.string.msg_err_no_headline));
        viewHolder.headlineView.setContentDescription(context.getString(R.string.msg_err_no_headline));
        Log.e(LOG_TAG, context.getString(R.string.msg_err_no_headline));
    }

    // Generate transition names for shared elements for devices running lollipop or above.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        viewHolder.thumbnailView.setTransitionName(THUMBNAIL_TRANSITION_NAME + cursor.getPosition());
        viewHolder.headlineView.setTransitionName(HEADLINE_TRANSITION_NAME + cursor.getPosition());
    }
}