我从 javaCameraView 加载位图中的图像。这是发送到异步任务以便将其发送到 php 文件。在第一秒没有问题。在这段时间之后,我遇到了线程问题,但我没有找到解决方案。这是 LogCat :
07-25 16:36:29.492: E/AndroidRuntime(17507): FATAL EXCEPTION: AsyncTask #5
07-25 16:36:29.492: E/AndroidRuntime(17507): java.lang.RuntimeException: An error occured while executing doInBackground()
07-25 16:36:29.492: E/AndroidRuntime(17507): at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-25 16:36:29.492: E/AndroidRuntime(17507): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-25 16:36:29.492: E/AndroidRuntime(17507): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-25 16:36:29.492: E/AndroidRuntime(17507): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-25 16:36:29.492: E/AndroidRuntime(17507): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-25 16:36:29.492: E/AndroidRuntime(17507): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-25 16:36:29.492: E/AndroidRuntime(17507): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-25 16:36:29.492: E/AndroidRuntime(17507): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-25 16:36:29.492: E/AndroidRuntime(17507): at java.lang.Thread.run(Thread.java:856)
07-25 16:36:29.492: E/AndroidRuntime(17507): Caused by: java.lang.OutOfMemoryError
07-25 16:36:29.492: E/AndroidRuntime(17507): at java.lang.String.<init>(String.java:375)
07-25 16:36:29.492: E/AndroidRuntime(17507): at java.lang.String.<init>(String.java:238)
07-25 16:36:29.492: E/AndroidRuntime(17507): at android.util.Base64.encodeToString(Base64.java:456)
07-25 16:36:29.492: E/AndroidRuntime(17507): at com.example.objectDetect.SendData.getStringFromBitmap(SendData.java:186)
07-25 16:36:29.492: E/AndroidRuntime(17507): at com.example.objectDetect.SendData.doInBackground(SendData.java:63)
07-25 16:36:29.492: E/AndroidRuntime(17507): at com.example.objectDetect.SendData.doInBackground(SendData.java:1)
07-25 16:36:29.492: E/AndroidRuntime(17507): at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-25 16:36:29.492: E/AndroidRuntime(17507): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-25 16:36:29.492: E/AndroidRuntime(17507): ... 5 more
我认为问题出在这个函数上,但我不知道为什么:
private String getStringFromBitmap(Bitmap bitmapPicture)
{
if(!bitmapPicture.isRecycled())
{
final int COMPRESSION_QUALITY = 100;
String encodedImage;
ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
bitmapPicture.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY, byteArrayBitmapStream);
byte[] b = byteArrayBitmapStream.toByteArray();
encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
return encodedImage;
}
else
{
return null;
}
}
经过大量测试,我有一个不同的 logcat,但它总是内存问题。我在运行函数的线程中创建了一个位图。
在 logcat 下方:
07-26 09:42:50.491: E/AndroidRuntime(31291): FATAL EXCEPTION: Thread-27487
07-26 09:42:50.491: E/AndroidRuntime(31291): java.lang.OutOfMemoryError
07-26 09:42:50.491: E/AndroidRuntime(31291): at android.graphics.Bitmap.nativeCreate(Native Method)
07-26 09:42:50.491: E/AndroidRuntime(31291): at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
07-26 09:42:50.491: E/AndroidRuntime(31291): at android.graphics.Bitmap.createBitmap(Bitmap.java:620)
07-26 09:42:50.491: E/AndroidRuntime(31291): at com.example.objectDetection.objectDetectionView$1.run(objectDetectionView.java:197)
预先感谢您的帮助。