1

以下是我的代码。我正在尝试将 String image_url 从 SingleMenuItemActivity.class 传递给 SaveImageTask.class 。我还不能完成它。也许我的代码有问题。有人可以检查我的代码。我同时使用 AsyncTask。

SingleMenuItemActivity.class

public class SingleMenuItemActivity  extends Activity {

    // XML node keys
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";  
static final String KEY_THUMB_URL = "thumb_url";
private ProgressDialog pDialog;
String title;
String artist;
String image_url;
ImageView view;
Intent intent;
Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.single_list_item);
        new loadSingleView().execute(); 


        view = (ImageView) findViewById(R.id.single_image);



    }



public class loadSingleView extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(
                    SingleMenuItemActivity.this);
            pDialog.setMessage("Connecting to Server ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
        @Override
        protected String doInBackground(String... args) {
            // updating UI from Background Thread


            Intent in = getIntent();

            image_url = in.getStringExtra(KEY_THUMB_URL);
                        title = in.getStringExtra(KEY_TITLE);
            artist = in.getStringExtra(KEY_ARTIST);

            return null;

                    }
        @Override       
        protected void onPostExecute(String args) {
            // dismiss the dialog after getting all products

            ImageLoader imgLoader = new ImageLoader(getApplicationContext());

            imgLoader.DisplayImage(image_url, view);

            TextView lblName = (TextView) findViewById(R.id.name_title);
            TextView lblCost = (TextView) findViewById(R.id.name_artist);

            lblName.setText(title);
            lblCost.setText(artist);
            pDialog.dismiss();

        }


}   
 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.save_image:


            new SaveImageTask(this).execute(image_url);


                  return true;

            default:
                return false;
        }

SaveImageTask.class

public class SaveImageTask extends AsyncTask<String , String , String>
{
    private Context context;
    private ProgressDialog pDialog;
    String image_url;
    URL myFileUrl = null;
    Bitmap bmImg = null;

    public SaveImageTask(Context context) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub

        super.onPreExecute();

        pDialog = new ProgressDialog(context);
        pDialog.setMessage("Downloading Image ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub

        try {  

            myFileUrl = new URL(image_url);
            HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();   
            conn.setDoInput(true);   
            conn.connect();     
            InputStream is = conn.getInputStream();
            bmImg = BitmapFactory.decodeStream(is); 
        }
        catch (IOException e)
        {       
            e.printStackTrace();  
        }
        try {       

            String path = myFileUrl.getPath();
            String idStr = path.substring(path.lastIndexOf('/') + 1);
        File filepath = Environment.getExternalStorageDirectory();
        File dir = new File (filepath.getAbsolutePath() + "/Wallpaper/");
            dir.mkdirs();
            String fileName = idStr;
            File file = new File(dir, fileName);
            FileOutputStream fos = new FileOutputStream(file);
            bmImg.compress(CompressFormat.JPEG, 75, fos);   
            fos.flush();    
            fos.close();       
        }
        catch (Exception e)
                {
                    e.printStackTrace();  
                }
        return null;   
    }

    @Override
    protected void onPostExecute(String args) {
        // TODO Auto-generated method stub

        pDialog.dismiss();

    }


}
4

1 回答 1

2

代替

 myFileUrl = new URL(image_url);

尝试使用 arg[0] array

于 2012-08-18T03:21:12.000 回答