简体   繁体   中英

Android app crashing on certain devices when handling large files

I'm working on an encryption and decryption app on Android, that deals with video files. My app runs perfectly on some devices but crashes on some, while dealing with the same file size. My code for the InputStream and OutputStream is below

package com.example.rama.beta;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button encryptButton = (Button) findViewById(R.id.button1);
        Button DecryptButton = (Button) findViewById(R.id.button2);
        encryptButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    encrypt();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        DecryptButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    decrypt();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

    }
    static void encrypt() throws IOException, NoSuchAlgorithmException,
                NoSuchPaddingException, InvalidKeyException {
            File extStore = Environment.getExternalStorageDirectory();
            FileInputStream fis = new FileInputStream(extStore + "/abc.m4v");
            FileOutputStream fos = new FileOutputStream(extStore + "/encabc.m4v");

            SecretKeySpec sks = new SecretKeySpec("xxxx".getBytes(),
                    "AES");

            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, sks);

            CipherOutputStream cos = new CipherOutputStream(fos, cipher);

            int read;
            byte[] buffer = new byte[1024];
            while ((read = fis.read(buffer)) != -1) {
                cos.write(buffer, 0, read);
            }

            cos.flush();
            cos.close();
            fis.close();
        }

    static void decrypt() throws IOException, NoSuchAlgorithmException,
                NoSuchPaddingException, InvalidKeyException {

            File extStore = Environment.getExternalStorageDirectory();
            FileInputStream fis = new FileInputStream(extStore + "/encabc.m4v");

            FileOutputStream fos = new FileOutputStream(extStore + "/decabc.m4v");
            SecretKeySpec sks = new SecretKeySpec("xxxx".getBytes(),
                    "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, sks);
            CipherInputStream cis = new CipherInputStream(fis, cipher);
            int read;
            byte[] buffer = new byte[1024];
            while ((read = cis.read(buffer)) != -1) {
                fos.write(buffer, 0, read);
            }
            fos.flush();
            fos.close();
            cis.close();

        }
    }

The noticeable difference between both android devices I'm using is the RAM, the app works perfectly while handling a 50mb video on a device with 2gb RAM, but crashes on a device with 1gb RAM. I'm not sure if it is a RAM problem or something else. Appreciate any inputs.

06-24 18:21:08.179 D/PhoneWindowManager(1760): mFullScreenIsEnable = true, mAlwaysFullScreen = false\par
06-24 18:21:08.179 I/ActivityManager(1760): START u0 \{act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10304000 cmp=com.example.rama.beta/.MainActivity\} from pid 1989\par
06-24 18:21:08.349 D/dalvikvm(20695): Late-enabling CheckJNI\par
06-24 18:21:08.349 I/ActivityManager(1760): Start proc com.example.rama.beta for activity com.example.rama.beta/.MainActivity: pid=20695 uid=10046 gids=\{50046, 1015, 1028\}\par
06-24 18:21:08.379 D/PhoneWindowManager(1760): mFullScreenIsEnable = true, mAlwaysFullScreen = false\par
06-24 18:21:08.419 E/SurfaceFlinger(1238): ro.sf.lcd_density must be defined as a build property\par
06-24 18:21:08.439 I/qtaguid (1760): Failed write_ctrl(s 1 10046) res=-1 errno=1\par
06-24 18:21:08.439 W/NetworkManagementSocketTagger(1760): setKernelCountSet(10046, 1) failed with errno -1\par
06-24 18:21:08.439 D/PhoneWindowManager(1760): mFullScreenIsEnable = true, mAlwaysFullScreen = false\par
06-24 18:21:08.449 E/SurfaceFlinger(1238): ro.sf.lcd_density must be defined as a build property\par
06-24 18:21:08.459 D/PhoneWindowManager(1760): mFullScreenIsEnable = true, mAlwaysFullScreen = false\par
06-24 18:21:08.469 D/PhoneWindowManager(1760): mFullScreenIsEnable = true, mAlwaysFullScreen = false\par
06-24 18:21:08.479 E/Trace   (20695): error opening trace file: No such file or directory (2)\par
06-24 18:21:08.649 D/PhoneWindowManager(1760): mFullScreenIsEnable = true, mAlwaysFullScreen = false\par
06-24 18:21:08.669 E/SurfaceFlinger(1238): ro.sf.lcd_density must be defined as a build property\par
06-24 18:21:08.679 D/libEGL  (20695): loaded /system/lib/egl/libEGL_mali.so\par
06-24 18:21:08.679 D/libEGL  (20695): loaded /system/lib/egl/libGLESv1_CM_mali.so\par
06-24 18:21:08.679 D/libEGL  (20695): loaded /system/lib/egl/libGLESv2_mali.so\par
06-24 18:21:08.719 W/BufferQueue(1238): freeAllBuffersLocked called but mQueue is not empty\par
06-24 18:21:08.749 D/OpenGLRenderer(20695): Enabling debug mode 0\par
06-24 18:21:08.929 I/ActivityManager(1760): Displayed com.example.rama.beta/.MainActivity: +599ms\par
06-24 18:21:11.089 I/dalvikvm(20695): Total arena pages for JIT: 11\par
06-24 18:21:11.089 I/dalvikvm(20695): Total arena pages for JIT: 12\par
06-24 18:21:11.099 I/dalvikvm(20695): Total arena pages for JIT: 13\par
06-24 18:21:11.099 I/dalvikvm(20695): Total arena pages for JIT: 14\par
06-24 18:21:11.099 I/dalvikvm(20695): Total arena pages for JIT: 15\par
06-24 18:21:11.099 I/dalvikvm(20695): Total arena pages for JIT: 16\par
06-24 18:21:11.099 I/dalvikvm(20695): Total arena pages for JIT: 17\par
06-24 18:21:11.129 I/dalvikvm(20695): Total arena pages for JIT: 18\par
06-24 18:21:11.999 D/dalvikvm(20695): GC_CONCURRENT freed 1832K, 40% free 4175K/6880K, paused 3ms+2ms, total 35ms\par
06-24 18:21:12.729 D/dalvikvm(20695): GC_CONCURRENT freed 1947K, 41% free 4175K/7028K, paused 3ms+3ms, total 19ms\par
06-24 18:21:13.649 D/dalvikvm(20695): GC_CONCURRENT freed 1952K, 41% free 4172K/7036K, paused 3ms+2ms, total 34ms\par
06-24 18:21:14.419 D/dalvikvm(20695): GC_CONCURRENT freed 1925K, 41% free 4197K/7036K, paused 2ms+1ms, total 26ms\par
06-24 18:21:15.159 D/dalvikvm(20695): GC_CONCURRENT freed 1985K, 41% free 4172K/7068K, paused 2ms+2ms, total 37ms\par
06-24 18:21:16.019 D/dalvikvm(20695): GC_CONCURRENT freed 1950K, 41% free 4194K/7068K, paused 3ms+52ms, total 77ms\par
06-24 18:21:16.049 D/dalvikvm(1760): GC_CONCURRENT freed 2590K, 30% free 12059K/17108K, paused 3ms+37ms, total 223ms\par
06-24 18:21:17.489 D/dalvikvm(20695): GC_CONCURRENT freed 1969K, 41% free 4175K/7068K, paused 3ms+3ms, total 18ms\par
06-24 18:21:17.869 I/InputDispatcher(1760): Application is not responding: Window\{41b2f780 u0 com.example.rama.beta/com.example.rama.beta.MainActivity\}.  It has been 5006.0ms since event, 5005.6ms since wait started.  Reason: Waiting because the touched window has not finished processing the input events that were previously delivered to it.\par
06-24 18:21:17.879 D/InputManager-JNI(1760): notifyANR\par
06-24 18:21:17.879 I/WindowManager(1760): Input event dispatching timed out sending to com.example.rama.beta/com.example.rama.beta.MainActivity\par
06-24 18:21:17.929 I/Process (1760): Sending signal. PID: 20695 SIG: 3\par
06-24 18:21:17.929 I/dalvikvm(20695): threadid=3: reacting to signal 3\par
06-24 18:21:17.949 I/dalvikvm(20695): Wrote stack traces to '/data/anr/traces.txt'\par
06-24 18:21:17.949 I/Process (1760): Sending signal. PID: 1760 SIG: 3\par
06-24 18:21:17.949 I/dalvikvm(1760): threadid=3: reacting to signal 3\par
06-24 18:21:17.979 D/dalvikvm(20695): GC_CONCURRENT freed 1208K, 41% free 4171K/7068K, paused 1ms+4ms, total 29ms\par
06-24 18:21:18.189 I/Process (1760): Sending signal. PID: 1989 SIG: 3\par
06-24 18:21:18.189 I/dalvikvm(1989): threadid=3: reacting to signal 3\par
06-24 18:21:18.189 I/dalvikvm(1760): Wrote stack traces to '/data/anr/traces.txt'\par
06-24 18:21:18.199 I/Process (1760): Sending signal. PID: 2171 SIG: 3\par
06-24 18:21:18.199 I/dalvikvm(2171): threadid=3: reacting to signal 3\par
06-24 18:21:18.259 I/dalvikvm(2171): Wrote stack traces to '/data/anr/traces.txt'\par
06-24 18:21:18.259 I/Process (1760): Sending signal. PID: 2180 SIG: 3\par
06-24 18:21:18.259 I/dalvikvm(2180): threadid=3: reacting to signal 3\par
06-24 18:21:18.259 D/ConnectivityService(1760): special network not available ni=mobile_hipri\par
06-24 18:21:18.279 I/dalvikvm(1989): Wrote stack traces to '/data/anr/traces.txt'\par
06-24 18:21:18.349 I/dalvikvm(2180): Wrote stack traces to '/data/anr/traces.txt'\par
06-24 18:21:18.439 D/dalvikvm(1760): GC_CONCURRENT freed 890K, 30% free 12146K/17108K, paused 17ms+15ms, total 252ms\par
06-24 18:21:18.669 D/dalvikvm(1760): GC_EXPLICIT freed 464K, 29% free 12316K/17108K, paused 5ms+10ms, total 98ms\par
06-24 18:21:18.919 D/dalvikvm(20695): GC_CONCURRENT freed 1944K, 41% free 4173K/7068K, paused 3ms+3ms, total 18ms\par
06-24 18:21:19.209 I/Process (1760): Sending signal. PID: 18063 SIG: 3\par
06-24 18:21:19.209 I/dalvikvm(18063): threadid=3: reacting to signal 3\par
06-24 18:21:19.239 I/dalvikvm(18063): Wrote stack traces to '/data/anr/traces.txt'\par
06-24 18:21:19.249 E/ActivityManager(1760): ANR in com.example.rama.beta (com.example.rama.beta/.MainActivity)\par
06-24 18:21:19.249 E/ActivityManager(1760): Reason: keyDispatchingTimedOut\par
06-24 18:21:19.249 E/ActivityManager(1760): Load: 1.09 / 1.36 / 1.55\par
06-24 18:21:19.249 E/ActivityManager(1760): CPU usage from 6504ms to 0ms ago:\par
06-24 18:21:19.249 E/ActivityManager(1760):   83% 20695/com.example.rama.beta: 74% user + 8.7% kernel / faults: 1365 minor\par
06-24 18:21:19.249 E/ActivityManager(1760):   2.9% 1760/system_server: 2.1% user + 0.7% kernel / faults: 154 minor\par
06-24 18:21:19.249 E/ActivityManager(1760):   0% 1168/nand10: 0% user + 0% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760):   1.3% 1238/surfaceflinger: 0% user + 1.3% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760):   0% 19796/flush-93:80: 0% user + 0% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760):   0.9% 1241/mediaserver: 0.3% user + 0.6% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760):   0.6% 12373/kworker/u:4: 0% user + 0.6% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760):   0.1% 1167/nftld: 0% user + 0.1% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760):   0.3% 10516/kworker/0:0: 0% user + 0.3% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760):   0% 785/hdmi proc: 0% user + 0% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760):   0.1% 11380/logcat: 0.1% user + 0% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760):   0.1% 13429/kworker/u:2: 0% user + 0.1% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760):   0.1% 18063/com.nolanlawson.logcat: 0.1% user + 0% kernel / faults: 5 minor\par
06-24 18:21:19.249 E/ActivityManager(1760):  +0% 20829/migration/1: 0% user + 0% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760):  +0% 20830/kworker/1:0: 0% user + 0% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760):  +0% 20831/ksoftirqd/1: 0% user + 0% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760):  +0% 20832/kworker/1:1: 0% user + 0% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760): 0.-4% TOTAL: 0.-5% user + 0.-1% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760): CPU usage from 763ms to 1283ms later:\par
06-24 18:21:19.249 E/ActivityManager(1760):   101% 20695/com.example.rama.beta: 98% user + 3.8% kernel / faults: 38 minor\par
06-24 18:21:19.249 E/ActivityManager(1760):     98% 20695/mple.rama.beta: 94% user + 3.8% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760):     3.8% 20698/GC: 3.8% user + 0% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760):   3.8% 1760/system_server: 0% user + 3.8% kernel / faults: 1 minor\par
06-24 18:21:19.249 E/ActivityManager(1760):     3.8% 1924/InputDispatcher: 0% user + 3.8% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760):     1.9% 1781/ActivityManager: 0% user + 1.9% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760):   1.4% 18063/com.nolanlawson.logcat: 1.4% user + 0% kernel / faults: 1 minor\par
06-24 18:21:19.249 E/ActivityManager(1760):     1.4% 18063/anlawson.logcat: 1.4% user + 0% kernel\par
06-24 18:21:19.249 E/ActivityManager(1760): 54% TOTAL: 48% user + 5.7% kernel\par
06-24 18:21:19.259 W/ActivityManager(1760):   Force finishing activity com.example.rama.beta/.MainActivity\par
06-24 18:21:19.309 I/ActivityManager(1760): Killing ProcessRecord\{41ac6640 20695:com.example.rama.beta/u0a10046\}: user's request\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/InputDispatcher(1760): Dropping event because there is no touched window.\par
06-24 18:21:19.329 I/qtaguid (1760): Failed write_ctrl(s 0 10046) res=-1 errno=1\par
06-24 18:21:19.329 W/NetworkManagementSocketTagger(1760): setKernelCountSet(10046, 0) failed with errno -1\par
06-24 18:21:19.329 I/ActivityManager(1760): Process com.example.rama.beta (pid 20695) has died.\par
06-24 18:21:19.329 I/WindowState(1760): WIN DEATH: Window\{41b2f780 u0 com.example.rama.beta/com.example.rama.beta.MainActivity\}\par

as expected, you are doing your encryption and decryption on the main UI thread.

        @Override
        public void onClick(View v) {
            try {
                encrypt();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

This happens if your app becomes unresponsive for to long, you can read more about that on the official google android page.

You have to do them in an extra thread, for example use an AsyncTask for that. Use the doInBackground(...) { method of an async task to do your encryption/decryption and show a progress indicator in your app. Then in the onPostExecute(...) method you can update your UI (remove the progress indicator and continue what your app is doing).

There are various examples on how to use async tasks. like here , here or here are examples and information about background processes in android .

06-24 18:21:17.869 I/InputDispatcher(1760): Application is not responding: Window\\{41b2f780 u0 com.example.rama.beta/com.example.rama.beta.MainActivity\\}. It has been 5006.0ms since event, 5005.6ms since wait started. Reason: Waiting because the touched window has not finished processing the input events that were previously delivered to it.\\par

The encryption and decryption process that you are performing are getting executed on Main/UI thread, which can cause ANRs(App-Not-Responding) on certain devices.

I suggest you to move that task to new Thread or use AsyncTask such that the process does affect the UI thread and not crashing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM