0

我正在使用 Base64 从相机拍照以及从 SD 卡中挑选图像,同时在 imageview 中保存图像时出现 OutOfMemoryError。下面是错误日志。请帮忙。

错误日志

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)
4

1 回答 1

1

用这个..

在 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..


 }
}

这是解码功能..

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;
 }
于 2012-11-20T10:40:58.140 回答