[英]passing an image from a list view to another activity
單擊列表項時,我試圖將圖像從列表視圖傳遞到另一個活動。 我設法弄清楚了如何從列表項中獲取文本數據,但是一直停留在如何將圖像從列表傳遞到預期活動的過程中。 數據存儲在包含圖像作為資源的數組中。 我知道我可能缺少一些瑣碎的東西,但是我試圖使它以多種方式工作,並查看了多個教程,但未能解決問題。
這是我的活動代碼,其中聲明了數組,聲明了on item click監聽器。
package com.example.android.musicplayer69;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
public class ArtistActivity extends AppCompatActivity {
public static final String KEY_SONG = "KEY_SONG";
public static final String KEY_ARTIST = "KEY_ARTIST";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
ArrayList<Song> song = new ArrayList<Song>();
/** words.add("one")*/
song.add(new Song("Last Night", "The Strokes", "Is this
It",R.drawable.albumcover));
song.add(new Song("Golden Fleeces", "Israel Nash",
"Lifted",R.drawable.play));
song.add(new Song("Heavy, California", "Jungle", "Heavy
California",R.drawable.albumcover));
song.add(new Song("Grow Up", "Boiler", "Grow
Up",R.drawable.albumcover));
song.add(new Song("The Ballad of the Costa Concordia", "Car Seat
Head Rest", "Teens of Denial",R.drawable.albumcover));
song.add(new Song("Leaving", "Bass Drum of Death", "Just
Business",R.drawable.albumcover));
song.add(new Song("Creature Comfort", "Woods", "City Sun Eater in
the River of Light",R.drawable.albumcover));
song.add(new Song("Summer with Phill", "Dead Ghost", "Can't Get
No",R.drawable.albumcover));
song.add(new Song("Night Ride", "The Growlers", "Night
Ride",R.drawable.albumcover));
final SongAdapter adapter = new SongAdapter(this, song);
ListView listView = findViewById(R.id.list);
listView.setAdapter(adapter);
// When listView item is clicked, item data is sent to new activity
listView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
Song item = adapter.getItem(position);
Intent nowPlaying = new Intent(getApplicationContext(),
MainActivity.class);
nowPlaying.putExtra(KEY_SONG, item.getmSong());
nowPlaying.putExtra(KEY_ARTIST, item.getmArtist());
nowPlaying.putExtra("IMAGE_RES",item.getAlbumArt());
startActivity(nowPlaying);}
});
}
}
這是我試圖將信息傳遞給的活動的代碼。
package com.example.android.musicplayer69;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import static com.example.android.musicplayer69.ArtistActivity.KEY_ARTIST;
import static com.example.android.musicplayer69.ArtistActivity.KEY_SONG;
public class MainActivity extends AppCompatActivity {
private ImageSwitcher v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String playingSong = "";
String playingArtist = "";
Intent intent = getIntent();
if (null != intent) {
playingSong = intent.getStringExtra(KEY_SONG);
playingArtist = intent.getStringExtra(KEY_ARTIST);
{
}
}
Bundle bundle = this.getIntent().getExtras();
int nowPlayingArt = bundle.getInt("IMAGE_RES");
// Sets data to appropriate TextViews
TextView playingSongText = (TextView) findViewById(R.id.Song);
playingSongText.setText(playingSong);
TextView playingArtistText = (TextView) findViewById(R.id.Artist);
playingArtistText.setText(playingArtist);
ImageView nowPlayingAlbumArt = findViewById(R.id.AlbumCover);
nowPlayingAlbumArt.setImageResource(nowPlayingArt);
// Sets activity_main as the layout file
ImageView menu = findViewById(R.id.home_button);
// creates button/ sets on click listener for Artist View
menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent menuIntent = new Intent(MainActivity.this,
menuActivity.class);`
startActivity(menuIntent);
}
});
}
}
這是我從控制台收到的錯誤:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.musicplayer69, PID: 11885
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.musicplayer69/com.example.android.musicplayer69.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
at com.example.android.musicplayer69.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'
嘗試將您的圖像轉換為字節數組。 看一個例子。
示例(將圖像轉換為字節數組):
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
示例(傳遞您的字節數組):
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
示例(獲取傳遞的數據並轉換為位圖):
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.