您好我是Android新手,我需要使用Share Intent共享Image。为此我使用了AsyncTask。
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;
File file;
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()
+ "/Google Image Wallpaper/");
dir.mkdirs();
String fileName = idStr;
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();
share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, file);
startActivity(Intent.createChooser(share, "Share Image"));
}
}
现在我必须通过传递图像URL来调用此ShareImageTask。但我不知道怎么称呼这个???
例如: -
String Imageurl="my imageurl";
new ShareImageTask(Imageurl).execute();
答案 0 :(得分:1)
您可以将ShareImageTask
称为:
String Imageurl="my imageurl";
new ShareImageTask(Curent_Activity.this).execute(Imageurl);
并将您的In onPostExecute
代码更改为:
@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,Uri.parse(file.getAbsolutePath().toString()));
context.startActivity(Intent.createChooser(share, "Share Image"));
}
答案 1 :(得分:0)
将AsyncTask构造函数更改为喜欢此
public ShareImageTask(Context context,String image_url) {
this.context = context;
this.image_url = image_url;
}
所以,你可以像这样打电话
String Imageurl="my imageurl";
new ShareImageTask(getApplicationContext(),Imageurl).execute();
答案 2 :(得分:0)
只需这样做..
ShareImageTask task = new ShareImageTask();
task.execute(Imageurl);
答案 3 :(得分:0)
只需要在代码中更改一些内容..
你这样写的
new ShareImageTask(Imageurl).execute();
但你必须这样写
new ShareImageTask(getApplicationContext()).execute(Imageurl);
你的创建构造函数只有Context
参数,因此当你使用ShareImageTask class
如下面的代码:
课程定义:
public ShareImageTask(Context context) {
this.context = context;
}
班级初始化;
new ShareImageTask(getApplicationContext());