2011-09-12 30 views
4

我試圖從一個活動發送一個字節[]到另一個。在接收活動中,在獲得意圖附加數據後,byte []似乎爲空。有任何想法嗎?android無法從意圖獲取字節數組

謝謝。

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); 

[更新] 所以不是我已經改變了鍵值相同的數據/ bytrArr,還意圖現在只是傳遞一個Bundle

回答

8

鍵的值不是你的問題。你不是在你把它以同樣的方式檢索數據。

在代碼的第一部分,你是把一個byte []一個Bundle裏面,然後把該Bundle到意向演員。這意味着關鍵「數據」處的EXTRA是一個Bundle,而不是一個字節[]。你不需要以這種方式插入額外的東西。只需執行intent.putExtra("byteArr", touchView.data)即可將Extract的byte []插入。

這樣做,您將能夠在代碼的第二部分中使用getIntent().getByteArrayExtra("byteArr")檢索您的byte []。最後,作爲一個附註,如果你有多個你想用一個呼叫申請的附加組件,你可以把它們放到一個Bundle中,然後調用Intent.putExtras(bundle)以將Bundle中的所有數據單獨放入到組件中意圖。但是,這不同於將Bundle作爲一個額外的本身來添加。

HTH

+0

嗨修改了你的建議的代碼,但它仍然拋出NPE的ARR陣列。 – turtleboy

+0

嗨,只是再次測試它,你是正確的。當我第一次啓動應用程序時,我的代碼不會在第一次使用數據設置數組時使用其他代碼。謝謝 – turtleboy

0

不給相同的鍵名都的花絮。給一個不同的名字。

只需撥打intent.putExtra(bundle);即可將套件置於意圖中。

0

替換

intent.putExtra( 「數據」,束);

intent.putExtras(束);