我有一个小部件,解析XML提要,并显示其标题和图像。在这个小部件我使用的服务,定期改变内容(即,标题和图像)。为此我使用计时器类。当我们运行这个小部件,一些内容显示没有任何问题,但过了一段时间,它强制关闭,并显示一个错误,如“RemoteViews为小部件更新超过最大位图内存使用量(已用:2465280最大:2304000)总内存不能超过所需的设备的屏幕一次”。请有人帮助我解决这个问题...提前感谢
这里是我的应用程序窗口小部件提供者=〉
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;
}
}
4条答案
按热度按时间zfciruhq1#
我刚刚解决了一个类似的问题。但是我没有从网络上加载任何内容。我的小部件显示不同大小的小部件的不同图像。并且在一些屏幕上有同样的错误,就像你显示小部件4x4一样。
这就是我在www.example.com上找到developer.google.com:
RemoteViews对象使用的总位图内存不能超过填充屏幕所需的内存的1.5倍,即(屏幕宽度x屏幕高度x 4 x 1.5)字节。
因此,我可以在窗口小部件中对位图大小进行限制,以满足此要求。
在您的特定情况下,如果从流加载的位图大于可能的大小,则会发生错误。
我认为
公共静态位图解码流(输入流为,矩形输出填充,位图工厂。选项选择)
使用适当的选项应该会有帮助。(可能需要使用inSampleSize选项)。
希望我的帖子对你有帮助。
qv7cva1a2#
....
覆盖(...)为我做了这个把戏。没有更多的错误,如:小部件更新的RemoteViews超出最大位图内存使用错误
mefy6pfw3#
Google发明了一个有趣的图片缓存。我不知道为什么,但它大大增加了系统的负载。这就是为什么我每次都这样做,而不存储图片历史记录:
5f0d552i4#
演示RusArtM响应的第二部分
inSampleSize
的值上级1时,缩小图像。使用inSampleSize = 4
,我们得到的图像的宽度/高度是图像的1/4这是文档