2017-12-20 4 views
-1

버전 6.0.1 이상 버전의 서버로 사진을 보낼 수 없습니다. 제대로 작동합니다. 아래 오류가 있습니다.사진을 서버에 업로드 할 수 없음

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) 

카메라의 Utils 클래스 코드 그것 getBytes 방법에 오류를 나타내는 내부 방법에 저장 여기서 바이트 어레이로 비트 맵으로 변환 //

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(); 
} 

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

// 변환 바이트 배열에서 비트 맵까지

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

코드 –

+0

분명히 오류는 파일 또는 디렉토리에 해당 이름의 이미지가 없음을 나타냅니다. 널 (NULL)이 압축 함수로 들어갑니다. –

+0

[NullPointerException이란 무엇이며 어떻게 수정합니까?] (https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it)) –

답변

1

java.lang.NullPointerException : null 객체 참조에서 가상 메소드 'boolean android.graphics.Bitmap.compress (android.graphics.Bitmap $ Com pressFormat, int, java.io.OutputStream)'을 호출하려고 시도했습니다.

이렇게 알려줍니다.

비트 맵 bmp = BitmapFactory.decodeFile (tempUri.getPath(), options);

비트 맵이 없습니다. bmp==null.

사용하기 전에 확인하십시오! 전에 .compress()에 전화하십시오.

그러나 당신의 진짜 실수는

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

당신은 잘못된 경로 사용할 수 있습니다 :로 시작하는이 아닌 기존의 '경로'입니다

/external_files/DCIM/Camera/JPEG_20171220_163728_946425952.jpg 

합니다.

코드를로 변경하십시오.

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

지금 내 코드를 확인하십시오. –

+0

확인할 사항은 없습니다. 대신 'bmp == null'을 확인하고 코드가있는 경우 계속하지 않아야합니다. 추가 :'if (bmp == null) {Toast (... bitmap이 null 인 경우 ...); return;}'. – greenapps

+0

bmp가 null 인 경우 수행해야하는 작업. –

관련 문제