Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4599434
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:32:25+00:00 2026-05-21T23:32:25+00:00

I have a float array in java and want to convert each element to

  • 0

I have a float array in java and want to convert each element to its byte array counterpart to display using android.graphics.Bitmap and Bitmapfactory, however this operation doesn’t seem to work any words of wisdom would be greatly appreciated.

package com.bitmapdisplay;
import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;
import android.view.Window;


public class BitmapDiplay extends Activity {
/** Called when the activity is first created. */
    float[] array = {1, 6, 1, 4, 5, 0, 8, 7, 8, 6, 1,0, 5 ,6, 1,8};
    float[] new_array= new float[array.length];
    int[] int_array;
    byte[] byte_array;
    private static final int MASK = 0xff;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);


    for(int k = 0;k<array.length;k++)
    {
        new_array[k] = (float)((double)array[k]*2);
        int_array[k] = Float.floatToRawIntBits(new_array[k]);
        byte_array = intToByteArray(int_array[1]);
        setContentView(new Panel(this));
    }

}



public static byte[] intToByteArray(int param) {
    byte[] result = new byte[4];
    for (int i = 0; i < 4; i++) {
        int offset = (result.length - 1 - i) * 8;
        result[i] = (byte) ((param >>> offset) & MASK);
    }
    return result;
}




class Panel extends View {
    public Panel(Context byteArray) {
        super(byteArray);
    }
    @Override
    public void onDraw(Canvas canvas) {
        Bitmap _scratch = BitmapFactory.decodeByteArray(byte_array, 0, byte_array.length);
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(_scratch, 10, 10, null);
    }
}
}

I am converting from a single float to its 4bit array representation, however to do this I firstly convert it to raw int then to byte[]. I am not sure if this is the best way. However in the end I hope to construct a float matrix of calculated log spectral amplitudes of an audio track and map the matrix to a bitmap. However, I am stuck on just converting a single array to a bitmap. I am new to android and java. If there is another method that people might suggest to perform this task, that would be great to.Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:32:26+00:00Added an answer on May 21, 2026 at 11:32 pm

    BitmapFactory.decodeByteArray(byte_array,..) expects byte_array to be “byte array of compressed image data”. I doubt that some arbitrary bytes make up a compressed image. Maybe you should call Canvas.drawPoint() repeatedly?

    for(int k = 0;k < array.length;k++)
    {
      ..
      byte_array = intToByteArray(int_array[1]);
      ..
    }
    

    Here you set byte_array to be the 4 bytes that make up the second int(int_array[1]). And do that 16 times.

    Converting a float array to a byte array can be done easier: (did not test)

    float[] array = {1, 6, 1, 4, 5, 0, 8, 7, 8, 6, 1,0, 5 ,6, 1,8};
    ByteBuffer byteBuf = ByteBuffer.allocate(4 * array.length);
    FloatBuffer floatBuf = byteBuf.asFloatBuffer();
    floatBuf.put(array);
    byte [] byte_array = byteBuf.array();
    

    However, turning this into a image will look like funky random colors, unless you know exactly what you see. Doesn’t it make more sense, to generate colors from white to black?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have two items on the same line using float: left for
I have a vector of float arrays i.e. Vector . I want to convert
I have a Java array defined already e.g. float[] values = new float[3]; I
I am writing jython application with eclipse SWT/JFace. I have to pass float array
Can I have a class with one member either be float array or array
If you have a float in MSSQLServer, to what do you map this in
I have double (or float) variables that might be empty, as in holding no
Both are mathematical values, however the float does have more precision. Is that the
I have a Div with five float divs inside: var div=document.createElement(div); div.className=cssDivNino; var divFolio=document.createElement(div);
I have a function that returns a float from 0 to 255. I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.