2

私はビットマップを効率的にロードするためにdeveloper.androidページを調べていました.http: //developer.android.com/training/displaying-bitmaps/load-bitmap.html彼らは、スケールダウンバージョンをメモリにロードすることについて話しています。明らかに非常に効率的です。

今私の問題は、指定されたメソッドがリソースの id を必要とすることです。インターネットから画像/ビットマップをダウンロードするときに取得できません (ここで間違っている場合は修正してください)。

それで、インターネットからダウンロードした画像に対して、与えられた方法のいくつかのバリエーションを使用できる方法はありますか??

4

1 に答える 1

0

ちょっと私はこの方法について知りませんが、これは私がインターネットから画像をダウンロードしてSDカードに保存する方法です。要件に従ってこれを使用できます。写真を保存するために保存された方法を使用します

public class fetchImage extends Activity implements OnClickListener {
    int id;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = 1;// declaring static as of now

    }

    {

        new BackgroundTask().execute();
        File storagePath = Environment.getExternalStorageDirectory();
        File imgFile = new File(storagePath, "/Pictures/" + id + ".jpg");

        if (imgFile.exists()) {
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
                    .getAbsolutePath());
        }

    }

    class BackgroundTask extends AsyncTask<Void, Void, Void> {
        ProgressDialog mDialog;

        protected void onPreExecute() {
            mDialog = ProgressDialog.show(fetchImage.this, "",
                    getString(R.string.progress_bar_loading), true);
        };

        @Override
        protected Void doInBackground(Void... params) {
            try {

                savesd(id, null);

            } catch (final Exception e) {

            }
            return null;
        }

        private void savesd(int id, URL uri) throws IOException {
            URL url;
            if (uri == null) {
                url = new URL("http://i.zdnet.com/blogs/3-29-androids.jpg");
            } else {
                url = uri;
            }
            InputStream input = url.openStream();
            try {
                File storagePath = Environment.getExternalStorageDirectory();
                OutputStream output = new FileOutputStream(new File(
                        storagePath, "/Pictures/" + id + ".jpg"));
                try {
                    byte[] buffer = new byte[20000];
                    int bytesRead = 0;
                    while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
                        output.write(buffer, 0, bytesRead);
                    }
                } finally {
                    output.close();
                }
            } catch (Exception e) {

                e.printStackTrace();
            } finally {
                input.close();
            }

        }

        protected void onPostExecute(Void result) {
            mDialog.dismiss();
        };
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

}
于 2012-11-02T05:28:43.340 に答える