Android Share Intent使用AsyncTask

And*_*ice 2 android share android-asynctask

我真的不知道如何在我的代码中实现Share Intent.有人可以帮我,我需要重写整个代码吗?我正在使用AsyncTask来实现我的共享意图.我想使用共享意图分享我的图像.请检查我的下面的代码,"缺少类型ShareImageTask的方法startActivity(Intent)".

ShareImageTask.class

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


    public ShareImageTask(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(args[0]);
            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();     
            share = new Intent(Intent.ACTION_SEND);
            share.setType("image/jpeg");

            share.putExtra(Intent.EXTRA_STREAM,file);

            context.startActivity(Intent.createChooser(share, "Share Image")); 
        }
        catch (Exception e)
                {
                    e.printStackTrace();  
                }




        return null;   
    }

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

    }


}
Run Code Online (Sandbox Code Playgroud)

tol*_*gap 5

用这个:

context.startActivity(Intent.createChooser(share, "Share Image"));
Run Code Online (Sandbox Code Playgroud)

所以你指向你的AsyncTask中的Activity的Context,而不是AsyncTask本身.现在你正在调用一个方法AsyncTask::startActivity(),但那不存在.因为你在内部类中,所以你必须拥有要调用的对象startActivity().那是context你设定的变量.