Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法解析符号';ByteArrayBuffer';_Java_Android - Fatal编程技术网

Java 无法解析符号';ByteArrayBuffer';

Java 无法解析符号';ByteArrayBuffer';,java,android,Java,Android,因此,在我的应用程序中,有一个带有图像的网络视图,我想在点击按钮时保存它。 为此,我使用以下代码: import org.apache.http.util.ByteArrayBuffer; public void DownloadFromUrl(String fileName) { //this is the downloader method try { URL url = new URL(wv2.getUrl()); //you can write he

因此,在我的应用程序中,有一个带有图像的网络视图,我想在点击按钮时保存它。 为此,我使用以下代码:

import org.apache.http.util.ByteArrayBuffer;

 public void DownloadFromUrl(String fileName) {  //this is the downloader method
    try {
            URL url = new URL(wv2.getUrl()); //you can write here any link
            File file = new File(fileName);
            Canvas canvas = new Canvas();
            wv2.draw(canvas );

            long startTime = System.currentTimeMillis();
            Log.d("ImageManager", "download begining");
            Log.d("ImageManager", "download url:" + url);
            Log.d("ImageManager", "downloaded file name:" + fileName);
            URLConnection ucon = url.openConnection();

            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);

            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;
            while ((current = bis.read()) != -1) {
                    baf.append((byte) current);
            }

            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baf.toByteArray());
            fos.close();
            Log.d("ImageManager", "download ready in"
                            + ((System.currentTimeMillis() - startTime) / 1000)
                            + " sec");

    } catch (IOException e) {
            Log.d("ImageManager", "Error: " + e);
    }
}


其中
wv2
是WebView。奇怪的是,我不能再这样做了,并且在我的导入语句中出现了一个错误,
无法解析符号“ByteArrayBuffer”
,在最新的SDK中删除了org.apache.http,这就是导入失败的原因

注意:在此代码中,您不需要ByteArrayBuffer,您可以使用ByteBuffer,或者从
bis
写入
fos
,如下所示:

            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            FileOutputStream fos = new FileOutputStream(file);

            int current = 0;
            while ((current = bis.read()) != -1) {
                    fos.write(current);
            }

            fos.close();

是的,我看到了这个问题。但是WebView包含的图像不是链接中的站点或图像,而是从相机中获取的图片。将
ByteArrayBuffer
替换为
ByteBuffer
仍然会给我留下同样的错误。那么,我应该如何替换上面的
public void
的内部呢?如果您删除ByteArrayBuffer导入(通过ByteBuffer导入将其替换),我将非常感谢您提供的任何替代或教程。是的,使用
import org.apache.http.util.ByteBuffer
ByteBuffer不是org.apache.http的一部分(这就是重点)。尝试导入java.nio.ByteBuffer您还可以将
bis.read()
的结果直接写入
fos