本文介绍了java.lang.OutOfMemoryError而在Android的使用Base64的拍照摄像头,从的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的Base64拍照相机从,以及从SD卡采摘的形象,而在ImageView的我得到的OutOfMemoryError保存图像。下面是错误日志。请帮助。
错误日志
15 11-20:58:50.098:E / AndroidRuntime(4790):致命异常:主要
11-20 15:58:50.098:E / AndroidRuntime(4790):java.lang.OutOfMemoryError
11-20 15:58:50.098:E / AndroidRuntime(4790):在android.graphics.BitmapFactory.nativeDe codeStream(本机方法)
11-20 15:58:50.098:E / AndroidRuntime(4790):在android.graphics.BitmapFactory.de codeStream(BitmapFactory.java:500)
11-20 15:58:50.098:E / AndroidRuntime(4790):在android.graphics.BitmapFactory.de codeFILE(BitmapFactory.java:306)
11-20 15:58:50.098:E / AndroidRuntime(4790):在android.graphics.BitmapFactory.de codeFILE(BitmapFactory.java:331)
11-20 15:58:50.098:E / AndroidRuntime(4790):在com.Infoicon.KidsBSafeNew.MainActivity.onActivityResult(MainActivity.java:606)
11-20 15:58:50.098:E / AndroidRuntime(4790):在android.app.Activity.dispatchActivityResult(Activity.java:4649)
11-20 15:58:50.098:E / AndroidRuntime(4790):在android.app.ActivityThread.deliverResults(ActivityThread.java:2996)
11-20 15:58:50.098:E / AndroidRuntime(4790):在android.app.ActivityThread.handleSendResult(ActivityThread.java:3043)
11-20 15:58:50.098:E / AndroidRuntime(4790):在android.app.ActivityThread.access $ 1100(ActivityThread.java:127)
11-20 15:58:50.098:E / AndroidRuntime(4790):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1188)
11-20 15:58:50.098:E / AndroidRuntime(4790):在android.os.Handler.dispatchMessage(Handler.java:99)
11-20 15:58:50.098:E / AndroidRuntime(4790):在android.os.Looper.loop(Looper.java:137)
11-20 15:58:50.098:E / AndroidRuntime(4790):在android.app.ActivityThread.main(ActivityThread.java:4441)
11-20 15:58:50.098:E / AndroidRuntime(4790):在java.lang.reflect.Method.invokeNative(本机方法)
11-20 15:58:50.098:E / AndroidRuntime(4790):在java.lang.reflect.Method.invoke(Method.java:511)
11-20 15:58:50.098:E / AndroidRuntime(4790):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
11-20 15:58:50.098:E / AndroidRuntime(4790):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-20 15:58:50.098:E / AndroidRuntime(4790):在dalvik.system.NativeStart.main(本机方法)
11-20 15:58:50.118:E /系统(4790):java.lang.IllegalStateException:不必数据库锁定!
11-20 15:58:50.118:E /系统(4790):在android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2090)
11-20 15:58:50.118:E /系统(4790):在android.database.sqlite.SQLiteDatabase $ 1.entryRemoved(SQLiteDatabase.java:2182)
11-20 15:58:50.118:E /系统(4790):在android.database.sqlite.SQLiteDatabase $ 1.entryRemoved(SQLiteDatabase.java:2178)
11-20 15:58:50.118:E /系统(4790):在android.util.LruCache.trimToSize(LruCache.java:197)
11-20 15:58:50.118:E /系统(4790):在android.util.LruCache.evictAll(LruCache.java:285)
11-20 15:58:50.118:E /系统(4790):在android.database.sqlite.SQLiteDatabase.deallocCachedSqlStatements(SQLiteDatabase.java:2143)
11-20 15:58:50.118:E /系统(4790):在android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1126)
11-20 15:58:50.118:E /系统(4790):在android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:1914)
11-20 15:58:50.118:E /系统(4790):在java.lang.Daemons $ FinalizerDaemon.doFinalize(Daemons.java:182)
11-20 15:58:50.118:E /系统(4790):在java.lang.Daemons $ FinalizerDaemon.run(Daemons.java:168)
11-20 15:58:50.118:E /系统(4790):在java.lang.Thread.run(Thread.java:856)
解决方案
使用这个..
在的onActivityResult ..
@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图意图)
{
super.onActivityResult(要求code,结果code,意向);
如果(要求code == 1)
{
文件出=新的文件(Environment.getExternalStorageDirectory(),newImage.jpg); 如果(!out.exists())
{
Log.v(日志,找不到文件);
Toast.makeText(getBaseContext(), 错误同时捕捉图像,Toast.LENGTH_LONG) 。显示(); 返回; } Log.v(日志,文件+ out.getAbsolutePath());
文件f =新的文件(out.getAbsolutePath()); 尝试{ ExifInterface EXIF =新ExifInterface(out.getAbsolutePath());
方向= exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,1);
//Toast.makeText(getApplicationContext(),+方向,1).show();
Log.v(日志,ORT是+方向); }赶上(IOException异常E)
{
e.printStackTrace();
}
位图照片=去codeFILE(F,400,400); ////这种使用的base64后convet形象,因为这个功能会非常正常size..so做你的文件大小,内存不足的错误未发生..
}
}
这是德code函数。
公共静态位图德codeFILE(文件f,诠释宽度,诠释HIGHT){
尝试{
//德code图像尺寸
BitmapFactory.Options O =新BitmapFactory.Options();
o.inJustDe codeBounds = TRUE;
BitmapFactory.de codeStream(新的FileInputStream(F),空,O); //我们希望新的大小缩放到
最终诠释REQUIRED_WIDTH =宽度;
最终诠释REQUIRED_HIGHT = HIGHT;
//找到正确的比例值。它应该是2的幂。
int标= 1;
而(o.outWidth /规模/ 2 - = REQUIRED_WIDTH和放大器;&安培; o.outHeight /规模/ 2 - = REQUIRED_HIGHT)
规模* = 2; //德code。与inSampleSize
BitmapFactory.Options O2 =新BitmapFactory.Options();
o2.inSampleSize =规模;
返回BitmapFactory.de codeStream(新的FileInputStream(F),空,O2);
}赶上(FileNotFoundException异常五){}
返回null;
}
I am using Base64 for taking picture from camera as well as picking image from SDcard, while saving image in imageview I am getting OutOfMemoryError. Below is error log. Please Help.
ERROR LOG
11-20 15:58:50.098: E/AndroidRuntime(4790): FATAL EXCEPTION: main
11-20 15:58:50.098: E/AndroidRuntime(4790): java.lang.OutOfMemoryError
11-20 15:58:50.098: E/AndroidRuntime(4790): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
11-20 15:58:50.098: E/AndroidRuntime(4790): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500)
11-20 15:58:50.098: E/AndroidRuntime(4790): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:306)
11-20 15:58:50.098: E/AndroidRuntime(4790): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:331)
11-20 15:58:50.098: E/AndroidRuntime(4790): at com.Infoicon.KidsBSafeNew.MainActivity.onActivityResult(MainActivity.java:606)
11-20 15:58:50.098: E/AndroidRuntime(4790): at android.app.Activity.dispatchActivityResult(Activity.java:4649)
11-20 15:58:50.098: E/AndroidRuntime(4790): at android.app.ActivityThread.deliverResults(ActivityThread.java:2996)
11-20 15:58:50.098: E/AndroidRuntime(4790): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3043)
11-20 15:58:50.098: E/AndroidRuntime(4790): at android.app.ActivityThread.access$1100(ActivityThread.java:127)
11-20 15:58:50.098: E/AndroidRuntime(4790): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1188)
11-20 15:58:50.098: E/AndroidRuntime(4790): at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 15:58:50.098: E/AndroidRuntime(4790): at android.os.Looper.loop(Looper.java:137)
11-20 15:58:50.098: E/AndroidRuntime(4790): at android.app.ActivityThread.main(ActivityThread.java:4441)
11-20 15:58:50.098: E/AndroidRuntime(4790): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 15:58:50.098: E/AndroidRuntime(4790): at java.lang.reflect.Method.invoke(Method.java:511)
11-20 15:58:50.098: E/AndroidRuntime(4790): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-20 15:58:50.098: E/AndroidRuntime(4790): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-20 15:58:50.098: E/AndroidRuntime(4790): at dalvik.system.NativeStart.main(Native Method)
11-20 15:58:50.118: E/System(4790): java.lang.IllegalStateException: Don't have database lock!
11-20 15:58:50.118: E/System(4790): at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2090)
11-20 15:58:50.118: E/System(4790): at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2182)
11-20 15:58:50.118: E/System(4790): at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2178)
11-20 15:58:50.118: E/System(4790): at android.util.LruCache.trimToSize(LruCache.java:197)
11-20 15:58:50.118: E/System(4790): at android.util.LruCache.evictAll(LruCache.java:285)
11-20 15:58:50.118: E/System(4790): at android.database.sqlite.SQLiteDatabase.deallocCachedSqlStatements(SQLiteDatabase.java:2143)
11-20 15:58:50.118: E/System(4790): at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1126)
11-20 15:58:50.118: E/System(4790): at android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:1914)
11-20 15:58:50.118: E/System(4790): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
11-20 15:58:50.118: E/System(4790): at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
11-20 15:58:50.118: E/System(4790): at java.lang.Thread.run(Thread.java:856)
解决方案
use this..
in onActivityresult..
@Override
protected void onActivityResult(int requestCode, int resultcode, Intent intent)
{
super.onActivityResult(requestCode, resultcode, intent);
if(requestCode == 1)
{
File out = new File(Environment.getExternalStorageDirectory(), "newImage.jpg");
if(!out.exists())
{
Log.v("log", "file not found");
Toast.makeText(getBaseContext(),
"Error while capturing image", Toast.LENGTH_LONG)
.show();
return;
}
Log.v("log", "file "+out.getAbsolutePath());
File f = new File(out.getAbsolutePath());
try {
ExifInterface exif = new ExifInterface(out.getAbsolutePath());
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
//Toast.makeText(getApplicationContext(), ""+orientation, 1).show();
Log.v("log", "ort is "+orientation);
} catch (IOException e)
{
e.printStackTrace();
}
Bitmap photo =decodeFile(f,400,400);////after this use base64 to convet image because this function will do your file size in very normal size..so, outofMemory error not occured..
}
}
and this is decode function..
public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//The new size we want to scale to
final int REQUIRED_WIDTH=WIDTH;
final int REQUIRED_HIGHT=HIGHT;
//Find the correct scale value. It should be the power of 2.
int scale=1;
while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
scale*=2;
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
这篇关于java.lang.OutOfMemoryError而在Android的使用Base64的拍照摄像头,从的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!