我无法显示我解析的图像,因为在getStringExtra
中我收到错误。代码
public class SingleActivity extends Activity {
// JSON node keys
private static final String TAG_TITOLI = "titoli";
private static final Bitmap TAG_IMMAGINE = immagine;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_item);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
String titoli = in.getStringExtra(TAG_TITOLI);
Bitmap immagine = in.getStringExtra(TAG_IMMAGINE); //error
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.titoli_label);
ImageView lblImg = (ImageView) findViewById(R.id.imgeIV);
lblName.setText(titoli);
lblImg.setImageBitmap(immagine);
}
}
这里:Bitmap immagine = in.getStringExtra(TAG_IMMAGINE);
我无法理解写什么。我知道是一张图片所以我不能写String
那么呢?拜托,谢谢
我试图遵循建议,但我在logcat中得到了这个错误:
01-19 18:40:32.989: E/AndroidRuntime(9000): FATAL EXCEPTION: main
01-19 18:40:32.989: E/AndroidRuntime(9000): Process: it.rs.esempioparsinglistview, PID: 9000
01-19 18:40:32.989: E/AndroidRuntime(9000): java.lang.RuntimeException: Unable to start activity ComponentInfo{it.rs.esempioparsinglistview/it.rs.esempioparsinglistview.SingleActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'byte[] java.lang.String.getBytes()' on a null object reference
01-19 18:40:32.989: E/AndroidRuntime(9000): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
01-19 18:40:32.989: E/AndroidRuntime(9000): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
01-19 18:40:32.989: E/AndroidRuntime(9000): at android.app.ActivityThread.access$800(ActivityThread.java:145)
01-19 18:40:32.989: E/AndroidRuntime(9000): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
01-19 18:40:32.989: E/AndroidRuntime(9000): at android.os.Handler.dispatchMessage(Handler.java:102)
01-19 18:40:32.989: E/AndroidRuntime(9000): at android.os.Looper.loop(Looper.java:136)
01-19 18:40:32.989: E/AndroidRuntime(9000): at android.app.ActivityThread.main(ActivityThread.java:5081)
01-19 18:40:32.989: E/AndroidRuntime(9000): at java.lang.reflect.Method.invoke(Native Method)
01-19 18:40:32.989: E/AndroidRuntime(9000): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
01-19 18:40:32.989: E/AndroidRuntime(9000): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-19 18:40:32.989: E/AndroidRuntime(9000): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'byte[] java.lang.String.getBytes()' on a null object reference
01-19 18:40:32.989: E/AndroidRuntime(9000): at android.util.Base64.decode(Base64.java:118)
01-19 18:40:32.989: E/AndroidRuntime(9000): at it.rs.esempioparsinglistview.SingleActivity.onCreate(SingleActivity.java:44)
01-19 18:40:32.989: E/AndroidRuntime(9000): at android.app.Activity.performCreate(Activity.java:5231)
01-19 18:40:32.989: E/AndroidRuntime(9000): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-19 18:40:32.989: E/AndroidRuntime(9000): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
01-19 18:40:32.989: E/AndroidRuntime(9000): ... 9 more
以前的活动:
public class MainActivity extends Activity {
ProgressDialog mProgressDialog;
public static final String TAG_TITOLI = "titoli";
private static final String TAG_CONTENT = "content";
ListView lista;
public Button immaginebtn;
static final String BLOG_URL = "http://www.multiplayer.it";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lista = (ListView)this.findViewById(R.id.main_lista);//recupero lista da id
//View inflatedView = getLayoutInflater().inflate(R.layout.riga_listview, null);
//immaginebtn = (Button)findViewById(R.id.immaginebtn);
//creo ed eseguo l'asynctask
ParsingPaginaWeb parsing = new ParsingPaginaWeb();
parsing.execute("");
// Immagine btn
/*immaginebtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Execute Logo AsyncTask
new Logo().execute();
}
});*/
//new Logo().execute();
// Launching new screen on Selecting Single ListItem
lista.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String titoli = ((TextView) view.findViewById(R.id.riga_listview_titolo)).getText().toString();
//String cont = ((TextView) view.findViewById(R.id.riga_descrizione)).getText().toString();
//String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleActivity.class);
in.putExtra(TAG_TITOLI, titoli);
//in.putExtra(TAG_CONTENT, cont);
//in.putExtra(TAG_PHONE_MOBILE, description);
startActivity(in);
}
});
}
private class ParsingPaginaWeb extends AsyncTask<String,String,String> {
ArrayList<String> titoli; //lista dei titoli
List<Integer> immagine = new ArrayList<Integer>();
//ArrayList<String> content; //lista delle descrizioni
@Override
protected void onPreExecute()
{
//Toast.makeText(MainActivity.this ,"Caricamento lista titoli...", Toast.LENGTH_SHORT).show();
//prima di eseguire il parsing inizializzo gli arraylist
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle("Multiplayer.it");
mProgressDialog.setMessage("Caricamento articoli...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
titoli = new ArrayList<String>();
immagine = new ArrayList<Integer>();
//content = new ArrayList<String>();
}
@Override
protected String doInBackground(String... params) {
try {
Document doc = Jsoup.connect(BLOG_URL).get();
//Elements nodeBlogStatsimg = doc.select("img[src~=(?i)\\.(jpe?g)]");
Elements nodeBlogStats = doc.select("div.news-col-0 h3"); //per multiplayer.it Elements nodeBlogStats = doc.select("div.news-col-0 h3"); per ftv #comunePartINI > option
for(Element sezione : nodeBlogStats)
{
titoli.add(sezione.text());
}
} catch (Exception e) {
// In caso di errore
Log.e("ESEMPIO", "ERRORE NEL PARSING");
}
return null;
}
@Override
protected void onPostExecute(String result)
{
// dopo che ho eseguito il parsing mostro i dati nella listview
// usando il custom array adpater ParsingArrayAdapter
ParsingArrayAdapter adapter = new ParsingArrayAdapter(MainActivity.this, titoli, immagine);
lista.setAdapter(adapter);
mProgressDialog.dismiss();
new Logo().execute();
}
}
// Classe per caricamento immagini..
// Logo AsyncTask
private class Logo extends AsyncTask<Void, Void, Void> {
Bitmap bitmap;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle("Android Basic JSoup Tutorial");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
// Connect to the web site
Document document = Jsoup.connect(BLOG_URL).get();
// Using Elements to get the class data
Elements img = document.select("div.news-col-0 img[src]");
// Locate the src attribute
String imgSrc = img.attr("src");
// Download image from URL
InputStream input = new java.net.URL(imgSrc).openStream();
// Decode Bitmap
bitmap = BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Set downloaded image into ImageView
ImageView logoimg = (ImageView) findViewById(R.id.imageView1);
logoimg.setImageBitmap(bitmap);
mProgressDialog.dismiss();
// ParsingArrayAdapter adapter = new ParsingArrayAdapter(MainActivity.this, titoli);
// lista.setAdapter(adapter);
}
}
}
答案 0 :(得分:1)
试试这个 -
// send where details is object...
Intent i = new Intent(context, SingleActivity.class);
i.putExtra(TAG_IMMAGINE, bitmap);
startActivity(i);
//receive in SingleActivity class...
Bitmap immagine = (Bitmap) intent.getParcelableExtra(TAG_IMMAGINE);
答案 1 :(得分:0)
问题是,传递给intent bundle中的额外内容是字符串而不是位图。您需要将字符串(我假设是图像的路径)转换为位图,然后才能在ImageView中进行设置。你需要做这样的事情:
String path = in.getStringExtra(TAG_IMMAGINE);
File imgFile = new File(path);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
lblImg.setImageBitmap(myImage);
}
答案 2 :(得分:0)
将字符串转换为位图。
String string = in.getStringExtra(TAG_IMMAGINE);
byte [] encodeByte=Base64.decode(string,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
答案 3 :(得分:0)
有可能将图像编码为Base64字符串。试试这个:
String base64Str = in.getStringExtra(TAG_IMMAGINE);
byte[] imageBytes = Base64.decode(base64Str, DEFAULT);
Bitmap immagine = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);