I have a menu button, when clicked, I would like to access the URL of the image that is being displayed in the ImageView.
I have found a possible solution here but I need a way of getting the URL of the image that is currently being shown, would it be possible?
When using the possible solution, I get the erorr on "downloadFile" stating "void is an invalid type for the variable downloadFile"
Am I going the right direction to achieve this with what I am doing here and why is it giving me this error?
Any guidance would be greatly appreciated!
Class(Half way down is code):
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);
}
}
}