2016-03-02 76 views
7

我想從我的Environment.getExternalStorageDirectory加載一個屏幕截圖() ,並嘗試將其轉換爲位圖的Android PNG爲位圖--- SkImageDecoder ::廠返回null

public void onPictureTaken(String path) throws IOException { 

    String photoPath = filepath + "/" + path;; //UPDATE WITH YOUR OWN JPG FILE 

    File directory = new File (filepath); 
    File file = new File(directory, path); 

    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(photoPath, options); 

    // Calculate inSampleSize 
    options.inSampleSize = 4; 
    options.inJustDecodeBounds = false; 
    BitmapFactory.decodeFile(photoPath, options); 

} 

- - SkImageDecoder ::廠返回null

這裏是我的函數,它們調用onPictureTaken:

private void observeSceenshot(){ 
    filepath = Environment.getExternalStorageDirectory() 
      + File.separator + Environment.DIRECTORY_PICTURES 
      + File.separator + "Screenshots"; 
    Log.d(TAG, filepath); 

    FileObserver fileObserver = new FileObserver(filepath, FileObserver.CREATE) { 
     @Override 
     public void onEvent(int event, String path) { 
      Log.d(TAG, event + " " + path); 
      try { 
       onPictureTaken(path); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    }; 

    fileObserver.startWatching(); 
} 

有誰知道如何解決這個問題?也許是因爲我的PNG是大(1280x720)? 我還試圖此溶液具有相同的結果: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

編輯:這是日誌

十一月3日至2日:56:19.806 11581-11716/com.example.chilred_pc.myapplication d/DBG_com.example.chilred_pc.myapplication.ObserveScreenshots:256 Screenshot_2016-03-02-11-56-19.png 03-02 11:56:19.806 11581-11716/com.example.chilred_pc.myapplication D/directory:/ storage/emulated/0 /圖片/屏幕截圖 03-02 11:56:19.806 11581-11716/com.example.chilred_pc.myapplication D/file:/ storage/emulated/0/Pictures/Screenshots/Screenshot_2016-03-01-16 -38-08.png 03-02 11:56:19.806 11581-11716/com.example.chilred_pc.myapplication D/fileSize:35061 03-02 11:56:19.807 11581-11716/com.example.chilred_pc.myapplication D/skia: - - SkImageDecoder ::廠返回null 11月三日日至2日:56:19.808 11581-11716/com.example.chilred_pc.myapplication d/Skia的:--- decoder->解碼返回false

+0

請確保您有給權限在AndroidManifest.xml WRITE_EXTERNAL_STORAGE和WRITE_EXTERNAL_STORAGE。 請發佈您的錯誤日誌。 – pRaNaY

+0

當你做'int fileSize = file.length()'時你會得到什麼?什麼是fileSize – Bhargav

+0

的價值我已經寫在androidmanifest.xml中已經。 – Maddin

回答

1

我認爲問題的解決方案是截圖需要幾秒鐘來創建一個圖像。所以我試圖阻止系統幾秒鐘,現在它正在工作。

> SystemClock.sleep(3000); 

但最終我用另一種方法,這裏所示: Detect only screenshot with FileObserver Android

0

而不是給出手動值options.inSampleSize = 4,計算InSampleSize象下面這樣:

... 
int reqWidth=100; // give your requested width 
int reqHeight=100;// give your requested height 
options.inSampleSize = calculateSampleSize(options,reqWidth,reqHeight); 
... 

public static int calculateSampleSize(BitmapFactory.Options options, 
     int reqWidth, int reqHeight) { 

    final int width = options.outWidth; 
    final int height = options.outHeight; 
    int inSampleSize = 1; 

    if (width > reqWidth || height > reqHeight) { 
     if (width > height) { 
      inSampleSize = Math.round((float) height/(float) reqHeight); 
     } else { 
      inSampleSize = Math.round((float) width/(float) reqWidth); 
     } 
    } 
    return inSampleSize; 
} 

計算InSampleSize上你是指同一like給出。

+0

我試過這個,但它沒有改變 – Maddin