gpt4 book ai didi

java - Android 应用程序在处理大文件时在某些设备上崩溃

转载 作者:太空狗 更新时间:2023-10-29 13:52:23 24 4
gpt4 key购买 nike

我正在 Android 上开发一个处理视频文件的加密和解密应用程序。我的应用程序在某些设备上运行完美,但在某些设备上崩溃,同时处理相同的文件大小。我的 InputStream 和 OutputStream 代码如下

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();

}
}

我使用的两个 Android 设备之间的显着区别是 RAM,该应用程序在具有 2gb RAM 的设备上处理 50mb 视频时完美运行,但在具有 1gb RAM 的设备上崩溃。我不确定这是 RAM 问题还是其他问题。感谢任何输入。

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

最佳答案

正如预期的那样,您正在主 UI 线程上进行加密和解密。

        @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();
}
}

如果您的应用长时间无响应,就会发生这种情况,您可以在官方网站上阅读更多相关信息 google android page.

你必须在一个额外的线程中完成它们,例如使用 AsyncTask为了那个原因。使用异步任务的 doInBackground(...) { 方法进行加密/解密并在您的应用中显示进度指示器。然后在 onPostExecute(...) 方法中,您可以更新您的 UI(删除进度指示器并继续您的应用正在执行的操作)。

有各种关于如何使用异步任务的示例。喜欢here , herehere are examples and information about background processes in android .

关于java - Android 应用程序在处理大文件时在某些设备上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44739109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com