null指针当我下载图像并存储到android中的Sd卡时出现异常

时间:2014-12-29 07:25:33

标签: android

public class MainActivity extends Activity {
    LinearLayout linearMain;
    CheckBox checkBox;

    ImageView imageview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageview = (ImageView) findViewById(R.id.image);
        new myAsyncTask().execute();

    }

    class myAsyncTask extends AsyncTask<Void, Void, Void> {

        public ProgressDialog dialog;

        myAsyncTask() {
            dialog = new ProgressDialog(MainActivity.this);
            dialog.setMessage("Loading image...");
            dialog.setCancelable(true);
            dialog.setIndeterminate(true);
        }

        // AQuery aq = new AQuery(context);
        // aq.id(recciverimage).image(bitmap);
        // ;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog.show();
        }

        protected Void doInBackground(Void... arg0) {
            try {

                InputStream stream = null;
                URL url = new URL("http://api.lociiapp.com/TransientStorage/"
                        + "1" + ".jpg");
                URLConnection connection = url.openConnection();
                try {
                    // The sdcard directory e.g. '/sdcard' can be used directly,
                    // or
                    // more safely abstracted with getExternalStorageDirectory()
                    File storagePath = Environment
                            .getExternalStorageDirectory();
                    OutputStream output = new FileOutputStream(new File(
                            storagePath, "1.jpg"));
                    try {
                        byte[] buffer = new byte[1024];
                        int bytesRead = 0;
                        while ((bytesRead = stream.read(buffer, 0,
                                buffer.length)) >= 0) {
                            output.write(buffer, 0, bytesRead);
                        }
                    } finally {
                        output.close();
                    }

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

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

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

            dialog.dismiss();
            String imagePath = Environment.getExternalStorageDirectory()
                    .toString() + "/LociiImages/" + "1" + ".jpg";
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath);

            AQuery aq = new AQuery(MainActivity.this);
            aq.id(imageview).image(bitmap);

            // recciverimage.setImageBitmap(bitmap);

        }

    }
}

这是我的代码,我从服务器下载图像并存储在SD卡中,从SD卡我们在图像视图http://api.lociiapp.com/TransientStorage/1.jpg上打印图像使用此URl下载图像到服务器但无法存储请建议我在哪里做错了它给Null Pointer Exception。

1 个答案:

答案 0 :(得分:2)

我认为问题在于:

OutputStream output = new FileOutputStream(new File(
                        storagePath, "1.jpg"));

storagePath所在的位置:

File storagePath = Environment
                        .getExternalStorageDirectory();

稍后您尝试访问下载的图像:

String imagePath = Environment.getExternalStorageDirectory()
                .toString() + "/LociiImages/" + "1" + ".jpg";

如果您比较storagePath和您要查找的路径,那么您会看到您正在子文件夹LociiImages中搜索,但您没有在那里保存图片。

这就是为什么这一行:

Bitmap bitmap = BitmapFactory.decodeFile(imagePath);

投掷NPE。只是一个建议。希望它有所帮助。