如何使用模拟器在android中下载图像

时间:2014-04-02 07:37:09

标签: android

我有从网络下载图像的代码。没有错误。但是当我打开应用程序时,它会崩溃。我认为这是因为互联网连接。所以我给了路径,图像存储在我的系统中。但它仍然崩溃。对此有何解决方案?

这是代码

*MainActivity.java*

    public class MainActivity extends ActionBarActivity {
        public static final String URL ="Users/sangetha/Documents/news_icon_1.png";
        ImageView imageView;
        Button button;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_image_view_from_url);
            //Get the id of button & Image
            imageView=(ImageView)findViewById(R.id.icon);
            button=(Button)findViewById(R.id.button_click);
            button.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                     // Create an object for subclass of AsyncTask
                    DownloadImageTask task=new DownloadImageTask();
                      // Execute the task
                       task.execute(new String[] { URL });
                }
            });

            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, new PlaceholderFragment()).commit();
            }
        }

我在on create method

之外调用了同步任务
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap>{
    @Override
          protected Bitmap doInBackground(String... urls){
            Bitmap map = null;
            for (String url : urls) {
            map = downloadImage(url);
         }
        return map; 
        }
    // Sets the Bitmap returned by doInBackground
    @Override
    protected void onPostExecute(Bitmap result) {
        imageView.setImageBitmap(result);
    }
    // Creates Bitmap from InputStream and returns it
    private Bitmap downloadImage(String url) {
        Bitmap bitmap = null;
        InputStream stream = null;
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 1;

        try {
            stream = getHttpConnection(url);
            bitmap = BitmapFactory.
                    decodeStream(stream, null, bmOptions);
            stream.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return bitmap;
    }
    // Makes HttpURLConnection and returns InputStream
    private InputStream getHttpConnection(String urlString) throws IOException {
        InputStream stream = null;
        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();

        try {
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setRequestMethod("GET");
            httpConnection.connect();

            if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                stream = httpConnection.getInputStream();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return stream;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.image_view_from_url, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(
                R.layout.fragment_image_view_from_url, container, false);
        return rootView;
    }
}}

fragment_main.xml

<ImageView
    android:id="@+id/imageView"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:contentDescription="@string/icon"
    android:maxHeight="70dp"
    android:maxWidth="70dp" />
<Button
    android:id="@+id/button_click"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_centerInParent="true"
    android:text="@string/button_click"/>

问题是什么?有谁可以帮助我

1 个答案:

答案 0 :(得分:0)

您的图片视图名为imageView not icon:

<ImageView
    android:id="@+id/imageView"...

请尝试以下方法:
imageView = (ImageView)findViewById(R.id.imageView);

发生NullPointerException是因为您将findViewById(R.id.icon)的结果转换为ImageView。