2016-03-02 17 views
7

Sto cercando di caricare uno screenshot dal mio Environment.getExternalStorageDirectory() e provare a convertirlo in bitmapAndroid PNG bitmap --- SkImageDecoder :: fabbrica restituito 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 :: fabbrica restituito nulla

Qui è la mia funzione che chiama 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(); 
} 

Qualcuno sa come risolvere il problema? Forse perché il mio png è troppo grande (1280x720)? Ho provato anche questa soluzione con lo stesso risultato: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Edit: Qui è il ceppo

03-02 11: 56: 19,806 11.581-11.716/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/emulato/0/Immagini/Screenshot 03-02 11: 56: 19.806 11581-11716/com.example.chilred_pc.myapplication D/file:/memoria/emulato/0/Immagini/Schermate/Screenshot_2016-03-01-16 -38-08.png 03-02 11: 56: 19.806 11581-11716/com.example.chilred_pc.myapplication D/file Dimensione: 35061 03-02 11: 56: 19.807 11581-11716/com.example.chilred_pc.myapplication D/skia: - - SkImageDecoder :: fabbrica restituito nullo 03-02 11: 56: 19,808 11.581-11.716/com.example.chilred_pc.myapplication D/skia: --- Decoder-> decodifica restituito falso

+0

Assicurarsi di avere dare il permesso per WRITE_EXTERNAL_STORAGE e WRITE_EXTERNAL_STORAGE in AndroidManifest.xml. Si prega di inviare il registro degli errori. – pRaNaY

+0

cosa ottieni quando esegui 'int fileSize = file.length()'? qual è il valore di fileSize – Bhargav

+0

Ho già scritto in androidmanifest.xml. – Maddin

risposta

1

Penso che la soluzione al problema sia che lo screenshot richiede alcuni secondi per creare un'immagine. Così ho provato a fermare il sistema per alcuni secondi e ora funziona.

> SystemClock.sleep(3000); 

Ma alla fine ho usato un altro metodo, che viene qui mostrato: Detect only screenshot with FileObserver Android

0

Invece di proposta valore manuale a options.inSampleSize = 4, calcolare InSampleSize come di seguito:

... 
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; 
} 

Calcolare InSampleSize dato sullo stesso like cui si fa riferimento.

+0

Ho provato questo, ma non cambia nulla – Maddin

Problemi correlati