Android ImageViewer class supporting Pinch-Zoom and Scrolling
Asked Answered
P

2

17

I am searching an ImageViewer library that opens an image in my application for a given URI (the image fetched by a webservice is already stored within my application in a secured place). I really like the "Samsung Galaxy S" ImageViewer-Activity because it uses pinch-zoom and "scrolling" vertical/horizontal. Also it scales the picture very fast on my samsung phone :)

I know that I can open an image with an intent like this:

Intent i = new Intent(Intent.ACTION_VIEW);  
i.setDataAndType(uri, "image/*");
startActivity(i);

The best suitable viewer is being called, when none is found an ActivityNotFoundException is raised. So thats cool!

But the problem is that I am not allowed to open an image with an external intent (for security purposes). i.e: The user should not have the posibility to save the opened image via a menu option to his external sd-card or send this picture to another service (email/twitter or s.o.). So I have to write my own ImageViewer-Class (Activity) that can only be called within my application... Unfortunately I am not very skilled transforming images, so is there any open source project (or library) that covers this use case?

I already asked google and found this one http://code.google.com/p/android-pinch/ but it didnt work very well (also it has no scroll-functionality).

Thanks for your tips :)

Pinchhit answered 3/2, 2011 at 15:51 Comment(1)
Question that is also about pinch-to-zoom in android: #2646148Ancon
W
17

The easiest way to handle images is using a WebView, if the image is stored local or somewhere online. WebView supports pinch to zoom and other functions.

Example Java:

String imageUrl = "file:///local/dir/image.jpg"; // http://example.com/image.jpg
WebView wv = (WebView) findViewById(R.id.yourwebview);
wv.getSettings().setBuiltInZoomControls(true);
wv.loadUrl(imageUrl);

XML source:

<WebView android:id="@+id/yourwebview"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
Wallet answered 3/2, 2011 at 16:9 Comment(3)
Well thats an awesome solution, thanks! It works the way it should. Although I have to set one option wv.setInitialScale(100); Glad that its that easy to view an image in android ;) Is the image being stored in the internal db from my application? Or do I have to call wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);`?Pinchhit
I don't think it's stored in cache or anything related.Wallet
webView.getSettings().setDisplayZoomControls(false); It will hide Default ZoomControlsAnkney
P
20

At this moment I'm too use WebView. But this is conceptually wrong, WebView is not for displaying images, it for displaying web content. WebView is bulky and cumbersome. You restricted in customizing it. For example, I need scaling, but I don't want to see zoom controls, or I want to move them in other place then default.

Therefore, I searched about this problem and found some solutions:

  • PhotoView widget from Chris Banes
  • Also, you can look at this tutorial from Sony Ericsson (sources available) and can implement your own widget: part1, part2, part3
  • And another one library ImageViewZoom at github

UPDATE

Chris Banes was created awesome library which supports gestures for zooming pictures, see first point above

Pricket answered 19/7, 2012 at 10:16 Comment(1)
ImageViewZoom doesn't support pinch to zoom. Quite useless these days when users want to pinch with their fingers to zoom in on part of the image.Sleuthhound
W
17

The easiest way to handle images is using a WebView, if the image is stored local or somewhere online. WebView supports pinch to zoom and other functions.

Example Java:

String imageUrl = "file:///local/dir/image.jpg"; // http://example.com/image.jpg
WebView wv = (WebView) findViewById(R.id.yourwebview);
wv.getSettings().setBuiltInZoomControls(true);
wv.loadUrl(imageUrl);

XML source:

<WebView android:id="@+id/yourwebview"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
Wallet answered 3/2, 2011 at 16:9 Comment(3)
Well thats an awesome solution, thanks! It works the way it should. Although I have to set one option wv.setInitialScale(100); Glad that its that easy to view an image in android ;) Is the image being stored in the internal db from my application? Or do I have to call wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);`?Pinchhit
I don't think it's stored in cache or anything related.Wallet
webView.getSettings().setDisplayZoomControls(false); It will hide Default ZoomControlsAnkney

© 2022 - 2024 — McMap. All rights reserved.