2012-08-18 90 views
2

我真的不知道如何實現Share Intent到我的代碼。有人可以幫助我,我是否需要重寫整個代碼?我爲我的共享意圖使用AsyncTask。我想用共享意圖分享我的圖片。請檢查我的下面的代碼,缺少「方法startActivity(Intent)未定義類型ShareImageTask」。Android共享意圖使用AsyncTask

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

    } 


} 

回答

5

使用此:

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

所以指向您的活動的背景下在你的AsyncTask,而不是給自己的AsyncTask。現在你正在調用方法AsyncTask::startActivity(),但那不存在。因爲你在內心階層,所以你必須在startActivity()之上。這是您設置的context變量。

+0

這是一個正確的答案 – 2012-08-18 17:38:24

+0

耶其工作。但我還有另一個問題,我的共享意圖不會捕獲任何東西。你想讓我打開另一個問題嗎? – 2012-08-18 17:48:43

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 

    } 

    } 
}