gpt4 book ai didi

使用 AsyncTask 的 Android 共享 Intent

转载 作者:太空狗 更新时间:2023-10-29 16:22:04 27 4
gpt4 key购买 nike

我真的不知道如何在我的代码中实现 Share Intent。有人可以帮助我吗,我需要重写整个代码吗?我将 AsyncTask 用于我的共享 Intent 。我想使用分享 Intent 分享我的形象。请检查我下面的代码,“方法 startActivity(Intent) 未定义 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();

}


}

最佳答案

使用这个:

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

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

关于使用 AsyncTask 的 Android 共享 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12020748/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com