In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "how to implement a picture viewer in Android". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to implement a picture viewer in Android.
The specific code is as follows:
Public class MainActivity extends Activity {private EditText et_path;private ImageView iv; / / create handler object / / private Handler handler = new Handler () {/ process messages / / public void handleMessage (android.os.Message msg) {/ Bitmap bitmap = (Bitmap) msg.obj;// iv.setImageBitmap (bitmap); / /};}; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState) SetContentView (R.layout.activity_main); / / [1] find the control we care about: et_path = (EditText) findViewById (R.id.et_path); iv = (ImageView) findViewById (R.id.iv) } / / [2] Click the button to view the source code public void click (View v) {new Thread () {public void run () {try {/ / [2.1] of the specified path to get the path String path = et_path.getText (). ToString (). Trim (); File file = new File (getCacheDir (), Base64.encodeToString (path.getBytes (), Base64.DEFAULT) If (file.exists () & & file.length () > 0) {/ / use cached pictures System.out.println ("use cached pictures"); final Bitmap cacheBitmap = BitmapFactory.decodeFile (file.getAbsolutePath ()); / / display cacheBitmap on iv / / Message msg = Message.obtain (); / / msg.obj = cacheBitmap / / handler.sendMessage (msg); / / send runOnUiThread (new Runnable () {public void run () {iv.setImageBitmap (cacheBitmap);}}) } else {/ / first visit to the network to obtain data System.out.println ("first access to the network"); / / [2.2] create a url object URL url = new URL (path); / / [2.3] get httpurlconnection HttpURLConnection conn = (HttpURLConnection) url.openConnection () / / [2.4] set the request method conn.setRequestMethod ("GET"); / / [2.5] set the timeout conn.setConnectTimeout (5000); / / [2.6] get the status code returned by the server int code = conn.getResponseCode () If (code = = 200) {/ / [2.7] No matter what data it is (txt text picture data), it always returns InputStream in = conn.getInputStream () as a stream; / / [2.7] caching images Google provides us with a cache directory FileOutputStream fos = new FileOutputStream (file) Int len=-1; byte [] buffer = new byte [1024]; / / 1kb while ((len=in.read (buffer))! =-1) {fos.write (buffer, 0, len);} fos.close (); in.close () / / [2.8B] get bitmap (bitmap) final Bitmap bitmap = BitmapFactory.decodeFile (file.getAbsolutePath ()) through bitmap factory / / this sentence api runs in the UI thread no matter where you call action (new Runnable () {public void run () {/ / run method must be executed in the UI thread / / [2.9] display the bitmap on the iv iv.setImageBitmap (bitmap) }}); / / Message msg = Message.obtain (); / / use static methods of msg to reduce object creation / / msg.obj = bitmap;// handler.sendMessage (msg); / / send messages}} catch (Exception e) {e.printStackTrace ();};} .start () }} at this point, I believe you have a deeper understanding of "how to implement a picture viewer in Android". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.