如何使用j2me将图像保存到三星、Lg、诺基亚手机的照片库中

发布于 2024-12-16 20:25:37 字数 375 浏览 2 评论 0原文

可能的重复:
如何使用 j2me 将图像保存到照片库

我正在开发一个应用程序,我想在其中从互联网下载图像并将其保存到每个手机的照片库中。请给我建议。 谢谢,

Possible Duplicate:
How to save an image into photo gallery using j2me

I am working on an application in which i want to download an image from internet and save it to photo gallery of every mobile. Please provide me suggestion.
Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺孤伤于心 2024-12-23 20:25:37
private void downloadImage(String folder, String photoName, String url) throws IOException
    {
        byte[] rawImg = null;
        try
        {
            String imageData = getDataFromUrl(url);
            rawImg = imageData.getBytes();
            putPhotoToPhone(rawImg, folder, photoName);
        }
        catch(Exception e1) {
            e1.printStackTrace();
        }
    }

    private String getDataFromUrl(String url) throws IOException {

        StringBuffer b = new StringBuffer();
        InputStream is = null;
        HttpConnection c = null;

        long len = 0 ;
        int ch = 0;
        c = (HttpConnection)Connector.open(url);
        is = c.openInputStream();
        len = c.getLength();
        if( len != -1)
        {
            for(int i =0 ; i < len ; i++ )
            {
                if((ch = is.read()) != -1)
                {
                    b.append((char) ch);
                }
            }
        }
        else
        {
            while ((ch = is.read()) != -1)
            {
                len = is.available() ;
                b.append((char)ch);
            }
        }
        is.close();
        c.close();
        return b.toString();
    }
    private void putPhotoToPhone(byte[] rawImg, String photoDir, String imageName)
    {
        FileConnection fcDir, fcFile;
        String pRoot = "Phone:/";
        OutputStream os;
        if (rawImg != null)
        {
            try
            {
                fcDir = (FileConnection) Connector.open("file:///"+pRoot+photoDir+"/", Connector.READ_WRITE);
                if (!fcDir.exists())
                    fcDir.mkdir();
                fcFile = (FileConnection) Connector.open("file:///"+pRoot+photoDir+"/"+imageName, Connector.READ_WRITE);
                if (fcFile.exists())
                    fcFile.delete();
                fcFile.create();
                os = fcFile.openOutputStream();
                os.write(rawImg);
                os.flush();
                os.close();
                fcFile.close();
                fcDir.close();
            }
            catch (Exception e) {}
        }
    }
private void downloadImage(String folder, String photoName, String url) throws IOException
    {
        byte[] rawImg = null;
        try
        {
            String imageData = getDataFromUrl(url);
            rawImg = imageData.getBytes();
            putPhotoToPhone(rawImg, folder, photoName);
        }
        catch(Exception e1) {
            e1.printStackTrace();
        }
    }

    private String getDataFromUrl(String url) throws IOException {

        StringBuffer b = new StringBuffer();
        InputStream is = null;
        HttpConnection c = null;

        long len = 0 ;
        int ch = 0;
        c = (HttpConnection)Connector.open(url);
        is = c.openInputStream();
        len = c.getLength();
        if( len != -1)
        {
            for(int i =0 ; i < len ; i++ )
            {
                if((ch = is.read()) != -1)
                {
                    b.append((char) ch);
                }
            }
        }
        else
        {
            while ((ch = is.read()) != -1)
            {
                len = is.available() ;
                b.append((char)ch);
            }
        }
        is.close();
        c.close();
        return b.toString();
    }
    private void putPhotoToPhone(byte[] rawImg, String photoDir, String imageName)
    {
        FileConnection fcDir, fcFile;
        String pRoot = "Phone:/";
        OutputStream os;
        if (rawImg != null)
        {
            try
            {
                fcDir = (FileConnection) Connector.open("file:///"+pRoot+photoDir+"/", Connector.READ_WRITE);
                if (!fcDir.exists())
                    fcDir.mkdir();
                fcFile = (FileConnection) Connector.open("file:///"+pRoot+photoDir+"/"+imageName, Connector.READ_WRITE);
                if (fcFile.exists())
                    fcFile.delete();
                fcFile.create();
                os = fcFile.openOutputStream();
                os.write(rawImg);
                os.flush();
                os.close();
                fcFile.close();
                fcDir.close();
            }
            catch (Exception e) {}
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文