1

我已经搜索并尝试了我在 Google 和 StackOverflow 上找到的关于这个主题的所有内容,但我没有找到让它起作用的方法。我有这段代码,但它给了我一个致命的例外。我认为,因为我是 Android 新手,所以我做错了一些事情。请帮帮我^^

    public void svgPhoto() throws IOException {
    String dir = Environment.getExternalStorageDirectory().toString();
    OutputStream fos = null;
    File file = new File(dir,"downloadImage.JPEG");
    Bitmap bm =BitmapFactory.decodeFile("http://perlbal.hi-pi.com/blog-images/3278/gd/1242316719/Chat-chat.jpg");
    fos = new FileOutputStream(file);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bm.compress(Bitmap.CompressFormat.JPEG, 50, bos);
    bos.flush();
    bos.close();
4

2 回答 2

0

公共类下载扩展 AsyncTask {

        @Override
        protected Void doInBackground(Void... params) 
                    {
            try {
        URL url = new URL(imageURL);
        file2 = new File(fileName);


        long startTime = System.currentTimeMillis();

        /* Open a connection to that URL. */
        URLConnection ucon = url.openConnection();

        /*
         * Define InputStreams to read from the URLConnection.
         */
        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);

        /*
         * Read bytes to the Buffer until there is nothing more to read(-1).
         */
        ByteArrayBuffer baf = new ByteArrayBuffer(50);
        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }

        /* Convert the Bytes read to a String. */
        FileOutputStream fos = new FileOutputStream(file2);
        fos.write(baf.toByteArray());
        fos.close();
        Log.d("ImageManager",
                "download ready in"
                        + ((System.currentTimeMillis() - startTime) / 1000)
                        + " sec");



    } catch (IOException e) {

    }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            try {
                try {

        File fileWithinMyDir = getApplicationContext().getFilesDir();
        String PATH = fileWithinMyDir.getAbsolutePath();


        Drawable d = Drawable.createFromPath(PATH.toString());
        Bitmap bmp = ((BitmapDrawable) d).getBitmap();

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

                if (dialog != null) {
                    if (dialog.isShowing()) {
                        dialog.dismiss();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
于 2013-02-28T14:32:57.463 回答
0

尝试下一个代码:

String dir = Environment.getExternalStorageDirectory().toString();

File outputfile = new File(dir+"/downloadImage.JPEG");
OutputStream fOut = null;
try {
fOut = new FileOutputStream(outputfile);
Bitmap bm =BitmapFactory.decodeFile("http://perlbal.hi-pi.com/blog-images/3278/gd/1242316719/Chat-chat.jpg");
bm.compress(CompressFormat.JPEG, 50, fOut); 
} catch (IOException e) {
Logger.error(e.getMessage(), e);
}finally{
try {
if(fOut != null){
fOut.flush();
fOut.close();
}
} catch (IOException e) {
Logger.error(e.getMessage(), e);
}
}
于 2013-02-28T16:07:52.953 回答