1

Google と StackOverflow でこのトピックについて見つけたすべてを検索して試しましたが、とにかく機能させることができませんでした。このコードを持っていますが、FATAL EXCEPTION がスローされます。私はAndroid Rookieなので、何か間違っていると思います。助けてください^^

    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

public class Download extends 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 に答える