【问题标题】:Not Able To Upload Photo To Server无法将照片上传到服务器
【发布时间】:2017-12-20 11:27:40
【问题描述】:

我无法将照片发送到版本 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);
}

【问题讨论】:

  • 粘贴你的代码
  • 显然错误表明您的文件或目录中没有具有该名称的图像。并且 null 将进入您的压缩功能。
  • Saad 我正在单击图像,它在设备的内部存储中,同时将其上传到服务器崩溃。
  • 您是否要求Version 6.0.1 and above@ChaudharyAmar 的运行时权限

标签: android android-camera android-bitmap


【解决方案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

使用前检查!在您致电.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);

【讨论】:

  • 没有什么要检查的。相反,您应该检查“bmp==null”,如果是,则不要继续使用您的代码。地址:if (bmp==null){Toast ( ... bitmap is null ...); return;}.
  • 如果 bmp 为空,我必须做什么。
猜你喜欢
  • 2020-04-04
  • 1970-01-01
  • 1970-01-01
  • 2012-10-10
  • 1970-01-01
  • 2012-06-23
  • 2017-12-19
  • 1970-01-01
  • 2011-04-11
相关资源
最近更新 更多