Wednesday, May 8, 2013

[android help] Loading large, scaled bitmap causes OOM (OutOfMemoryError) (Android)


I have now tried a thousand times to solve this on my own without any success whatsoever. I've logged and debugged the application and from the information that I've gathered there is this one large spritesheet that is used for an animation. It's a .png with the size of 3000x1614 pixels. There are 10x6 sprites in the spritesheet that is essential for the animation. There are no gaps in between the sprites whatsoever to make it as memory-efficient as possible.


This is my method in which I load, decode and resize my bitmaps into the aspect ratio of the user's phone vs. my phone which I am building the game upon:



public Pixmap newResizedPixmap(String fileName, PixmapFormat format, double Width, double Height) {
// TODO Auto-generated method stub
Config config = null;
if (format == PixmapFormat.RGB565)
config = Config.RGB_565;
else if (format == PixmapFormat.ARGB4444)
config = Config.ARGB_4444;
else
config = Config.ARGB_8888;

Options options = new Options();
options.inPreferredConfig = config;
options.inPurgeable=true;
options.inInputShareable=true;
options.inDither=false;

InputStream in = null;
Bitmap bitmap = null;
try {
//OPEN FILE INTO INPUTSTREAM
in = assets.open(fileName);
//DECODE INPUTSTREAM
bitmap = BitmapFactory.decodeStream(in, null, options);

if (bitmap == null)
throw new RuntimeException("Couldn't load bitmap from asset '"+fileName+"'");
} catch (IOException e) {
throw new RuntimeException("Couldn't load bitmap from asset '"+fileName+"'");
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
}

if (bitmap.getConfig() == Config.RGB_565)
format = PixmapFormat.RGB565;
else if (bitmap.getConfig() == Config.ARGB_4444)
format = PixmapFormat.ARGB4444;
else
format = PixmapFormat.ARGB8888;

//THIS IS WHERE THE OOM HAPPENS
return new AndroidPixmap(Bitmap.createScaledBitmap(bitmap, (int)(Width), (int)(Height), true), format);
}



This is the LogCat output of the error:



05-07 12:57:18.570: E/dalvikvm-heap(636): Out of memory on a 29736016-byte allocation.
05-07 12:57:18.570: I/dalvikvm(636): "Thread-89" prio=5 tid=18 RUNNABLE
05-07 12:57:18.580: I/dalvikvm(636): | group="main" sCount=0 dsCount=0 obj=0x414a3c78 self=0x2a1b1818
05-07 12:57:18.580: I/dalvikvm(636): | sysTid=669 nice=0 sched=0/0 cgrp=apps handle=705796960
05-07 12:57:18.590: I/dalvikvm(636): | schedstat=( 14462294890 67436634083 2735 ) utm=1335 stm=111 core=0
05-07 12:57:18.590: I/dalvikvm(636): at android.graphics.Bitmap.nativeCreate(Native Method)
05-07 12:57:18.590: I/dalvikvm(636): at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
05-07 12:57:18.590: I/dalvikvm(636): at android.graphics.Bitmap.createBitmap(Bitmap.java:586)
05-07 12:57:18.590: I/dalvikvm(636): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:466)
05-07 12:57:18.590: I/dalvikvm(636): at com.NAME.framework.impl.AndroidGraphics.newResizedPixmap(AndroidGraphics.java:136)
05-07 12:57:18.590: I/dalvikvm(636): at com.NAME.GAME.GameScreen.(GameScreen.java:121)
05-07 12:57:18.600: I/dalvikvm(636): at com.NAME.GAME.CategoryScreen.update(CategoryScreen.java:169)
05-07 12:57:18.600: I/dalvikvm(636): at com.NAME.framework.impl.AndroidFastRenderView.run(AndroidFastRenderView.java:34)
05-07 12:57:18.600: I/dalvikvm(636): at java.lang.Thread.run(Thread.java:856)
05-07 12:57:18.620: I/Choreographer(636): Skipped 100 frames! The application may be doing too much work on its main thread.
05-07 12:57:18.670: W/dalvikvm(636): threadid=18: thread exiting with uncaught exception (group=0x40a13300)
05-07 12:57:18.720: E/AndroidRuntime(636): FATAL EXCEPTION: Thread-89
05-07 12:57:18.720: E/AndroidRuntime(636): java.lang.OutOfMemoryError
05-07 12:57:18.720: E/AndroidRuntime(636): at android.graphics.Bitmap.nativeCreate(Native Method)
05-07 12:57:18.720: E/AndroidRuntime(636): at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
05-07 12:57:18.720: E/AndroidRuntime(636): at android.graphics.Bitmap.createBitmap(Bitmap.java:586)
05-07 12:57:18.720: E/AndroidRuntime(636): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:466)
05-07 12:57:18.720: E/AndroidRuntime(636): at com.NAME.framework.impl.AndroidGraphics.newResizedPixmap(AndroidGraphics.java:136)
05-07 12:57:18.720: E/AndroidRuntime(636): at com.NAME.GAME.GameScreen.(GameScreen.java:121)
05-07 12:57:18.720: E/AndroidRuntime(636): at com.NAME.GAME.CategoryScreen.update(CategoryScreen.java:169)
05-07 12:57:18.720: E/AndroidRuntime(636): at com.NAME.framework.impl.AndroidFastRenderView.run(AndroidFastRenderView.java:34)
05-07 12:57:18.720: E/AndroidRuntime(636): at java.lang.Thread.run(Thread.java:856)
05-07 12:57:18.847: D/Activity:(636): Pausing...



This is the Memory leak report using the DDMS:



Problem Suspect 1


One instance of "com.NAME.framework.impl.AndroidPixmap" loaded by "dalvik.system.PathClassLoader @ 0x41a06f90" occupies 12 393 680 (54,30%) bytes. The memory is accumulated in one instance of "byte[]" loaded by "".


Keywords dalvik.system.PathClassLoader @ 0x41a06f90 com.NAME.framework.impl.AndroidPixmap byte[]


Details »


Problem Suspect 2


The class "android.content.res.Resources", loaded by "", occupies 4 133 808 (18,11%) bytes. The memory is accumulated in one instance of "java.lang.Object[]" loaded by "".


Keywords java.lang.Object[] android.content.res.Resources


Details »


Problem Suspect 3


8 instances of "android.graphics.Bitmap", loaded by "" occupy 2 696 680 (11,82%) bytes.


Biggest instances: •android.graphics.Bitmap @ 0x41462ba0 - 1 048 656 (4,59%) bytes. •android.graphics.Bitmap @ 0x41a08cf0 - 768 064 (3,37%) bytes. •android.graphics.Bitmap @ 0x41432990 - 635 872 (2,79%) bytes.


Keywords android.graphics.Bitmap


Details »




Additional Information: I use either RGB565 or ARGB4444 as the config for my images. I wish to use ARGB8888 for the images with gradients, but it just takes up too much memory. Another thing to note is that I do recycle my bitmaps when I do not need them anymore.


Another thing that seems to consume a lot of memory is my Assets class, which consists of all the "Pixmaps" that the game uses. The bitmaps loaded from the assets are stored in these "Pixmaps" and removed when not needed anymore (the bitmaps that is). Is there maybe a better way to store these objects? Please let me know:



public static Pixmap image1;
public static Pixmap image2;
public static Pixmap image3;
public static Pixmap image4;
public static Pixmap image5;
public static Pixmap image6;
public static Pixmap image7;
public static Pixmap image8;
public static Pixmap image9;
public static Pixmap image10;
public static Pixmap image11;
...


Another thing to note is that the OOM only happens on devices with higher resolutions than my own (800x480), which is because the bitmaps get scaled to make the app fit larger devices.


Also take note that the images are being drawn on a canvas, since what I am developing is a game and I'm not very experienced with OpenGL.



So, what should I do to solve this issue? How am I able to load lots of scaled bitmaps without getting the OOM while keeping their quality?



EDIT #1: JUST TO MAKE SURE YOU ARE AWARE OF WHAT MY ISSUE IS


I am getting OOM at the following line:



Bitmap.createScaledBitmap(bitmap, (int)(Width), (int)(Height), true)


I'm desperate for help! This is my final blockade from releasing this bloody game!



EDIT #2: NEW METHODS I'VE TRIED THAT DIDN't WORK


I tried adding the decoded bitmaps to a LruCache before using them. I also tried a method where it creates a map of the bitmap in a temp file and then loads it in again. Like this:



//Copy the byte to the file
//Assume source bitmap loaded using options.inPreferredConfig = Config.ARGB_8888;
FileChannel channel = randomAccessFile.getChannel();
MappedByteBuffer map = null;
try {
map = channel.map(MapMode.READ_WRITE, 0, width*height*4);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bitmap.copyPixelsToBuffer(map);
//recycle the source bitmap, this will be no longer used.
bitmap.recycle();
//Create a new bitmap to load the bitmap again.
bitmap = Bitmap.createBitmap(width, height, config);
map.position(0);
//load it back from temporary
bitmap.copyPixelsFromBuffer(map);
//close the temporary file and channel , then delete that also
try {
channel.close();
randomAccessFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

((AndroidGame) activity).addBitmapToMemoryCache(""+fileName, Bitmap.createScaledBitmap(bitmap, (int)(Width), (int)(Height), true));

return new AndroidPixmap(((AndroidGame) activity).getBitmapFromMemCache(""+fileName), format);


.

stackoverflow.comm

[android help] Database insertion taking too much time- android sqlite


I am trying to insert around 2800 records into the sqlite database, it is taking 150 sec, which is way too much! Could anyone please tell how to optimize this insertion.



public void createVariantEntry(ArrayList> str) {
InsertHelper ih = new InsertHelper(Database, VARIANT_TABLE_NAME);
final int varid = ih.getColumnIndex(VARIANT_ID);
final int varmakeid = ih.getColumnIndex(VARIANT_MAKE_ID);
final int varmodid = ih.getColumnIndex(VARIANT_MODEL_ID);
final int varname = ih.getColumnIndex(VARIANT_NAME);
final int varposteddate = ih.getColumnIndex(VARIANT_POSTED_DATE);
for(int i=0;i<1253;i++)
{
ih.prepareForInsert();
ih.bind(varid, str.get(i).get(0));
ih.bind(varmakeid, str.get(i).get(1));
ih.bind(varmodid, str.get(i).get(2));
ih.bind(varname, str.get(i).get(3));
ih.bind(varposteddate, str.get(i).get(4));
ih.execute();
}
for(int i=1255;i {
ih.prepareForInsert();
ih.bind(varid, str.get(i).get(0));
ih.bind(varmakeid, str.get(i).get(1));
ih.bind(varmodid, str.get(i).get(2));
ih.bind(varname, str.get(i).get(3));
ih.bind(varposteddate, str.get(i).get(4));
ih.execute();
}
ih.close();
}


.

stackoverflow.comm

[android help] Setting up webserver to access media content for Android, Blackberry, and iPhone applications


first simply create VedioDownloader class and pass the url for download the vedio and call this class in thread in mainactivity



import android.os.Environment;
import android.util.Log;

public class VedioDownloader implements Runnable {
String vedio_URL = "http://daily3gp.com/vids/747.3gp";
//if download image then simply pass image url such as
//String image_URL = "http://www.appliconic.com/screen.jpg";
public void run() {
// TODO Auto-generated method stub
HttpURLConnection conn = null;
URL url = null;
try {
url = new URL(vedio_URLL);
} catch (MalformedURLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
conn = (HttpURLConnection) url.openConnection();
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
conn.setDoInput(true);

try {
conn.connect();
Log.d("Connection", "Connection Established");
InputStream input = conn.getInputStream();
File storagePath = Environment.getExternalStorageDirectory();
OutputStream output = new FileOutputStream(new File(storagePath,
"vedio.3gp"));
Log.d("1", "1");
byte[] buffer = new byte[input.available()];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
Log.d("2", "2");
output.close();
input.close();

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


}


mainactivity class



public class MainActivity extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("aftar thread","befor thread");
Thread objThread=new Thread(new VedioDownloader());
objThread.start();
Log.d("aftar thread","fater thread");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}



.

stackoverflow.comm

[android help] Emulator response is very slow

android - Emulator response is very slow - Stack Overflow







Tell me more ×

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

















Emulator in android is very slow and at times it is not responding as well. Does emulator performace is dependent on system configurations?


Is there any other way to view the android app output other than emulator? Kindly please suggest me as emulator is wasting most of my time.





























Yes, you can speed up the emulator by running the Atom x86 system images instead of the ARM ones (since those CPU instructions need to be translated and thus is slower). Read more here:


http://developer.android.com/tools/devices/emulator.html#accel-vm























The android emulator is slow, can't do much about it. However Intel HAX can speed up your emulator speed significanly. HAX must be installed from your android-sdk-direcotory/extrax/ Then setup your emulator to use the correct system-image. Take a look at intel-hardware-accelerated-execution-manager






















It is not a problem with your environment, it is just that the emulator is very slow.


Practically I use a real phone to do my tests. It is faster and tests are more realistic. But if you want to test your application on a lot of different Android versions and don't want to buy several phones, you will have to use the emulator from time to time.


The Android SDK now alows to use an x86-based Android emulator. See http://developer.android.com/tools/devices/emulator.html#accel-vm


Try Android x86. It's much faster than the Google Android emulator. Follow these steps:



  • Install VirtualBox.




  • Download the ISO file that you need.




  • Create a virtual machine as Linux 2.6/Other Linux, 512 Mb RAM, HD 2 GB. Network: PCnet-Fast III, attached to NAT. You can also use a bridged adapter, but you need a DHCP server in your environment.




  • Install Android x86 on the emulator, run it.




  • Press Alt+F1, type netcfg, remember the IP address, press Alt+F7.




  • Run cmd on your Windows XP system, change the directory to your Android tools directory, type adb connect .




  • Start Eclipse, open the ADT plugin, find the device, and enjoy!





















default






.

stackoverflow.comm

[android help] Android: making a simple layout


I would like to construct a simple table layout in Android BUT I got some problems with it. At part 1: there are some text views so I would like to put that TableRow's height to wrap_content. At part 2: same thing.


It works all still now BUT:


At part 3: there are 2 listview-s and at part 4 there are some buttons. I would like to do something like this: let the part 4's height to wrap content but make the part 3 to fill all the empty space on the screen.


a busy cat


Please give me a solution for this, beacause when i probed the part 3 filled out all the emty space till the bottom of the screen. Now I tryed with weight_sum BUT I do not like it because I would let the 4'd row to wrap_content.



xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:weightSum="1.0" >

android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:weightSum="1.0" >

android:id="@+id/phone_tab_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:background="@layout/style_right_border"
android:gravity="center"
android:singleLine="true"
android:text="Phone Tab"
android:textColor="@android:color/white"
android:textSize="14dp" />

android:id="@+id/pc_tab_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:gravity="center"
android:singleLine="true"
android:text="PC Tab"
android:textColor="@android:color/white"
android:textSize="14dp" />


android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@layout/style_bottom_border" >

android:id="@+id/phone_path_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:background="@layout/style_bott_right_border"
android:gravity="center"
android:paddingBottom="5dp"
android:singleLine="true"
android:text="mnt/sdcard/Movies/dpad/sadas/asda/sda/as"
android:textColor="@android:color/white"
android:textSize="8dp" />

android:id="@+id/pc_path_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:gravity="center"
android:paddingBottom="5dp"
android:singleLine="true"
android:text="mnt/sdcard/Movies/dpad/sadas/asda/sda/as"
android:textColor="@android:color/white"
android:textSize="8dp" />


android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="0.9"
android:weightSum="1.0" >

android:id="@+id/listview_phone_files"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:background="@layout/style_right_border" />

android:id="@+id/listview_pc_files"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7" />


android:id="@+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="0.1"
android:background="@layout/style_top_border"
android:gravity="center"
android:weightSum="1.0" >

android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Button" />

android:id="@+id/button_active_tab_changer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:background="@android:color/transparent"
android:onClick="changeActiveTabButton"
android:src="@drawable/button_change_tab" />

android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Button" />



.

stackoverflow.comm

[android help] How to get data from JsonArray within Array using Javascript/JQuery?


I'm having the service response like,



{
"Name": [
[{
"Key": "A",
"Value": "Sample1"
}
],
[{
"Key": "A",
"Value": "Sample2"
}
],
[{
"Key": "A",
"Value": "Sample3"
}
],
[{
"Key": "A",
"Value": "Sample4"
}
]
],
"Title": "Office"
}


I need the output as Value field.


I tried lot of ways.But not getting any solutions. Please help me..



.

stackoverflow.comm

Tuesday, May 7, 2013

[General] GO SMS Pro Auto Draft Saves


Hey guys,

So far I love Go SMS Pro as its much better than the stock S4 messaging app.

The only problem I have is that when I click on a text message conversation and then back out without typing anything, it "saves to draft" and then makes that message go all the way to the top. Is there any way to turn off drafts? I've looked anywhere but can't seem to find it.

Thx in advanced.



.

forum.xda-developers.com

[General] Browser making noises when loading pages


Got a GS4 a couple days ago and noticed today that when surfing the internet with the default browser there is a voice that says "loading started" "page loaded" each time I open a new page. Anyone have any idea where that came from and how I turn it off?



.

forum.xda-developers.com

[General] Can't add music to my phone


When you plug your phone into your computer, you will usually have a few options for how you want the phone to connect. After you have connected your phone using your USB cable, there should be some kind of USB connection icon in your Notification bar. If you swipe down and tap that icon, you should get some options on how to connect. You'll want to select the one that's labeled either "USB Mass Storage" or "Disk Drive," which will allow your computer to see your phone's storage as another drive. If you're not seeing anything on your computer, the phone might be set to "Charge Only."



.

forum.xda-developers.com

[General] Locked Gallery?


That's weird. Are you referring to the stock Gallery app? Where do you store your pics--internal storage or an external SD card? If external SD, I wonder if something might have happened to the card. I was going to suggest that perhaps the microSD became write-protected, but the microSDs don't have the write-protection switch like the full-size SDs, so I'm not sure how that would have happened.

You didn't encrypt your phone, did you?

In any case, you could try going to Settings/Apps/All, selecting Gallery, and clearing the cache. Then open the Gallery app again and see what happens.



.

forum.xda-developers.com

[General] Locked up


Hi, i am having a problem. One of my friends locked my cell phone android to the point it reached the gmail credentials. My problem is that i can't remenber my password, i already changed it on my pc, but the cell phone asks for the original one :S . Is there a way to just rollback to the pattern lock?



.

forum.xda-developers.com

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...