2

我真的不知道如何在我的代码中实现共享意图。有人可以帮助我,我需要重写整个代码吗?我将 AsyncTask 用于我的共享意图。我想使用分享意图分享我的图像。请检查我下面的代码,"The method startActivity(Intent) is undefined for the type ShareImageTask" 缺少某些内容。

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();

    }


}
4

3 回答 3

5

用这个:

context.startActivity(Intent.createChooser(share, "Share Image"));

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

于 2012-08-18T17:36:12.400 回答
1

您应该在 onPostExecute(将在 UI 线程上运行)中从您的 Activity 实例调用 startActivity(Intent),例如

class MyActivity {
  public class ShareImageTask extends AsyncTask<String , String , String> {
      . . .
    @Override
    protected String doInBackground(String... args) {
        . . . 
        return file;
    }

    @Override
    protected void onPostExecute(String args) {
        // TODO Auto-generated method stub
        pDialog.dismiss();
        share = new Intent(Intent.ACTION_SEND);
        share.setType("image/jpeg");

        share.putExtra(Intent.EXTRA_STREAM, file[0]);

        startActivity(Intent.createChooser(share, "Share Image")); //<<--The 

    }

  }
}
于 2012-08-18T17:39:44.993 回答
0

Android 开发者博客有一篇关于使用意图共享内容的文章:http ://android-developers.blogspot.com/2012/02/share-with-intents.html

于 2012-08-18T18:18:28.043 回答