2017-06-15 196 views
0

我让用户选择背景,用户可以从我带应用程序的列表中的图库或图片中选择自己的图片。我将此图片保存到FileOutputStream。但是,我的应用程序变得非常慢,我正在使用以下功能;Android处理大图像

//When user picks from gallery 
public void save(Bitmap bitmapImage) { 
    bitmapImage = scaleBitmap(bitmapImage); 

    FileOutputStream fileOutputStream = null; 
    try { 
     fileOutputStream = new FileOutputStream(createFile()); 
     bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      if (fileOutputStream != null) { 
       fileOutputStream.close(); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

//When user picks from list and I load from drawables 
public void save(int resId) { 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = IMAGE_IN_SAMPLE_SIZE; 
    options.inScaled = false; 
    options.inJustDecodeBounds = false; 

    Bitmap bitmapImage = BitmapFactory.decodeResource(context.getResources(), resId, options); 

    FileOutputStream fileOutputStream = null; 
    try { 
     fileOutputStream = new FileOutputStream(createFile()); 
     bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      if (fileOutputStream != null) { 
       fileOutputStream.close(); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

如何让这些功能更快?

我也得到这个消息:

06-15 12:02:58.884 22320-22320/com I/dalvikvm-heap: Grow heap (frag case) to 15.172MB for 2073616-byte allocation 
06-15 12:03:00.716 22320-22320/com I/dalvikvm-heap: Grow heap (frag case) to 15.149MB for 2073616-byte allocation 
06-15 12:03:03.129 22320-22320/com I/Choreographer: Skipped 145 frames! The application may be doing too much work on its main thread. 
+1

考虑使用滑翔 – EpicPandaForce

+0

的[应用程序可以做它的主线程的工作太多了]可能的复制(https://stackoverflow.com/questions/14678593/the-application-可能会做太多的工作在其主线程) – user3802077

+0

我想使用滑翔,但我怎样才能保存一个图像滑动到文件? – tomhogenkamp

回答

0

ü不应该做在主线程必须IO的工作,也许你可以做到这一点诠释一个主题,并通过hanlder更新UI。

new Thread(new Runnable() { 
     @Override 
     public void run() { 
      // save your picture 
      handler.sendEmptyMessage(0); 
     } 
    }).start(); 

而且最好使用rxjava和rxAndroid。

希望它可以帮助你