2017-11-09 4 views
0

Android開発にはかなり新しいです。私は、キャプチャされたイメージを取得し、そのイメージをバックグラウンドとして設定できる単純なアプリを作っています。私はsetBackground(新しいBitmapDrawble ...)を使ってこれを行うことができました。私の次のアクティビティは、その背景と共にテキストビューを編集することでした。保存ボタンをオンにすると、編集されたテキストビューと背景の両方を表示できる次のアクティビティに進みます。ImageViewをビットマップに設定して次のアクティビティに送信

私の質問では、編集したテキストを表示できましたが、背景を表示できませんでした。ここに私の次のコードです。

RetrieveImage.Activity

public static final String EXTRA_MESSAGE = "com.example.hyunsukcirllee.footstep.MESSAGE"; 
private ImageView mdisplayBackground; 
public static final int SEND_MESSAGE = 1; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_retrieve_photo_background); 

    //Bundle to get intent 
    Bundle extras = getIntent().getExtras(); 

    //If Bundle is not empty proceed 
    if(extras != null){ 
     //get the imagepass key from the previous activity and set it to bitmap 
     Bitmap retrieveImage = (Bitmap) extras.get("imagepass"); 

     //If the bitmap is not empty 
     if(retrieveImage != null){ 
      //Set it to following imageView 
      mdisplayBackground = (ImageView) findViewById(R.id.background_captured); 
      mdisplayBackground.setBackground(new BitmapDrawable(getResources(), retrieveImage)); 
      mdisplayBackground.getBackground().setAlpha(80); 

     } 
    } 
} 

//onClick Send button 
public void sendMessage(View view){ 
    //Creating an explicit intent to display the sent message 
    Intent footstep_intent = new Intent(this, DisplayMessageActivity.class); 

    //EditText Id 
    EditText editText = (EditText) findViewById(R.id.editText); 

    //User editText 
    String message = editText.getText().toString(); 

    //This part is what I want to implement!!! 
    mdisplayBackground = (ImageView) findViewById(R.id.background_captured); 
    mdisplayBackground.buildDrawingCache(); 
    Bitmap image_pass_again = mdisplayBackground.getDrawingCache(); 

    //Put both Bitmap and String Message to the intent 
    footstep_intent.putExtra("Image", image_pass_again); 
    footstep_intent.putExtra(EXTRA_MESSAGE, message); 
    startActivity(footstep_intent); 
} 

}

画像とテキストの両方を表示する

次のアクティビティ現在、送信ボタンをクリックした後に動作しないコードに続いて

private ImageView mdisplayBbackground; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_display_message); 

    //if the intent has the name image do setbackground 
    if(getIntent().hasExtra("Image")) { 
     Bundle iimage = getIntent().getExtras(); 
     Bitmap b = (Bitmap) iimage.get("image"); 
     mdisplayBbackground = (ImageView) findViewById(R.id.background_capturedd); 
     mdisplayBbackground.setBackground(new BitmapDrawable(getResources(), b)); 

    } 

    //if the intent contains the message-> view 
    if (getIntent().hasExtra(RetrievePhotoBackgroundActivity.EXTRA_MESSAGE)){ 
     Intent footstep_intent = getIntent(); 
     String message = footstep_intent.getStringExtra(RetrievePhotoBackgroundActivity.EXTRA_MESSAGE); 
     TextView textView = (TextView) findViewById(R.id.textView); 
     textView.setText(message); 
    } 
} 

}

。上記のコードを使用してビットマップを渡してコンテンツURIに設定してみましたが、私が推測するコンセプトを正しく理解していませんでした。 "外部ストレージを使用してイメージファイルの直接パスを呼び出し、次のアクティビティ用にimageViewを設定する方法はありますか?" もう1つの質問は「Imはまだこの分野で練習しているので、コーディングの面でより良いフォーマットがあれば、フィードバックを得たい」 ありがとう!

+0

あなたは、静的フィールドを使用することができますが、あなたは、すぐにそれがあるとして、ビットマップを再利用する必要があります不要になりました。アドバイスをいただきありがとうございます。 –

答えて

1

異なる画面間でビットマップを送信しないでください。作業が完了した後、すべてのビットマップをリサイクルする必要があるため、ある時点でメモリリークが発生します。
また、画像の読み込み/処理/キャッシングなどを自分で処理しないでください.GlideやPicassoなどのライブラリを使用することを検討してください。最初のものはGoogleが推奨しています。ここで

は、ファイルシステムからロードする画像の一例である、profileAvatarはImageViewのです:

Glide.with(mContext) 
    .load(new File(filePath)) 
    .into(profileAvatar); 

本当に使いやすい;)デフォルトグライドでは、メモリ内の画像をキャッシュします。

+0

ビットマップの扱いに関して、このメモリリークに関するいくつかの徹底的な説明を得ることができますか?そして私は、外部記憶装置をも使用することなく、次の活動に「画像取り込みアクティビティ」内の画像ビューに保存された取り込まれた画像を取り出すことができるかどうかを知りたいと思います。グライドライブラリをご紹介いただきありがとうございます。 –

0

別のアプローチを強くおすすめします。

古い携帯電話と大きなビットマップを持っていると機能しない可能性があります。本当にやりたいのですが、メモリが大量にかかり、速度も遅くなる可能性があります。あなたは本当に

以下の送信ビットマップ

try { 
    //Write file 
    String filename = "abc.png"; 
    FileOutputStream stream = this.openFileOutput(filename, Context.MODE_PRIVATE); 
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 

    //Cleanup 
    stream.close(); 
    bmp.recycle(); 

    //Pop intent 
    Intent in1 = new Intent(this, Activity2.class); 
    in1.putExtra("image", filename); 
    startActivity(in1); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

受信側のようなビットマップを、次に行う渡したいというならば

Bitmap bmp = null; 
String filename = getIntent().getStringExtra("image"); 
try { 
    FileInputStream is = this.openFileInput(filename); 
    bmp = BitmapFactory.decodeStream(is); 
    is.close(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
関連する問題