2017-12-20 9 views
-1

バージョン6.0.1以降のサーバーに写真を送信することはできません。以下のエラーが発生しました。Photo To Serverをアップロードすることができません

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) 

そのは、getBytesメソッドでエラーを示すカメラUtilsのクラスコードと内部メソッドに保存します。

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:ヌルオブジェクトリファレンスで仮想メソッド 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap $ Com pressFormat、int、java.io.OutputStream)'を呼び出そうとしました。

これはすべてを示します。

ビットマップbmp = BitmapFactory.decodeFile(tempUri.getPath()、options);

ビットマップはありません。 bmp==null

使用前に確認してください! に電話する前に。

/external_files/DCIM/Camera/JPEG_20171220_163728_946425952.jpg 

そもそも非既存の「パス」です。

しかし、あなたの本当の間違いは、あなたが間違ったパスを使用

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

です。

にコードを変更します。

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

をチェックしています。 –

+0

チェックすることはありません。代わりに、 'bmp == null'をチェックし、そうであればコードを続行しないでください。追加: 'if(bmp == null){トースト(...ビットマップがヌルです...); return;} '。 – greenapps

+0

bmpがnullの場合はどうすればいいですか? –

関連する問題