2017-06-15 229 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。

希望它可以幫助你