2016-05-19 7 views
0

設定アクティビティを1つ作成しました。私はそこに多くの断片があります。 これらのうちの1つにロゴ設定(DialogPreferences)があり、ギャラリーから画像を選択し、その画像をDialogPreferencesのImageViewに表示する必要があります。ギャラリー画像データをアクティビティからDialogPreferencesに渡す

ここでは、設定アクティビティのonActivityResultでギャラリー画像を取得しています。しかし、私はそのイメージデータをConfiguration(Fragment) - > LogoPreferences(DialogPreferences)に必要とします。

私の質問は、どのように(アクティビティの)onActivityResultからLogoPreferences(つまりDialogPreferenceであり、Configurationフラグメント内にある)からイメージデータを取得できるかということです。

ありがとうございます。

答えて

0

あなたはのByteArrayonActivityResult()から取得Bitmap画像を変換し、その後、戻ってあなたのLogoPreferences活性(またはいずれかの活動あなたはデータがに行きたい)の配列を変換することができます。

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    // Check to see the result is from the activity you are targeting 
    if (requestCode == 1 && resultCode == RESULT_OK && data != null) { 
     Uri selectedImage = data.getData(); 
     try { 
      Bitmap bitmapImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage); 
      Log.i("Image Path", selectedImage.getPath()); 

      // Convert bitmap to byte array 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      bitmapImage.compress(Bitmap.CompressFormat.PNG, 0 /*ignored if PNG*/, bos); 
      byte[] bitmapdata = bos.toByteArray(); 

      Bundle b = new Bundle(); 
      b.putByteArray("byteArray", bitmapdata); 
      Intent intent = new Intent(this, LogoPreferences.class); 
      intent.putExtras(b); 
      startActivity(intent); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

次にLogoPreferences活動にそのバイト配列を復号化するため、あなたがこれを行うことができます:ここにあなたができる何かである

if(getIntent().hasExtra("byteArray")) { 
     Bitmap bitmap = BitmapFactory.decodeByteArray(
       getIntent().getByteArrayExtra("byteArray"), 0, getIntent().getByteArrayExtra("byteArray").length); 
     // Optionally set the Bitmap to an ImageView 
     ImageView imv = new ImageView(this); 
     imv.setImageBitmap(bitmap); 
     } 

はそれが役に立てば幸い!

関連する問題