BitmapFactory.decodeStream 无法从 ftp 解码 png 类型

2023-12-04

  • 我的错误是什么?如何显示来自 FTP 的 png?

我是 android 新手,尝试显示来自不同连接/源的图像。 然后我已经显示了从可绘制和 HTTP 加载的图像。

现在,我尝试从 FTP 显示,当我使用时,我收到消息“---解码器->解码返回 false”。BitmapFactory.decodeStream(ins, null, options);

然后我找到了解决方案..

static class FlushedInputStream extends FilterInputStream {
    public FlushedInputStream(InputStream inputStream) {
        super(inputStream);
    }

    @Override
    public long skip(long n) throws IOException {
        long totalBytesSkipped = 0L;
        while (totalBytesSkipped < n) {
            long bytesSkipped = in.skip(n - totalBytesSkipped);
            if (bytesSkipped == 0L) {
                int b = read();
                if (b < 0) {
                    break; // we reached EOF
                } else {
                    bytesSkipped = 1; // we read one byte
                }
            }
            totalBytesSkipped += bytesSkipped;
        }
        return totalBytesSkipped;
    }
}

然后就可以加载/解码文件类型“Jpeg、jpg”,有完整的显示。

但是当位图再次加载文件类型“PNG”时,log cat 说“---解码器->解码返回 false”。

感谢您的建议...

    ImageView bmImage = (ImageView) findViewById(R.id.faceImageView);
    BitmapFactory.Options bmOptions;
    bmOptions = new BitmapFactory.Options();
    bmOptions.inSampleSize = 1;
    Bitmap bm = LoadImage(image_URL, bmOptions);
    bmImage.setImageBitmap(bm);

...

private Bitmap LoadImage(String URL, BitmapFactory.Options options) {
    Bitmap bitmap = null;
    InputStream in = null;
    FlushedInputStream fIns = null;

    try {
        if (isFTP) {
            in = downloadFile("");
            if (remoteFile.toLowerCase().contains(".png")) {
                fIns = new FlushedInputStream(in);
                bitmap = BitmapFactory.decodeStream(fIns, null, options);
                // byte[] bMapArray = new byte[buf.available()];
                // buf.read(bMapArray);
                // bitmap = BitmapFactory.decodeByteArray(bMapArray, 0,
                // bMapArray.length);
            } else {
                fIns = new FlushedInputStream(in);
                bitmap = BitmapFactory.decodeStream(fIns);
            }

        } else { // HTTP
            in = OpenHttpConnection(URL);
            fIns = new FlushedInputStream(in);
            bitmap = BitmapFactory.decodeStream(fIns);
        }

        in.close();
    } catch (IOException e1) {
    }
    return bitmap;
}
public synchronized InputStream downloadFile(String localfilename) {
        InputStream inputStream = null;
        String user = "aaa";
        String pass = "8888";
        String host = "xxx.xxx.xxx.xxx";

        try {

            FTPClient mFTPClient = new FTPClient();
            mFTPClient.connect(host);
            mFTPClient.login(user, pass);
            mFTPClient.enterLocalPassiveMode();
            mFTPClient.changeWorkingDirectory("/DroidPic");
            String[] aa = mFTPClient.listNames();
            String strTmp = "";
            do {
                strTmp = aa[(new Random()).nextInt(aa.length)];
            } while (remoteFile == strTmp);
            remoteFile = strTmp;
            inputStream = mFTPClient.retrieveFileStream(remoteFile);
        } catch (Exception ex) {
            Toast.makeText(this, "Err:" + ex.getMessage(), Toast.LENGTH_LONG)
                    .show();
        }
        return inputStream;
    }

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

BitmapFactory.decodeStream 无法从 ftp 解码 png 类型 的相关文章

随机推荐