2017-12-20 9 views
-1

Ich kann kein Foto an den Server in der Version 6.0.1 und höher unter Versionen senden funktioniert gut. Nach unten gerichteter Fehler.Ich kann kein Foto auf den Server hochladen

12-20 16:37:42.888 31885-31885/com.testing E/BitmapFactory: Unable to 
decode stream: java.io.FileNotFoundException: 
/external_files/DCIM/Camera/JPEG_20171220_163728_946425952.jpg: open failed: 
ENOENT (No such file or directory) 
12-20 16:37:42.890 31885-31885/com.testing E/AndroidRuntime: FATAL 
EXCEPTION: main 
                   Process: 
com.testing, PID: 31885 

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean 
android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, 
int, java.io.OutputStream)' on a null object reference 
                    at 
com.testing.CameraUtils.getBytes(CameraUtils.java:107) 
                    at 
com.testing.CameraUtils.saveToInternal(CameraUtils.java:124) 

Kamera Utils Class Code, wo seine den Fehler mit getBytes Verfahren und Speichern der internen Methode zeigt:

public static String saveToInternal(Context context, Uri tempUri) 
{ 
    OutputStream out = null; 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = 10; 
    Bitmap bmp= BitmapFactory.decodeFile(tempUri.getPath(),options); 
    File destinationInternalImageFile = new File(getOutputInternalMediaFile(context, 1).getPath()); 
    byte[] bitmapdata=getBytes(bmp); 
    ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata); 
    try { 
     destinationInternalImageFile.createNewFile(); 
     out = new FileOutputStream(destinationInternalImageFile); 
     byte[] buf = new byte[1024]; 
     int len; 
     while ((len = bs.read(buf)) > 0) { 
      out.write(buf, 0, len); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    finally { 
     try { 
      if (bs != null) { 
       bs.close(); 
      } 
      if (out != null) { 
       bs.close(); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    Log.e(TAG, "File Size : " + (destinationInternalImageFile.length()/1024) + "KB"); 
    return destinationInternalImageFile.getPath(); 
} 

// von Bitmap konvertieren Bytedatenfeld

public static byte[] getBytes(Bitmap bitmap) { 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG,50, stream); 
    return stream.toByteArray(); 
} 

// konvertiert von Byte-Array zu Bitmap

public static Bitmap getImage(byte[] image) { 
    return BitmapFactory.decodeByteArray(image, 0, image.length); 
} 
+0

fügen Sie Ihren Code –

+0

eindeutig Fehler zeigen, dass kein Bild dort mit diesem Namen in der Datei oder das Verzeichnis ist. und null geht in deine Komprimierungsfunktion. –

+0

Mögliches Duplikat von [Was ist eine NullPointerException, und wie behebe ich sie?] (Https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-fix-it) –

Antwort

1

java.lang.NullPointerException: Versuch, die virtuelle Methode 'boolean android.graphics.Bitmap.compress (android.graphics.Bitmap $ Com pressFormat, int, java.io.OutputStream)' auf eine Nullobjekt-Referenz aufzurufen.

Dies sagt alles.

Bitmap bmp = BitmapFactory.decodeFile (tempUri.getPath(), Optionen);

Sie haben kein Bitmap! bmp==null.

Vor Gebrauch prüfen! Bevor Sie .compress() darauf anrufen.

Aber Ihr wirklicher Fehler ist

Bitmap bmp= BitmapFactory.decodeFile(tempUri.getPath(),options); 

Sie verwenden einen falschen Weg:

/external_files/DCIM/Camera/JPEG_20171220_163728_946425952.jpg 

, dass ein nicht vorhandener 'Pfad' ist zu beginnen.

Ändern Sie Ihren Code in.

InputStream is = getContentResolver().openInputStream(tempUri); 
Bitmap bmp= BitmapFactory.decodeStream(is,options); 
+0

überprüfe meinen Code jetzt. –

+0

Es gibt nichts zu überprüfen. Stattdessen sollten Sie nach 'bmp == null' suchen und nicht mit Ihrem Code fortfahren. Add: 'if (bmp == null) {Toast (... bitmap ist null ...); Rückkehr;} '. – greenapps

+0

was muss ich tun, wenn der BMP null ist. –