使用parse.com并遇到分配内存问题 [英] Using parse.com and having allocation memory issue

查看:95
本文介绍了使用parse.com并遇到分配内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的新手,在过去3个月中,我一直在开发应用程序,并学到了一些东西.但是我还没有涉及到如何解决这个问题.我一直使用Parse.com作为服务器,发送图片,保存用户数据等.使用所有这些数据,如果我多次打开某些活动(尤其是带有图片的活动),则该应用将不断崩溃.现在,我已经压缩了图片,并使它们的最大分辨率为400x400.但是以某种方式,内存不足的分配不断弹出.有谁知道有什么方法可以解决这个问题?因为当应用程序在Android Studio中崩溃时,手机上的先前内存似乎已被删除(并非所有缓存数据).

I'm new to programming, I've been making an app for the last 3 months and learned a few things. But I haven't come across to how to solve this issue. I've been using Parse.com as my server, sending pictures, saving user data etc. With all of this data the app keeps crashing if I open some activities more than a few times, particularly activities with pictures. Now I have compressed the pictures and made them max 400x400 in resolution. But somehow the allocation is out of memory keeps popping up. Does anyone know any methods with which you can solve this? Because when the app crashes in Android Studio the previous memory which was on my phone seems to get deleted (Not all of the cache data).

谢谢.

//编辑这是Logcat

// EDIT this is the Logcat

**

Out of memory: Heap Size=43591KB, Allocated=14395KB, Limit=49152KB
06-10 03:39:26.220    9344-9344/com.parseapp.eseen.eseen E/dalvikvm﹕ Extra info: Footprint=43591KB, Allowed Footprint=43591KB, Trimmed=21928KB
06-10 03:39:26.220    9344-9344/com.parseapp.eseen.eseen D/skia﹕ --- Unable to allocate space, reportSizeToVM = true
06-10 03:39:26.220    9344-9344/com.parseapp.eseen.eseen W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40d90390)
06-10 03:39:26.230    9344-9344/com.parseapp.eseen.eseen E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.parseapp.eseen.eseen/com.parseapp.eseen.eseen.LoginActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
            at android.app.ActivityThread.access$600(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5511)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
            at android.view.LayoutInflater.createView(LayoutInflater.java:613)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:369)
            at android.app.Activity.setContentView(Activity.java:1912)
            at com.parseapp.eseen.eseen.LoginActivity.onCreate(LoginActivity.java:29)
            at android.app.Activity.performCreate(Activity.java:5066)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
            at android.app.ActivityThread.access$600(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5511)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
            at android.view.LayoutInflater.createView(LayoutInflater.java:587)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:369)
            at android.app.Activity.setContentView(Activity.java:1912)
            at com.parseapp.eseen.eseen.LoginActivity.onCreate(LoginActivity.java:29)
            at android.app.Activity.performCreate(Activity.java:5066)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
            at android.app.ActivityThread.access$600(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5511)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.OutOfMemoryError: (Heap Size=43591KB, Allocated=14395KB)
            at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:626)
            at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:473)
            at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
            at android.content.res.Resources.loadDrawable(Resources.java:1959)
            at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
            at android.view.View.<init>(View.java:3354)
            at android.view.View.<init>(View.java:3291)
            at android.view.ViewGroup.<init>(ViewGroup.java:432)
            at android.widget.RelativeLayout.<init>(RelativeLayout.java:184)
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
            at android.view.LayoutInflater.createView(LayoutInflater.java:587)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:369)
            at android.app.Activity.setContentView(Activity.java:1912)
            at com.parseapp.eseen.eseen.LoginActivity.onCreate(LoginActivity.java:29)
            at android.app.Activity.performCreate(Activity.java:5066)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
            at android.app.ActivityThread.access$600(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5511)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
            at dalvik.system.NativeStart.main(Native Method)

**

代码太短,无法放入此处,因为它限制为30,000个字符

The code is too long to put it here since it's limited to 30,000 chars

推荐答案

如果您使用parse.com并从数据库中获取图像并将其转换为位图,则会使您的应用程序崩溃.

If you are using parse.com and getting the Images from database and converting it to bitmaps it will crash your app..

您应该做的是,从parse.com获取URL的图像,其中有一个名为的方法.

what you should do is, get the image of the URL from parse.com there is method called..

 ParseFile.getUrl(); 

为此实现了Picasso库,这是从数据库加载图像的完美解决方案.

For that implement the Picasso library, which is perfect solution for loading images from database.

要在您的应用中实现毕加索,

To implement picasso in your app,

compile 'com.squareup.picasso:picasso:2.5.2'

然后从解析数据库中获取文件

Then get the file from parse database

ParseFile file = getParseFile("ImageColumn");

要加载文件

Uri url = Uri.parse(file.getUrl());

然后在传递文件网址后获取上下文和..add.

then just get context and ..add following passing your file url..

Picasso.with(context)
   .load(url)  //load from your URL of parseFile
   .into(imageView);  //set it to your ImageView

这就是为什么要执行此操作..

Here is the reason, why you should implement this..

获取位图文件将消耗大量内存,这将为您提供 OutOfMemory异常,您现在正在获取.所以你 只需获取文件的URL即可.正在载入图片 依赖互联网.因此您通过不进行转换来节省内存 每一点都变成图像. 另外,您将循环处理每张图像,这将导致您的应用崩溃.

Getting Bitmap file will consume so much memory, and it will give you OutOfMemory Exception, which you are getting right now. So instead you just get the URL of file and that's it. Loading image will be dependent on the Internet. so you are saving the memory by not converting every single bit into image. Also you will do in loop for every image will cause your app to crash.

ALSO完整教程可从官方网站获得 毕加索图书馆

ALSO full tutorial will be available from Official website Picasso Library

这篇关于使用parse.com并遇到分配内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆