OutOfMemoryError while using Universal Image Loader in gridview - universal-image-loader

I have around 53 images to show in gridview from drwable. But i was getting oom while doing that. So i started using universal image loader to cache these images on to disc and pull it from there..! But i am still getting oom.
Here is my configuration
imageLoader =ImageLoader.getInstance();
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"icons");
else
cacheDir=mContext.getCacheDir();
if(!cacheDir.exists())
cacheDir.mkdirs();
config= new ImageLoaderConfiguration.Builder(mContext)
.memoryCache(new WeakMemoryCache())
.denyCacheImageMultipleSizesInMemory()
.threadPoolSize(2)
.offOutOfMemoryHandling()
.discCache(new UnlimitedDiscCache(cacheDir))
.enableLogging()
.build();
imageLoader.init(config);
options = new DisplayImageOptions.Builder()
.cacheOnDisc()
.bitmapConfig(Bitmap.Config.RGB_565)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.build();
and this is my getview() of BaseAdapter
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
ViewHolder holder=new ViewHolder();
inflator = mContext.getLayoutInflater();
convertView = inflator.inflate(R.layout.imagelayout, null);
holder.imageView=(ImageView)convertView.findViewById(R.id.imgDet);
convertView.setTag(holder);
}
ViewHolder hold=(ViewHolder)convertView.getTag();
try
{
imageLoader.displayImage("drawable://" +mThumbIds[position], hold.imageView, options,new ImageLoadingListener() {
#Override
public void onLoadingStarted(String arg0, View arg1) {
// TODO Auto-generated method stub
}
#Override
public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
// TODO Auto-generated method stub
}
#Override
public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
// TODO Auto-generated method stub
}
#Override
public void onLoadingCancelled(String arg0, View arg1) {
// TODO Auto-generated method stub
}
});
}catch(OutOfMemoryError oom)
{
oom.printStackTrace();
}
catch(Exception ex)
{
ex.printStackTrace();
}
return convertView;
}
please help me with this.

Always Consider the native way to load drawables first.
ImageView.setImageResource(...) instead of using of ImageLoader.

Related

JxBrowser does not refresh

I am currently using JxBrowser as an embedded browser in my Java application. I create a browser like below:
public static void main(String[] args) {
Browser browser = new Browser();
BrowserView browserView = new BrowserView(browser);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.add(browserView, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
browser.loadURL("https://developer.microsoft.com/en-us/graph");
frame.addWindowListener(new WindowListener() {
#Override
public void windowClosing(WindowEvent e) {
}
#Override
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void windowClosed(WindowEvent arg0) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(browserView, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
browser.getCacheStorage().clearCache();
browser.loadURL("https://developer.microsoft.com/en-us/graph");
}
#Override
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
});
}
When the first URL is opened, I sign in and choose to not keep me signed in. I then close that window and when the second window opens, I see that my user is still logged in. As you can see I am clearing cache as well before opening the URL the second time. But the cache is not cleared.
I am not sure that the issue is related to caching. Would you please create a simple sample that can be used for reproducing the issue from my end?

Spring AOP Not working properly

I'm trying to handle exceptions with AOP approach in my Spring/Swing Application and I couldn't make it work.
Main Class:
public class MainFrame extends JFrame {
private JPanel mainPanel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainFrame() {
initializeMainPanel();
}
private void initializeMainPanel() {
exitLabel.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
throw new Exception("test");
}
});
}
}
Aspect Class:
#Aspect
public class AspectTest{
#AfterThrowing(pointcut = "execution(* com.test.MainFrame.*(..))", throwing = "ex")
public void logError(Exception ex) throws Throwable {
// ex.printStackTrace();
}
}
So, I throw an exception within my Mouse Listener and expect to catch it in my AspectTest class' AfterThrowing method but it does not work.
Can someone please help me to understand what I'm missing here?
#AfterThrowing cannot catch exceptions, only notice them and log them or do something similar. If you want to handle exceptions in an aspect you need to use an #Around advice.

how to disable camera using XposedHook?

I want to disable my camera using xposed. So I am not able to understand which native method should I hook so that my phone camera gets disabled.
I tried this:-
public class main1 implements IXposedHookLoadPackage {
#Override
public void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable{
if(!lpparam.packageName.equals("android.hardware.camera2"))
return;
findAndHookMethod("android.hardware.camera2.CameraManager", lpparam.classLoader, "openCamera", String.class, CameraDevice.StateCallback.class, Handler.class, new XC_MethodReplacement() {
#Override
protected Object replaceHookedMethod(MethodHookParam methodHookParam) throws Throwable {
XposedBridge.log("CameraBlocked");
return null;
}
});
XposedBridge.log("Loaded app: " + lpparam.packageName);
}
}
But it doesn't work. Please help me out with this.

start asynchtask oncreate after recognizing asynchtask

public class MainActivity extends Activity {
TextView statustv = (TextView) findViewById(R.id.status);;
ProgressDialog pd;
String status, url = "http://wvde.state.wv.us/closings/county/monongalia";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new School().execute();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class School extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(MainActivity.this);
pd.setTitle("Android Basic JSoup Tutorial");
pd.setMessage("Loading...");
pd.setIndeterminate(false);
pd.show();
}
#Override
protected Void doInBackground(Void... params) {
try {
Document doc = Jsoup.connect(url).get();
Elements table = doc.select("td#content_body");
status = table.select("table").text();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
statustv.setText(status);
pd.dismiss();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_refresh) {
new School().execute();
return true;
}
return super.onOptionsItemSelected(item);
}
}
How can I have new School().execute(); happen oncreate without getting a nullpointer error because right now when oncreate executes it executes new School().execute(); before it even knows what the asynchtask is. How can i have it execute correctly oncreate?
You can post a runnable to the current thread's handler. The runnable starts the AsyncTask.
Here is an easy example of using handler: https://stackoverflow.com/a/1921759/1843698
Official Doc for Handler: http://developer.android.com/reference/android/os/Handler.html
The handler schedule a task (your runnable) in current thread, and your task will be executed later in the same thread as soon as possible, but it will be executed after onCreate() finishes.

OSGI expose An "ClassNotFoundException: org.w3c.dom.***" Error when release

I only wrote the following codes in Activator.start() function
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
Node node = new Node() {
#Override
public Object setUserData(String arg0, Object arg1, UserDataHandler arg2) {
// TODO Auto-generated method stub
return null;
}
#Override
public void setTextContent(String arg0) throws DOMException {
// TODO Auto-generated method stub
}
#Override
public void setPrefix(String arg0) throws DOMException {
// TODO Auto-generated method stub
}
#Override
public void setNodeValue(String arg0) throws DOMException {
// TODO Auto-generated method stub
}
#Override
public Node replaceChild(Node arg0, Node arg1) throws DOMException {
// TODO Auto-generated method stub
return null;
}
#Override
public Node removeChild(Node arg0) throws DOMException {
// TODO Auto-generated method stub
return null;
}
#Override
public void normalize() {
// TODO Auto-generated method stub
System.out.println("normalize 方法调用");
}
#Override
public String lookupPrefix(String arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public String lookupNamespaceURI(String arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public boolean isSupported(String arg0, String arg1) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isSameNode(Node arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isEqualNode(Node arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isDefaultNamespace(String arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public Node insertBefore(Node arg0, Node arg1) throws DOMException {
// TODO Auto-generated method stub
return null;
}
#Override
public boolean hasChildNodes() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean hasAttributes() {
// TODO Auto-generated method stub
return false;
}
#Override
public Object getUserData(String arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public String getTextContent() throws DOMException {
// TODO Auto-generated method stub
return null;
}
#Override
public Node getPreviousSibling() {
// TODO Auto-generated method stub
return null;
}
#Override
public String getPrefix() {
// TODO Auto-generated method stub
return null;
}
#Override
public Node getParentNode() {
// TODO Auto-generated method stub
return null;
}
#Override
public Document getOwnerDocument() {
// TODO Auto-generated method stub
return null;
}
#Override
public String getNodeValue() throws DOMException {
// TODO Auto-generated method stub
return null;
}
#Override
public short getNodeType() {
// TODO Auto-generated method stub
return 0;
}
#Override
public String getNodeName() {
// TODO Auto-generated method stub
return null;
}
#Override
public Node getNextSibling() {
// TODO Auto-generated method stub
return null;
}
#Override
public String getNamespaceURI() {
// TODO Auto-generated method stub
return null;
}
#Override
public String getLocalName() {
// TODO Auto-generated method stub
return null;
}
#Override
public Node getLastChild() {
// TODO Auto-generated method stub
return null;
}
#Override
public Node getFirstChild() {
// TODO Auto-generated method stub
return null;
}
#Override
public Object getFeature(String arg0, String arg1) {
// TODO Auto-generated method stub
return null;
}
#Override
public NodeList getChildNodes() {
// TODO Auto-generated method stub
return null;
}
#Override
public String getBaseURI() {
// TODO Auto-generated method stub
return null;
}
#Override
public NamedNodeMap getAttributes() {
// TODO Auto-generated method stub
return null;
}
#Override
public short compareDocumentPosition(Node arg0) throws DOMException {
// TODO Auto-generated method stub
return 0;
}
#Override
public Node cloneNode(boolean arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public Node appendChild(Node arg0) throws DOMException {
// TODO Auto-generated method stub
return null;
}
};
node.normalize();
}
Everything goes well when run in eclipse environment, but, when release the product, ERRORS in log when runs:
Root exception:
java.lang.NoClassDefFoundError: org/w3c/dom/Node
Caused by: java.lang.ClassNotFoundException: org.w3c.dom.Node
Anyone can give some help?
OSGi gives access to system packages but only java.* packages by default, this does not include other packages like: javax.net , javax.xml , com.sun
Thus it is necessary to specify any of such packages for OSGi framework to export them through the system bundle making them accessible to other bundles that import them.
To do that you need to set a configuration property with the additional packages required by your bundles, try setting it as a system property before starting the OSGi framework such that it picks up this property when it first starts.
Assuming you are on OSGi 4.2, that property would be configured like:
org.osgi.framework.system.packages.extra=org.w3c.dom
You may want to check the Apache Felix Framework Configuration Properties for more details, though this property is part of the OSGi spec and thus should be available in other implementations as well
Please update your question to include the bundle's MANIFEST.MF
It looks like org.w3c.dom is not implicitly provided in your production. Check the Import-Package header, may be you don't have Import-Package: org.w3c.dom
If you are using Equinox, you can edit the config.ini and add "org.w3c.dom" to org.osgi.framework.system.packages key and import the same packages in your MANIFEST.MF
in my case adding
org.osgi.framework.bootdelegation=xx...xxx,org.w3c.dom
solved my problem.

Resources