0

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);
    }
}
}
Community
  • 1
  • 1
Jack
  • 2,043
  • 7
  • 40
  • 69
  • i think you have to take a look at your code again... you are returning true for void method.. – Swap-IOS-Android Nov 07 '13 at 17:23
  • You are getting a compiler error because you cannot declare a method inside a `switch` statement. Move `dowloadFile(String mImageUrl)` outside of the `onOptionsItemSelected(...)` method. Then, from within the `switch` statement, your code would be something like `case R.id.Setwallpapaer: downloadFile(urlToImage); break;` – MDrabic Nov 07 '13 at 17:31

1 Answers1

0

you cannot get the uri since there are several methods of setting the image, not only setImageUri, see setImage* methods

pskink
  • 23,874
  • 6
  • 66
  • 77