Bitmap returning null when initialised with byte array












0















I have Bitmap that I'm assigning a byte array value, using code:



public class LegacyCameraManager implements Camera.ErrorCallback, Camera.PreviewCallback, Camera.AutoFocusCallback, Camera.PictureCallback {
public static Bitmap mBitmap;
@Override
public void onPreviewFrame(byte bytes, Camera camera) {
Boolean isProcessing = UserSharedPref.initializeSharedPreferencesForprocessFrames(mContext).getBoolean(UserSharedPref.processFrames, true);
if (isProcessing) {
Log.d("TEST:","length of bytes:"+bytes.length);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
mBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
Log.d("TEST:","The bitmap:"+mBitmap);
tfDetector.onPreviewFrame(bytes, camera);

}
}


The rest of the code works well and this method gets called continuously like it should, the thing is mBitmap is logging out as "null" always and this variable has to be set to an imageview like this,onclikc of a button like this:



 if (LegacyCameraManager.mBitmap == null) {
Log.d("TEST:","Bitmaps is NULL!!");
img.setImageResource(R.drawable.office);
} else {
img.setImageBitmap(Bitmap.createScaledBitmap(LegacyCameraManager.mBitmap, img.getWidth(),
img.getHeight(), false));
}


The Logcat (a lot of number of times like it should):
length of bytes:460800
The bitmap:null



And therefore, the image is not getting set to the bitmap being null, the actual function is to set the image view to a picture taken at the instant which will happen of the bitmap is assigned like it is supposed to. Where am I going wrong?










share|improve this question

























  • I would recommend try to not provide any option into the decodeByteArray.

    – Ivan
    Nov 13 '18 at 11:30











  • yes, tried without the option as well....same.....null

    – Sonic_
    Nov 13 '18 at 11:32











  • if onPreviewFrame(byte data, Camera camera) is from camera.previewCallback, then you have to convert those byte data to yuv then to bitmap check this link stackoverflow.com/questions/4768165/…

    – d.gjinovci
    Nov 13 '18 at 12:00











  • decodeByteArray() takes a compressed image (.jpg, .png, etc.). The frames passed to onPreviewFrame() are not compressed; they are simply in a different format (whatever format you specified when setting up the capture).

    – greeble31
    Nov 13 '18 at 20:16











  • Hey, thanks a lot @d.gjinovci your suggestion worked for me.

    – Sonic_
    Nov 14 '18 at 6:48
















0















I have Bitmap that I'm assigning a byte array value, using code:



public class LegacyCameraManager implements Camera.ErrorCallback, Camera.PreviewCallback, Camera.AutoFocusCallback, Camera.PictureCallback {
public static Bitmap mBitmap;
@Override
public void onPreviewFrame(byte bytes, Camera camera) {
Boolean isProcessing = UserSharedPref.initializeSharedPreferencesForprocessFrames(mContext).getBoolean(UserSharedPref.processFrames, true);
if (isProcessing) {
Log.d("TEST:","length of bytes:"+bytes.length);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
mBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
Log.d("TEST:","The bitmap:"+mBitmap);
tfDetector.onPreviewFrame(bytes, camera);

}
}


The rest of the code works well and this method gets called continuously like it should, the thing is mBitmap is logging out as "null" always and this variable has to be set to an imageview like this,onclikc of a button like this:



 if (LegacyCameraManager.mBitmap == null) {
Log.d("TEST:","Bitmaps is NULL!!");
img.setImageResource(R.drawable.office);
} else {
img.setImageBitmap(Bitmap.createScaledBitmap(LegacyCameraManager.mBitmap, img.getWidth(),
img.getHeight(), false));
}


The Logcat (a lot of number of times like it should):
length of bytes:460800
The bitmap:null



And therefore, the image is not getting set to the bitmap being null, the actual function is to set the image view to a picture taken at the instant which will happen of the bitmap is assigned like it is supposed to. Where am I going wrong?










share|improve this question

























  • I would recommend try to not provide any option into the decodeByteArray.

    – Ivan
    Nov 13 '18 at 11:30











  • yes, tried without the option as well....same.....null

    – Sonic_
    Nov 13 '18 at 11:32











  • if onPreviewFrame(byte data, Camera camera) is from camera.previewCallback, then you have to convert those byte data to yuv then to bitmap check this link stackoverflow.com/questions/4768165/…

    – d.gjinovci
    Nov 13 '18 at 12:00











  • decodeByteArray() takes a compressed image (.jpg, .png, etc.). The frames passed to onPreviewFrame() are not compressed; they are simply in a different format (whatever format you specified when setting up the capture).

    – greeble31
    Nov 13 '18 at 20:16











  • Hey, thanks a lot @d.gjinovci your suggestion worked for me.

    – Sonic_
    Nov 14 '18 at 6:48














0












0








0








I have Bitmap that I'm assigning a byte array value, using code:



public class LegacyCameraManager implements Camera.ErrorCallback, Camera.PreviewCallback, Camera.AutoFocusCallback, Camera.PictureCallback {
public static Bitmap mBitmap;
@Override
public void onPreviewFrame(byte bytes, Camera camera) {
Boolean isProcessing = UserSharedPref.initializeSharedPreferencesForprocessFrames(mContext).getBoolean(UserSharedPref.processFrames, true);
if (isProcessing) {
Log.d("TEST:","length of bytes:"+bytes.length);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
mBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
Log.d("TEST:","The bitmap:"+mBitmap);
tfDetector.onPreviewFrame(bytes, camera);

}
}


The rest of the code works well and this method gets called continuously like it should, the thing is mBitmap is logging out as "null" always and this variable has to be set to an imageview like this,onclikc of a button like this:



 if (LegacyCameraManager.mBitmap == null) {
Log.d("TEST:","Bitmaps is NULL!!");
img.setImageResource(R.drawable.office);
} else {
img.setImageBitmap(Bitmap.createScaledBitmap(LegacyCameraManager.mBitmap, img.getWidth(),
img.getHeight(), false));
}


The Logcat (a lot of number of times like it should):
length of bytes:460800
The bitmap:null



And therefore, the image is not getting set to the bitmap being null, the actual function is to set the image view to a picture taken at the instant which will happen of the bitmap is assigned like it is supposed to. Where am I going wrong?










share|improve this question
















I have Bitmap that I'm assigning a byte array value, using code:



public class LegacyCameraManager implements Camera.ErrorCallback, Camera.PreviewCallback, Camera.AutoFocusCallback, Camera.PictureCallback {
public static Bitmap mBitmap;
@Override
public void onPreviewFrame(byte bytes, Camera camera) {
Boolean isProcessing = UserSharedPref.initializeSharedPreferencesForprocessFrames(mContext).getBoolean(UserSharedPref.processFrames, true);
if (isProcessing) {
Log.d("TEST:","length of bytes:"+bytes.length);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
mBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
Log.d("TEST:","The bitmap:"+mBitmap);
tfDetector.onPreviewFrame(bytes, camera);

}
}


The rest of the code works well and this method gets called continuously like it should, the thing is mBitmap is logging out as "null" always and this variable has to be set to an imageview like this,onclikc of a button like this:



 if (LegacyCameraManager.mBitmap == null) {
Log.d("TEST:","Bitmaps is NULL!!");
img.setImageResource(R.drawable.office);
} else {
img.setImageBitmap(Bitmap.createScaledBitmap(LegacyCameraManager.mBitmap, img.getWidth(),
img.getHeight(), false));
}


The Logcat (a lot of number of times like it should):
length of bytes:460800
The bitmap:null



And therefore, the image is not getting set to the bitmap being null, the actual function is to set the image view to a picture taken at the instant which will happen of the bitmap is assigned like it is supposed to. Where am I going wrong?







java android arrays android-bitmap






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 11:33







Sonic_

















asked Nov 13 '18 at 11:07









Sonic_Sonic_

3617




3617













  • I would recommend try to not provide any option into the decodeByteArray.

    – Ivan
    Nov 13 '18 at 11:30











  • yes, tried without the option as well....same.....null

    – Sonic_
    Nov 13 '18 at 11:32











  • if onPreviewFrame(byte data, Camera camera) is from camera.previewCallback, then you have to convert those byte data to yuv then to bitmap check this link stackoverflow.com/questions/4768165/…

    – d.gjinovci
    Nov 13 '18 at 12:00











  • decodeByteArray() takes a compressed image (.jpg, .png, etc.). The frames passed to onPreviewFrame() are not compressed; they are simply in a different format (whatever format you specified when setting up the capture).

    – greeble31
    Nov 13 '18 at 20:16











  • Hey, thanks a lot @d.gjinovci your suggestion worked for me.

    – Sonic_
    Nov 14 '18 at 6:48



















  • I would recommend try to not provide any option into the decodeByteArray.

    – Ivan
    Nov 13 '18 at 11:30











  • yes, tried without the option as well....same.....null

    – Sonic_
    Nov 13 '18 at 11:32











  • if onPreviewFrame(byte data, Camera camera) is from camera.previewCallback, then you have to convert those byte data to yuv then to bitmap check this link stackoverflow.com/questions/4768165/…

    – d.gjinovci
    Nov 13 '18 at 12:00











  • decodeByteArray() takes a compressed image (.jpg, .png, etc.). The frames passed to onPreviewFrame() are not compressed; they are simply in a different format (whatever format you specified when setting up the capture).

    – greeble31
    Nov 13 '18 at 20:16











  • Hey, thanks a lot @d.gjinovci your suggestion worked for me.

    – Sonic_
    Nov 14 '18 at 6:48

















I would recommend try to not provide any option into the decodeByteArray.

– Ivan
Nov 13 '18 at 11:30





I would recommend try to not provide any option into the decodeByteArray.

– Ivan
Nov 13 '18 at 11:30













yes, tried without the option as well....same.....null

– Sonic_
Nov 13 '18 at 11:32





yes, tried without the option as well....same.....null

– Sonic_
Nov 13 '18 at 11:32













if onPreviewFrame(byte data, Camera camera) is from camera.previewCallback, then you have to convert those byte data to yuv then to bitmap check this link stackoverflow.com/questions/4768165/…

– d.gjinovci
Nov 13 '18 at 12:00





if onPreviewFrame(byte data, Camera camera) is from camera.previewCallback, then you have to convert those byte data to yuv then to bitmap check this link stackoverflow.com/questions/4768165/…

– d.gjinovci
Nov 13 '18 at 12:00













decodeByteArray() takes a compressed image (.jpg, .png, etc.). The frames passed to onPreviewFrame() are not compressed; they are simply in a different format (whatever format you specified when setting up the capture).

– greeble31
Nov 13 '18 at 20:16





decodeByteArray() takes a compressed image (.jpg, .png, etc.). The frames passed to onPreviewFrame() are not compressed; they are simply in a different format (whatever format you specified when setting up the capture).

– greeble31
Nov 13 '18 at 20:16













Hey, thanks a lot @d.gjinovci your suggestion worked for me.

– Sonic_
Nov 14 '18 at 6:48





Hey, thanks a lot @d.gjinovci your suggestion worked for me.

– Sonic_
Nov 14 '18 at 6:48












1 Answer
1






active

oldest

votes


















-1














Just try this:



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


Returns the decoded Bitmap, or null if the image could not be decoded.






share|improve this answer
























  • "mBitmap" has to be a global variable. As it is also being used by another activity. It cant be a local object.

    – Sonic_
    Nov 13 '18 at 11:44













  • Local or Public that's not your problem. I think you should learn basic of java like Access Modifier. Thanks

    – Sultan Mahmud
    Nov 13 '18 at 11:47











  • It's a static variable what's wrong with it?

    – Sonic_
    Nov 13 '18 at 11:54











  • This does not address the basic problem: That the input format is not compressed, and therefor decodeByteArray() is not appropriate to begin with.

    – greeble31
    Nov 13 '18 at 20:23











  • opps. I overlooked that thing. ok Then try this: ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(byteArray); Bitmap bitmap = BitmapFactory.decodeStream(arrayInputStream);

    – Sultan Mahmud
    Nov 13 '18 at 20:31











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279662%2fbitmap-returning-null-when-initialised-with-byte-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














Just try this:



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


Returns the decoded Bitmap, or null if the image could not be decoded.






share|improve this answer
























  • "mBitmap" has to be a global variable. As it is also being used by another activity. It cant be a local object.

    – Sonic_
    Nov 13 '18 at 11:44













  • Local or Public that's not your problem. I think you should learn basic of java like Access Modifier. Thanks

    – Sultan Mahmud
    Nov 13 '18 at 11:47











  • It's a static variable what's wrong with it?

    – Sonic_
    Nov 13 '18 at 11:54











  • This does not address the basic problem: That the input format is not compressed, and therefor decodeByteArray() is not appropriate to begin with.

    – greeble31
    Nov 13 '18 at 20:23











  • opps. I overlooked that thing. ok Then try this: ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(byteArray); Bitmap bitmap = BitmapFactory.decodeStream(arrayInputStream);

    – Sultan Mahmud
    Nov 13 '18 at 20:31
















-1














Just try this:



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


Returns the decoded Bitmap, or null if the image could not be decoded.






share|improve this answer
























  • "mBitmap" has to be a global variable. As it is also being used by another activity. It cant be a local object.

    – Sonic_
    Nov 13 '18 at 11:44













  • Local or Public that's not your problem. I think you should learn basic of java like Access Modifier. Thanks

    – Sultan Mahmud
    Nov 13 '18 at 11:47











  • It's a static variable what's wrong with it?

    – Sonic_
    Nov 13 '18 at 11:54











  • This does not address the basic problem: That the input format is not compressed, and therefor decodeByteArray() is not appropriate to begin with.

    – greeble31
    Nov 13 '18 at 20:23











  • opps. I overlooked that thing. ok Then try this: ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(byteArray); Bitmap bitmap = BitmapFactory.decodeStream(arrayInputStream);

    – Sultan Mahmud
    Nov 13 '18 at 20:31














-1












-1








-1







Just try this:



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


Returns the decoded Bitmap, or null if the image could not be decoded.






share|improve this answer













Just try this:



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


Returns the decoded Bitmap, or null if the image could not be decoded.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 11:41









Sultan MahmudSultan Mahmud

23017




23017













  • "mBitmap" has to be a global variable. As it is also being used by another activity. It cant be a local object.

    – Sonic_
    Nov 13 '18 at 11:44













  • Local or Public that's not your problem. I think you should learn basic of java like Access Modifier. Thanks

    – Sultan Mahmud
    Nov 13 '18 at 11:47











  • It's a static variable what's wrong with it?

    – Sonic_
    Nov 13 '18 at 11:54











  • This does not address the basic problem: That the input format is not compressed, and therefor decodeByteArray() is not appropriate to begin with.

    – greeble31
    Nov 13 '18 at 20:23











  • opps. I overlooked that thing. ok Then try this: ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(byteArray); Bitmap bitmap = BitmapFactory.decodeStream(arrayInputStream);

    – Sultan Mahmud
    Nov 13 '18 at 20:31



















  • "mBitmap" has to be a global variable. As it is also being used by another activity. It cant be a local object.

    – Sonic_
    Nov 13 '18 at 11:44













  • Local or Public that's not your problem. I think you should learn basic of java like Access Modifier. Thanks

    – Sultan Mahmud
    Nov 13 '18 at 11:47











  • It's a static variable what's wrong with it?

    – Sonic_
    Nov 13 '18 at 11:54











  • This does not address the basic problem: That the input format is not compressed, and therefor decodeByteArray() is not appropriate to begin with.

    – greeble31
    Nov 13 '18 at 20:23











  • opps. I overlooked that thing. ok Then try this: ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(byteArray); Bitmap bitmap = BitmapFactory.decodeStream(arrayInputStream);

    – Sultan Mahmud
    Nov 13 '18 at 20:31

















"mBitmap" has to be a global variable. As it is also being used by another activity. It cant be a local object.

– Sonic_
Nov 13 '18 at 11:44







"mBitmap" has to be a global variable. As it is also being used by another activity. It cant be a local object.

– Sonic_
Nov 13 '18 at 11:44















Local or Public that's not your problem. I think you should learn basic of java like Access Modifier. Thanks

– Sultan Mahmud
Nov 13 '18 at 11:47





Local or Public that's not your problem. I think you should learn basic of java like Access Modifier. Thanks

– Sultan Mahmud
Nov 13 '18 at 11:47













It's a static variable what's wrong with it?

– Sonic_
Nov 13 '18 at 11:54





It's a static variable what's wrong with it?

– Sonic_
Nov 13 '18 at 11:54













This does not address the basic problem: That the input format is not compressed, and therefor decodeByteArray() is not appropriate to begin with.

– greeble31
Nov 13 '18 at 20:23





This does not address the basic problem: That the input format is not compressed, and therefor decodeByteArray() is not appropriate to begin with.

– greeble31
Nov 13 '18 at 20:23













opps. I overlooked that thing. ok Then try this: ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(byteArray); Bitmap bitmap = BitmapFactory.decodeStream(arrayInputStream);

– Sultan Mahmud
Nov 13 '18 at 20:31





opps. I overlooked that thing. ok Then try this: ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(byteArray); Bitmap bitmap = BitmapFactory.decodeStream(arrayInputStream);

– Sultan Mahmud
Nov 13 '18 at 20:31


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279662%2fbitmap-returning-null-when-initialised-with-byte-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

Bicuculline

さくらももこ