无法将照片上传到服务器
Not Able To Upload Photo To Server
我无法将照片发送到版本 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)
Camera Utils Class 显示 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);
}
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.
这说明了一切。
Bitmap 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
这是一个不存在的 'path' 开头。
将您的代码更改为。
InputStream is = getContentResolver().openInputStream(tempUri);
Bitmap bmp= BitmapFactory.decodeStream(is,options);
我无法将照片发送到版本 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)
Camera Utils Class 显示 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);
}
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.
这说明了一切。
Bitmap 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
这是一个不存在的 'path' 开头。
将您的代码更改为。
InputStream is = getContentResolver().openInputStream(tempUri);
Bitmap bmp= BitmapFactory.decodeStream(is,options);