Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法从URL解析图像_Android_Url - Fatal编程技术网

Android 无法从URL解析图像

Android 无法从URL解析图像,android,url,Android,Url,我一直在犯这样的错误: 10-16 21:56:30.502:INFO/System.out(361):在错误的位图uri:android.graphics.drawable上resolveUri失败。BitmapDrawable@44f0c0c8 在尝试运行此代码时 public class deals extends Activity{ private ArrayList<HashMap<String, Drawable>> myBooks; @Overr

我一直在犯这样的错误:

10-16 21:56:30.502:INFO/System.out(361):在错误的位图uri:android.graphics.drawable上resolveUri失败。BitmapDrawable@44f0c0c8

在尝试运行此代码时

public class deals extends Activity{

private ArrayList<HashMap<String, Drawable>> myBooks;





@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.deal);

myBooks = new ArrayList<HashMap<String,Drawable>>();
HashMap<String, Drawable> hm;


ListView myListView = (ListView) findViewById(R.id.listView1);

Environment.getExternalStorageDirectory().getAbsolutePath();

Drawable drawable3 = LoadImageFromWebOperations("http://www.bitterwallet.com/wp-content/uploads/2010/04/Tesco-Logo-Colour.jpg");

hm = new HashMap<String, Drawable>();
hm.put("Company", null);
hm.put("Offer", null);
hm.put("IMG", LoadImageFromWebOperations("http://2.bp.blogspot.com/_mCJwxGhlcQQ/TJxEHIOTpnI/AAAAAAAACqA/D11qqbNam80/s1600/TESCO+logo.jpg"));
myBooks.add(hm);

SimpleAdapter adapter = new SimpleAdapter(this, myBooks, R.layout.listview,  new String[]{"company","offer","IMG"}, new int[]{R.id.text1, R.id.text2, R.id.img});


myListView.setAdapter(adapter);


try {
    JSONArray jsonArray = new JSONArray(readTwitterFeed());
    int length = jsonArray.length();
      List<String> listContents = new ArrayList<String>(length);
     for (int i = 0; i < length; i++)
      {
//        listContents.add(jsonArray.getString(i));
         System.out.println(jsonArray.getString(i));

      }


 //     myListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listContents));


} catch (Exception e) {
    e.printStackTrace();
}

}

 public String readTwitterFeed() {

        StringBuilder builder = new StringBuilder();
        System.out.println("here");
        HttpClient client = new DefaultHttpClient();
        //HttpGet httpGet = new HttpGet(
            //  "http://www.hotels-in-london-hotels.com/mytrolly/service.php?request={\"mode\":\"category\"}");
        String url = "";
        try {
            //url = "http://hotels-in-london-hotels.com/mytrolly/service.php?request={\"mode\":\"search\",\"category\":\"2\",\"seller\":\"0\",\"page\":\"1\"}";
            url = "http://www.hotels-in-london-hotels.com/mytrolly/serviceSeller.php";





            System.out.println(url);
            String encodedurl = URLEncoder.encode(url,"UTF-8");
            Log.d("TEST", encodedurl);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } 


        HttpGet httpGet = new HttpGet(url);

        System.out.println("here");
        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }
            } else {
                //Log.e(ParseJSON.class.toString(), "Failed to download file");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            System.out.println("here is the error" + e.toString());
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("here is the error" + e.toString());
        }
        return builder.toString();
    }

 private Drawable LoadImageFromWebOperations(String url)
   {
                try
                {
                        InputStream is = (InputStream) new URL(url).getContent();
                        Drawable d = Drawable.createFromStream(is, "src name");
                        return d;
                }catch (Exception e) {
                        System.out.println("Exc="+e);
                        return null;
                }
        }
}
公共类交易扩展活动{
私人ArrayList myBooks;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.deal);
myBooks=newArrayList();
HashMap-hm;
ListView myListView=(ListView)findViewById(R.id.listView1);
Environment.getExternalStorageDirectory().getAbsolutePath();
Drawable Drawable 3=LoadImageFromWebOperations(“http://www.bitterwallet.com/wp-content/uploads/2010/04/Tesco-Logo-Colour.jpg");
hm=新的HashMap();
hm.put(“公司”,空);
hm.put(“要约”,无效);
hm.put(“IMG”),LoadImageFromWebOperations(“http://2.bp.blogspot.com/_mCJwxGhlcQQ/TJxEHIOTpnI/AAAAAAAACqA/D11qqbNam80/s1600/TESCO+logo.jpg),;
myBooks.add(hm);
simpledapter adapter=newsimpledapter(this,myBooks,R.layout.listview,新字符串[]{“company”,“offer”,“IMG”},新int[]{R.id.text1,R.id.text2,R.id.IMG});
myListView.setAdapter(适配器);
试一试{
JSONArray JSONArray=新的JSONArray(readTwitterFeed());
int length=jsonArray.length();
List listContents=新的ArrayList(长度);
for(int i=0;i
我试过做一些类似于这个小伙子的事情:


但我得到了一个空指针异常。有人知道如何解决这个问题吗?

我使用下面的代码获取url的图像,试试看,我希望得到你的答案

static Bitmap bmp;
private static File cacheDir;
static Bitmap bitmap = null;
static HttpURLConnection conn;

private static InputStream fetch(String urlString)
        throws MalformedURLException, IOException {

    conn = (HttpURLConnection) (new URL(urlString)).openConnection();
    conn.setDoInput(true);
    conn.connect();
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet request = new HttpGet(urlString);
    HttpResponse response = httpClient.execute(request);
    return conn.getInputStream();
}

Resources res;

// ***********New Function For Fatching
public static Drawable LoadImage(String URL) {
    Drawable drawable = null;
    Bitmap bm = null;
    try {
        if (fetch(URL) != null) {
            try {
                BitmapFactory.Options options = null;
                // options.inSampleSize=4;
                InputStream obj = (InputStream) fetch(URL);
                bm = BitmapFactory.decodeStream(obj, null, null);
                obj.close();
                conn.disconnect();
            } catch (OutOfMemoryError e) {
            } catch (IndexOutOfBoundsException e) {
            }
            drawable = new BitmapDrawable(bm);
            return drawable;
        } else {

            return null;
        }

    } catch (Exception e) {
        Log.e("GUIMethodClasas", "fetchDrawable failed", e);
        return null;

    }

}
请尝试以下代码:

URL url = new URL(yourImageURL);
HttpUrlConnection conn = (HttpUrlConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
Bitmap b = BitmapFactory.decodeStream(is);
yourImageViewObject.setImageBitmap(b);

Hope this will help you! Happy Coding

嘿,伙计。今晚回家后我会试试这个,让你知道我怎么样了,谢谢你。