java android无法从意图中获取字节数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7392325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android can't get byte array from intent
提问by turtleboy
i'm trying to send a byte[] from one activity to another. in the recieving activity the byte[] seems to be null after getting the intent extras. any ideas?
我正在尝试将一个字节 [] 从一个活动发送到另一个活动。在接收活动中,字节 [] 在获得意图附加项后似乎为空。有任何想法吗?
thanks.
谢谢。
Button save = (Button)findViewById(R.id.save);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
touchView.isSaved = true;
Bundle bundle = new Bundle();
bundle.putByteArray("byteArr", touchView.data);
Intent intent = new Intent(mContext, SavePic.class);
intent.putExtra(bundle );
startActivity(intent);
}}) ;
.
.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.savepic);
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setText("");
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
Bundle extras = getIntent().getExtras();
byte [] arr = extras.getByteArray("byteArr");
if(arr != null){
Log.e("xxxxxx", "********* arr not null");
}else{
Log.e("xxxxxx", "********* arr is null");
}
final Bitmap mCBitmap2 = BitmapFactory.decodeByteArray(arr, 0, arr.length);
.
.
[updated] i've changed the key values so the are not the same data/bytrArr, also the intent now just passes a Bundle
[更新] 我已经更改了键值,因此不是相同的数据/bytrArr,现在意图也只是传递了一个 Bundle
回答by Devunwired
The value of the keys is not your problem. You're not retrieving the data in the same way that you are putting it in.
键的值不是你的问题。您没有以与放入数据的方式相同的方式检索数据。
In the first section of code, you are putting a byte[] inside a Bundle
, and then putting that Bundle
into the Intent extras. This means that the EXTRA at the key "data" is a Bundle, not a byte[]. You have no need to insert the extras in this fashion. Simply do intent.putExtra("byteArr", touchView.data)
to insert the byte[] as an Extra.
在代码的第一部分中,您将一个 byte[] 放入 a 中Bundle
,然后将其Bundle
放入 Intent extras 中。这意味着关键“数据”处的 EXTRA 是一个 Bundle,而不是一个 byte[]。您无需以这种方式插入额外内容。只需intent.putExtra("byteArr", touchView.data)
将 byte[] 作为 Extra 插入即可。
Doing this, you will be able to retrieve your byte[] back with getIntent().getByteArrayExtra("byteArr")
in the second section of code.
这样做,您将能够getIntent().getByteArrayExtra("byteArr")
在第二部分代码中检索您的 byte[] 。
Finally, just as a side note, if you DID have multiple extras you wanted to apply with one call, you could put each one into a Bundle and then call Intent.putExtras(bundle)
to have all the data from the Bundle placed individually into the Intent. But this is not the same as adding that Bundle as an extra itself.
最后,作为旁注,如果您确实想要通过一次调用应用多个附加项,您可以将每个附加项放入一个 Bundle,然后调用Intent.putExtras(bundle)
以将 Bundle 中的所有数据单独放入 Intent 中。但这与将该 Bundle 添加为额外内容不同。
HTH
HTH
回答by Ronnie
Dont give the same key name to both the extras. Give a different name.
不要为这两个附加项提供相同的密钥名称。给一个不同的名字。
Just call intent.putExtra(bundle);
for putting the bundle in the intent.
只需调用intent.putExtra(bundle);
将捆绑包放入意图中即可。
回答by Yashwanth Kumar
Replace
代替
intent.putExtra("data",bundle );
intent.putExtra(“数据”,捆绑);
with
和
intent.putExtras(bundle );
意图.putExtras(捆绑);