从FTP服务器获取图片,然后转换为Android中的位图 [英] get picture from FTP server then convert to Bitmap in Android

查看:170
本文介绍了从FTP服务器获取图片,然后转换为Android中的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于这个线程的答案, a>,我们可以从FTP服务器上传和下载:

  //上传
尝试
{
FTPClient con = new FTPClient();
con.connect(192.168.2.57); (con.login(Administrator,KUjWbk))
{
con.enterLocalPassiveMode();

if //重要!
con.setFileType(FTP.BINARY_FILE_TYPE);
String data =/sdcard/vivekm4a.m4a;

FileInputStream in = new FileInputStream(new File(data));
布尔结果= con.storeFile(/ vivekm4a.m4a,in);
in.close();
if(result)Log.v(upload result,succeeded);
con.logout();
con.disconnect();


catch(Exception e)
{
e.printStackTrace();


//下载
尝试
{
FTPClient con = new FTPClient();
con.connect(192.168.2.57); (con.login(Administrator,KUjWbk))
{
con.enterLocalPassiveMode();

if //重要!
con.setFileType(FTP.BINARY_FILE_TYPE);
String data =/sdcard/vivekm4a.m4a;

OutputStream out = new FileOutputStream(new File(data));
布尔结果= con.retrieveFile(vivekm4a.m4a,out);
out.close();
if(result)Log.v(下载结果,成功);
con.logout();
con.disconnect();

$ b $ catch(Exception e)
{
Log.v(下载结果,失败);
e.printStackTrace();





$ b

在下载部分,是否有可能检索文件(假定文件总是jpg),并将其转换为位图,而不必在我的应用程序文件夹中创建文件?解析方案

您可以使用 picaso 库以获取从FTP服务器下载的图像并获取其位图。

毕加索图书馆有特殊的方法目标,这将直接为您提供为你下载的位图代码。

希望它能帮助你。
eg

  Target target = new Target(){
@Override
public void onBitmapLoaded (位图位图,Picasso.LoadedFrom from){
//你可以在这里使用位图对象
}
@Override
public void onBitmapFailed(){
} $ (this).load(url)。into(target); b $ b}
Picasso.with(this).load(url)。


Based on an answer on this thread, we can upload and download from FTP server:

//UPLOAD
try
{
    FTPClient con = new FTPClient();
    con.connect("192.168.2.57");

    if (con.login("Administrator", "KUjWbk"))
    {
        con.enterLocalPassiveMode(); // important!
        con.setFileType(FTP.BINARY_FILE_TYPE);
        String data = "/sdcard/vivekm4a.m4a";

        FileInputStream in = new FileInputStream(new File(data));
        boolean result = con.storeFile("/vivekm4a.m4a", in);
        in.close();
        if (result) Log.v("upload result", "succeeded");
            con.logout();
            con.disconnect();
        }
    }
catch (Exception e)
{
    e.printStackTrace();
}

//DOWNLOAD
try
{
    FTPClient con = new FTPClient();
    con.connect("192.168.2.57");

    if (con.login("Administrator", "KUjWbk"))
    {
        con.enterLocalPassiveMode(); // important!
        con.setFileType(FTP.BINARY_FILE_TYPE);
        String data = "/sdcard/vivekm4a.m4a";

        OutputStream out = new FileOutputStream(new File(data));
        boolean result = con.retrieveFile("vivekm4a.m4a", out);
        out.close();
        if (result) Log.v("download result", "succeeded");
        con.logout();
        con.disconnect();
    }
}
catch (Exception e)
{
    Log.v("download result","failed");
    e.printStackTrace();
}

In download part, is it possible to just retrieve the file (assuming the file is always jpg) and convert it to Bitmap without having to create a file in my application's folder?

解决方案

You can use picaso library to get the image downloaded from FTP server and obtain its bitmap.
Picasso library has special method "target" which will directly provide you the downloaded bitmap for you code.
Hope it will help you. e.g

Target target = new Target() {
      @Override
      public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {       
       // You can use the bitmap object here
      }
      @Override
      public void onBitmapFailed() {
      }
}
 Picasso.with(this).load("url").into(target);

这篇关于从FTP服务器获取图片,然后转换为Android中的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆