我无法将版本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类代码,它使用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 :(得分:1)
java.lang.NullPointerException:尝试调用虚方法' boolean android.graphics.Bitmap.compress(android.graphics.Bitmap $ Com pressFormat,int,java.io.OutputStream)'在null对象引用上。
这说明了一切。
位图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);