如何将专辑封面传递给另一个活动

时间:2015-10-10 11:18:39

标签: android android-imageview audio-player android-music-player albumart

public void accessMetaData(int songIndex)
{
    MediaMetadataRetriever retriver = new MediaMetadataRetriever ();


    retriver.setDataSource(songsList.get(songIndex).get("songPath"));

    showMetaData(retriver);
}

public  void showMetaData(MediaMetadataRetriever retriver)
{
    // Log.i("Script", "METADATA_KEY_ALBUM: " +retriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
    // Log.i("Script", "METADATA_KEY_ALBUMARTIST: " +retriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST));


    byte [] imgBytes = retriver.getEmbeddedPicture();
    Bitmap bitmap ;

    if(imgBytes != null) {

        bitmap = BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length);

    ImageView ivImage = (ImageView)findViewById(R.id.ivImage);
        ivImage.setImageBitmap(bitmap); 

    /*  PlayListActivity.ay7aga = (ImageView)findViewById(R.id.widget_album_art);
        ivImage.setImageBitmap(bitmap); */

    }

我有这个代码来获取活动A中的专辑封面,我希望将相同的专辑封面传递给活动B,此代码获得当前播放歌曲的专辑封面

2 个答案:

答案 0 :(得分:0)

如果我按照你的问题,你可以将图像从一个活动传递到另一个活动。将您的专辑封面ImageView转换为位图。 将ImageView的DrawingCache设置为true,然后将背景保存为位图并通过putExtra传递。

 image.setDrawingCacheEnabled(true);
  Bitmap b=image.getDrawingCache();
  Intent i = new Intent(this, nextActivity.class);

  i.putExtra("Bitmap", b);
  startActivity(i);

在你的下一个活动中,

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Bitmap");
imageView.setImageBitmap(bitmap);

答案 1 :(得分:0)

 btnPlaylist.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

        /*  Intent i = new Intent(getApplicationContext(), PlayListActivity.class);
            Bitmap bmOriginal = BitmapFactory.decodeResource(getResources(), R.id.ivImage);
            i.putExtra("ha",bmOriginal);
            startActivityForResult(i, 100);  */

            Intent i = new Intent(getApplicationContext(), PlayListActivity.class);
            ivImage.buildDrawingCache();
            Bitmap bitmap =  ivImage.getDrawingCache();

            i.putExtra("BitmapImage", bitmap);



        }
    });

}





/**
 * Receiving song index from playlist view
 * and play the song
 * */
@Override
protected void onActivityResult(int requestCode,
                                 int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == 100){
         currentSongIndex = data.getExtras().getInt("songIndex");
         // play selected song
         playSong(currentSongIndex);
    }

}

/**
 * Function to play a song
 * @param songIndex - index of song
 * */
public  void  playSong(int songIndex){
    // Play song
    try {


        mp.reset();
        accessMetaData(songIndex); // here i can show albumart in Activity A
        mp.setDataSource(songsList.get(songIndex).get("songPath"));
        mp.prepare();
        mp.start();
        // Displaying Song title
        String songTitle = songsList.get(songIndex).get("songTitle");
        songTitleLabel.setText(songTitle);

        // Changing Button Image to pause image
        btnPlay.setImageResource(R.drawable.btn_pause);

        // set Progress bar values
        songProgressBar.setProgress(0);
        songProgressBar.setMax(100);

        // Updating progress bar
        updateProgressBar();            
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}










      Activity B
   //      public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.playlist);
    getActionBar().hide();


    ay7aga = (ImageView)findViewById(R.id.widget_album_art);

    Bundle bundle = this.getIntent().getExtras();
     Bitmap bmA = bundle.getParcelable("ha");
     ay7aga.setImageBitmap(bmA); 





 public void accessMetaData(int songIndex)
{
    MediaMetadataRetriever retriver = new MediaMetadataRetriever ();


    retriver.setDataSource(songsList.get(songIndex).get("songPath"));

    showMetaData(retriver);
}

public void showMetaData(MediaMetadataRetriever retriver)
{
    // Log.i("Script", "METADATA_KEY_ALBUM: " +retriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
    // Log.i("Script", "METADATA_KEY_ALBUMARTIST: " +retriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST));


    byte [] imgBytes = retriver.getEmbeddedPicture();
    Bitmap bitmap ;

    if(imgBytes != null) {

        bitmap = BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length);

    ImageView ay7aga = (ImageView)findViewById(R.id.widget_album_art);
    ay7aga.setImageBitmap(bitmap);



    }