Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.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 无法从android中的服务器加载图像_Java_Android - Fatal编程技术网

Java 无法从android中的服务器加载图像

Java 无法从android中的服务器加载图像,java,android,Java,Android,在这里,我试图从服务器下载一个图像。但它总是抛出异常。有谁能告诉我为什么会这样,正确的方法是什么 public static String getBitmap(String url) throws IOException { InputStream is = (InputStream) new URL(url).getContent(); Bitmap bmp= BitmapFactory.decodeStream(is); ByteArray

在这里,我试图从服务器下载一个图像。但它总是抛出异常。有谁能告诉我为什么会这样,正确的方法是什么

 public static String getBitmap(String url) throws IOException {

        InputStream is = (InputStream) new URL(url).getContent();
        Bitmap bmp= BitmapFactory.decodeStream(is);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] b=stream.toByteArray();
        String encoded = Base64.encodeToString(b, Base64.DEFAULT);


        is.close();
        return encoded;


}

请尝试此功能获取位图并下载位图

Bitmap bitmap = getBitmapfromUrl(imageurl);
imageview.setImageBitmap(bitmap);

SaveImage(bitmap);

public Bitmap getBitmapfromUrl(String imageUrl) {
    try {
        URL url = new URL(imageUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap bitmap = BitmapFactory.decodeStream(input);
        return bitmap;

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;

    }
}

private void SaveImage(Bitmap finalBitmap) {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-" + n;
    File file = new File(myDir, fname);
    if (file.exists()) file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
并在清单文件中添加此权限


尝试使用
Glide
Picasso
图像处理库

  • 这是Glide演示
  • 并在gradle中添加此依赖项

    compile 'com.github.bumptech.glide:glide:3.7.0'
    
    您还可以在加载图像时设置占位符。它类似于html中的
    alt
    属性

    Glide.with(this).load(YourImageURL)
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                        .placeholder(R.drawable.backimage)
                        .into(Imageview);
    
  • 这是毕加索的演示
  • 并添加此依赖项:

    compile 'com.squareup.picasso:picasso:2.5.2'
    

    这些库还提供了
    缓存功能
    ,因此您无需再次加载。

    从活动中调用此函数并获取inputStream。获取inputStream(位图位图=位图.decodeStream(inputStream))后


    粘贴您获得的异常您获得的异常是什么??使用您可以发布异常的堆栈跟踪吗?使用
    Picasso
    Glide
    库加载服务器映像是更好的选择。@rashmi ranjan请检查我的答案。在url中获取图像并轻松保存图像
    ,但它总是引发异常。有人能告诉我为什么会发生这种情况,正确的方法是什么吗?
    您没有回答这些问题。你的回答不是回答,而是建议。
       Picasso.with(context).load(url).placeholder(R.drawable.user_placeholder)
            .error(R.drawable.user_placeholder_error)
            .into(imageView);
    
    compile 'com.squareup.picasso:picasso:2.5.2'
    
    private InputStream OpenHttpConnection(String urlString) throws IOException
        {
        InputStream in = null;
        int response = -1;
    
        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();
    
        if (!(conn instanceof HttpURLConnection)) throw new IOException("Not an HTTP connection");
    
        try{
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();
    
        response = httpConn.getResponseCode();
        if (response == HttpURLConnection.HTTP_OK) {
        in = httpConn.getInputStream();
        }
        }
        catch (Exception ex)
        {
        throw new IOException("Error connecting");
        }
        return in;
        }