我有一个菜单按钮,单击时,我想访问 ImageView 中显示的图像的 URL。
我在这里找到了一个可能的解决方案,但我需要一种方法来获取当前显示的图像的 URL,这可能吗?
使用可能的解决方案时,我在“downloadFile”上收到错误消息,指出“void 是变量 downloadFile 的无效类型”
我在这里所做的事情是否朝着正确的方向实现这一目标,为什么会给我这个错误?
任何指导将不胜感激!
类(一半是代码):
public class ImageDetailFragment extends Fragment {
private static final String IMAGE_DATA_EXTRA = "extra_image_data";
private String mImageUrl;
private ImageView mImageView;
private ImageFetcher mImageFetcher;
public static ImageDetailFragment newInstance(String imageUrl) {
final ImageDetailFragment f = new ImageDetailFragment();
final Bundle args = new Bundle();
args.putString(IMAGE_DATA_EXTRA, imageUrl);
f.setArguments(args);
return f;
}
public ImageDetailFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mImageUrl = getArguments() != null ? getArguments().getString(
IMAGE_DATA_EXTRA) : null;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.image_detail_fragment,
container, false);
mImageView = (ImageView) v.findViewById(R.id.imageView);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (RecentDetailActivity.class.isInstance(getActivity())) {
mImageFetcher = ((RecentDetailActivity) getActivity())
.getImageFetcher();
mImageFetcher.loadImage(mImageUrl, mImageView);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.image_menu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Favoritewallpaper:
Toast.makeText(getActivity(), "Save Wallpaper Clicked",
Toast.LENGTH_SHORT).show();
case R.id.Setwallpaper:
// Set wallpaper here:
Bitmap bmImg;
//Here
void downloadFile(String mImageUrl) {
URL myFileUrl = null;
try {
myFileUrl = new URL(mImageUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
// this.imView.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getActivity());
try {
myWallpaperManager.setBitmap(bmImg);
} catch (Exception e) {
//Log.e("MyLog", e.toString());
}
}
}
return true;
}
@Override
public void onDestroy() {
super.onDestroy();
if (mImageView != null) {
ImageWorker.cancelWork(mImageView);
mImageView.setImageDrawable(null);
}
}
}