2012-11-21 31 views
7

我有一個小部件,它解析xml供稿並顯示其標題和圖像。在此小部件中,我使用的是定期更改內容(即標題和圖像)的服務.For這是我使用計時器類。當我們運行這個小部件,一些內容顯示沒有任何問題,但在某些時候它強制關閉,並顯示錯誤,如「RemoteViews小部件更新超過最大位圖內存使用情況(使用:2465280最大:2304000)總內存不能超過一次填充設備屏幕所需的內存「。請有人幫我解決這個問題...提前用於小部件更新的RemoteViews超過最大位圖內存使用錯誤

這裏感謝我的AppWidgetProvider =>

public class myappwidgetprovider extends AppWidgetProvider { 
    public static String urls="http://www.abc.com/en/rssfeeds/9/latest/rss.xml"; 
    // XML node keys 
    static final String KEY_HEAD = "item"; // parent node 
    //static final String KEY_TITLE = "title"; 
    static final String KEY_DATE = "pubDate"; 
    public static String headflag="english"; 
    public static String[] Title; 
     public static String[] Description; 
     public static String[] Tit; 
     public static String[] Tit2; 
     public static String[] Desc; 
     public static String[] Desc2; 
     public static String[] image; 
    public static TextView flashnews; 

    public static int i=0; 

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
     { 

     Log.i("Tag", "onCreateView"); 
     parse(); 


      RemoteViews remoteViews; 
         ComponentName thisWidget = new ComponentName(context,myappwidgetprovider .class); 

       int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget); 

       Intent intent = new Intent(context.getApplicationContext(), 
         Updatewidget.class); 
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds); 
          context.startService(intent); 

       } 





    public static void parse() 
    { 

      URL url; 

      try { 

       url = new URL(urls); 
       HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
       if((conn.getResponseCode() == HttpURLConnection.HTTP_OK)){ 
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
         DocumentBuilder db = dbf.newDocumentBuilder(); 
         Document doc; 
         doc = db.parse(url.openStream()); 
         doc.getDocumentElement().normalize(); 

         NodeList itemLst = doc.getElementsByTagName("item"); 
         Description = new String[itemLst.getLength()];//........ 
         Title = new String[itemLst.getLength()]; 
         Tit=new String[itemLst.getLength()]; 
         Tit2=new String[itemLst.getLength()]; 
         Desc=new String[itemLst.getLength()]; 
         Desc2=new String[itemLst.getLength()]; 
         image= new String[itemLst.getLength()]; 

         for(int i=0; i < itemLst.getLength(); i++){ 

          Node item = itemLst.item(i); 
          if(item.getNodeType() == Node.ELEMENT_NODE){ 
            Element ielem = (Element)item; 
            NodeList title = ielem.getElementsByTagName("title"); 
            NodeList date = ielem.getElementsByTagName("pubDate"); 
            NodeList description = ielem.getElementsByTagName("description"); 
            Tit[i]= title.item(0).getChildNodes().item(0).getNodeValue(); 
            Desc[i]= description.item(0).getChildNodes().item(0).getNodeValue(); 
            Tit2[i]=Translate.title(Tit[i]); 
            Desc2[i]=Translate.description(Desc[i]); 
            if(headflag=="malayalam") 
            { 
             Desc2[i]=Desc2[i].replace("read more","IqSpXÂ"); 
            } 
            Title[i] =Tit2[i]; 
            if (Desc2[i].contains("<img ")){ 
             String img = Desc2[i].substring(Desc2[i].indexOf("<img ")); 
             String cleanUp = img.substring(0, img.indexOf(">")+1); 
             img = img.substring(img.indexOf("src=") + 5); 
             int indexOf = img.indexOf("'"); 
             if (indexOf==-1){ 
              indexOf = img.indexOf("\""); 
             } 
             img = img.substring(0, indexOf); 

           //setImgLink(img); 
            if(headflag=="malayalam") 
            { 
             String img2=img.replace("files","files/imagecache/android_320"); 
             Description[i]=Desc2[i].replace(img,img2); 
             image[i]=img2; 
            } 

           else 
           { 
            String img2=img.replace("files","files/imagecache/android_1_img"); 
            Description[i]=Desc2[i].replace(img,img2); 
            image[i]=img2; 
           } 
            } 
           else 
           { 
            Description[i] =Desc2[i]; 
           } 



          } 

          } 

         } 
      } catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (DOMException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (ParserConfigurationException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (SAXException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
    } 

} 

,這裏是我的服務=>

public class Updatewidget extends Service { 
    static String UPDATEMOOD ="UPDATEMOOD"; 
    public Intent newintent; 
    public AppWidgetManager app; 
    public RemoteViews newviews; 
    public int[] newappid; 
    int i=0; 
    @Override 
    public void onStart(final Intent intent, int startId) { 

     Log.i("Tag", "Service Called!!!!!!!!!!!!!"); 

     newintent=intent; 
       int[] allWidgetIds = intent 
        .getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS); 
     newappid=allWidgetIds; 

     final AppWidgetManager appWidgetMan = AppWidgetManager.getInstance(this); 
     app=appWidgetMan; 
     final RemoteViews views = new RemoteViews(this.getPackageName(),R.layout.widget_main); 
     newviews=views; 
     views.setTextViewText(R.id.title, myappwidgetprovider.Title[0]); 
     Bitmap bitmap; 
      try { 
       bitmap = BitmapFactory.decodeStream((InputStream)new URL(myappwidgetprovider.image[0]).getContent()); 


       views.setImageViewBitmap(R.id.imageView4, bitmap); 
      } catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     appWidgetMan.updateAppWidget(allWidgetIds, views); 

     new ProgressAsyncTask().execute(); 








    } 



    public class ProgressAsyncTask extends 
     AsyncTask<Void, Integer, Void> { 

     @Override 
     protected Void doInBackground(Void... params) { 
      // TODO Auto-generated method stub 
      int delay = 5000; // delay for 5 sec. 

      int period = 5000; // repeat every sec. 

      Timer timer = new Timer(); 
       timer.scheduleAtFixedRate(new TimerTask() { 

        public void run() { 
         i++; 
         if(i==5) 
         { 
          i=0; 
         } 

         int[] allWidgetIds = newintent 
            .getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS); 
           newviews.setTextViewText(R.id.title, myappwidgetprovider .Title[i]); 
         Bitmap bitmap; 
          try { 
           bitmap = BitmapFactory.decodeStream((InputStream)new URL(myappwidgetprovider .image[i]).getContent()); 


           newviews.setImageViewBitmap(R.id.imageView4, bitmap); 
          } catch (MalformedURLException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } catch (IOException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 
         app.updateAppWidget(allWidgetIds, newviews); 
           } 


        }, delay, period); 
      return null; 
     } 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 





} 

回答

6

我只是解決了一個類似的問題。 但我沒有加載任何來自網絡的內容。 我的小部件針對不同的小部件尺寸顯示不同的圖像。對於某些屏幕,與您顯示小部件4x4時出現的錯誤相同。

那有什麼關係,我在developer.google.com發現:

使用的RemoteViews對象不能超過 總的位圖內存要求填寫的屏的1.5倍,即。 (屏幕寬度x屏幕 高度x 4 x 1.5)字節。

所以我只能在小部件中對我的位圖大小加以限制以適應此要求。

在您的特定情況下,當從流中加載位圖大於可能時,發生錯誤。

我覺得

公共靜態位圖decodeStream的ussage(InputStream爲,矩形outPadding, BitmapFactory.Options選擇採用)

與正確的選項應該有所幫助。 (可能需要使用inSampleSize選項)。

希望我的帖子有幫助。

+0

,我想我已經解決了這個問題。無論如何,謝謝你:) –

+0

@Rusanovskiy Artem:你怎麼知道傳遞給'decodeStream()'的確切選項?如果在窗口小部件中有四個位圖,並且您不知道源圖像的大小,那麼您如何知道您必須使用哪個'sampleSize'來防止出現「Exception」? – caw

+6

@BasimSherif:你能解釋你是如何解決你的問題的? – caw

1
Glide.with(mContext) 
     .load(imageUrl) 
     .override(480, 342) 

....

倍率(...)沒有的伎倆我。 一樣,沒有更多的錯誤: RemoteViews爲窗口更新超過最高位的內存使用錯誤

相關問題