前缀为斜杠的URL,位图资源(/)

时间:2014-03-14 13:09:06

标签: android android-listview bitmap filenotfoundexception

我尝试使用SimpleAdapter填充ListView,对于String值,我的编码工作正常。

...
ListView lv = (ListView)findViewById(R.id.list_jobs);
data = new ArrayList<Map>();
String[] from = {"job_postitle", "job_company", "job_location", "job_spec", "company_logo"};
int[] to = {R.id.job_postitle, R.id.job_company, R.id.job_location, R.id.job_spec, R.id.company_logo};
adapter = new SimpleAdapter(this, (List<? extends Map<String, ?>>) data, R.layout.activity_job_each, from, to);

lv.setAdapter(adapter);
...

我使用HashMap设置值

...
Map m = new HashMap();
m.put("job_postitle", iObj.getString("title"));
m.put("job_company", iObj.getString("company"));
m.put("job_location", iObj.getJSONArray("location").toString());
m.put("job_spec", iObj.getJSONArray("specialization").toString());
m.put("company_logo", R.drawable.ic_launcher);
...

company_logo是一个ImageView,如果我使用R.drawable.ic_launcher作为值,它可以工作。但是,如果我设置远程图像的URL或Bitmap资源等,则值将始终以斜杠(/)为前缀,例如:

...
URL url = new URL( Matcher.quoteReplacement( params[0] ) );
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();

stream = httpConnection.getInputStream();
bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);
stream.close();
...
return bitmap;
...

----------------------------更新------------------ ----------

其中params是来自AsyncTask doInBackground函数的数组

protected Bitmap doInBackground(String... params) {...}

当我在LogCat中打印params值时,我会得到这些...... for Log.d(TAG,params.toString());

03-16 06:06:43.781: D/param(951): [Ljava.lang.String;@41820230

和Log.d(TAG,params [0] .toString());

03-16 06:06:43.781: D/param(951): http://10.0.2.2/assets/img/blog/2.jpg

----------------------------更新------------------ ----------

并用此位图替换company_logo值,然后我将收到此错误:

03-14 20:49:55.780: E/BitmapFactory(948): Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap@4183f340: open failed: ENOENT (No such file or directory)

如果我用URL替换,我会得到错误:

03-14 21:06:46.240: E/BitmapFactory(1003): Unable to decode stream: java.io.FileNotFoundException: /http:/10.0.2.2/assets/img/blog/1.jpg: open failed: ENOENT (No such file or directory)

注意位图或URL开头的斜杠?有谁知道如何删除斜线?

0 个答案:

没有答案
相关问题