`

Gallery的使用之一

 
阅读更多

                       从一个指定的目录文件下取图片显示可以左右滑动

  例子显示的是从sdcard卡文件读取图片,并显示出来,判断当前是否有要读取的路径,如果有的话当前路径下是否有图片的判定

package com.lenovo.halo.gallery;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
//import android.os.LauncherSyncManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class MainActivity extends Activity {

	private static final String TAG = "MyLauncher";
	private MyHandler m_h;
	private ImageView i;
	private List<String> ImageList;
	private String[] list;
	// private int[] ids = {
	// R.drawable.p12,
	// R.drawable.p13,
	// R.drawable.p1,
	// R.drawable.p2,
	// R.drawable.p3,
	// R.drawable.p4,
	// R.drawable.p5,
	// R.drawable.p6,
	// R.drawable.p7,
	// R.drawable.p8,
	// R.drawable.p9,
	// R.drawable.p10,
	// R.drawable.p11
	// // //no5test add img here!!!!!!!
	// };

	int current = 0;
	TextView no_image;
	ImageView down_image,up_image;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i(TAG, "onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		i = (ImageView) findViewById(R.id.image);
		no_image = (TextView) findViewById(R.id.no_image);
		up_image=(ImageView)findViewById(R.id.up_image);
		down_image=(ImageView)findViewById(R.id.down_image);
		ImageList = getSD();
		Log.i(TAG, "ImageList.size()" + ImageList.size());
		if (ImageList.size() > 0) {
			list = ImageList.toArray(new String[ImageList.size()]);
			Log.i(TAG, "ImageList.size()>0");

			i.setImageURI(Uri.parse(list[current]));
			PowerManager a = (PowerManager) getSystemService(Context.POWER_SERVICE);
			WakeLock w = a.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
					| PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG);
			w.acquire();
			i.setOnTouchListener(new OnTouchListener() {

				@Override
				public boolean onTouch(View arg0, MotionEvent arg1) {
					// TODO Auto-generated method stub
					current++;
					if (current == list.length) {
						current = 0;
					}
					m_h.removeMessages(1);
					Message message1 = new Message();
					message1.what = 1;
					m_h.sendMessage(message1);

					return false;
				}

			});
			m_h = new MyHandler();
			
		} else {
		 
				no_image.setText("当前目录下没有可以显示的图片");
				up_image.setVisibility(View.GONE);
				down_image.setVisibility(View.GONE);
		}

		// i.setImageResource(ids[current]);

	}

	private List<String> getSD() {

		List<String> it = new ArrayList<String>();

//		String path = Environment.getExternalStorageDirectory() + "";
		String path = "/data/imagedir/";
		// File f = new File("/sdcard/");
		Log.e("TEST", "+++++++++++" + path);
		File f = new File(path);
		File[] files = f.listFiles();
		if (f.exists()) {
		 	File f1=new File(path+"/miwen.txt");
			try {
				f1.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				Log.e("TEST",e.toString());
			} 
			if (files.length > 0) {
				for (int i = 0; i < files.length; i++) {
					File file = files[i];
					if (getImageFile(file.getPath()))
						it.add(file.getPath());
				}
			} else {
 					no_image.setText("当前目录下没有可以显示的图片");
				 
			}

		} else {
			 
				no_image.setText("您指定的目录不存在");
			 

		}

		return it;

	}

	private boolean getImageFile(String fName) {
		boolean re;

		String end = fName
				.substring(fName.lastIndexOf(".") + 1, fName.length())
				.toLowerCase();

		if (end.equals("jpg") || end.equals("gif") || end.equals("png")
				|| end.equals("jpeg") || end.equals("bmp")) {
			re = true;
		} else {
			re = false;
		}
		return re;
	}

	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);

			switch (msg.what) {
			case 1:
				// i.setImageResource(ids[current]);
				if(list!=null&&list.length>0){
					i.setImageURI(Uri.parse(list[current]));
					Log.e(TAG, "current img is" + list[current] + "id is "
							+ current);
				}
				 
				break;
			}
		}
	}

//	@Override
//	public boolean onKeyUp(int keyCode, KeyEvent event) {
//		Log.e(TAG, "current img is" + list[current] + "id is " + current
//				+ "event is" + event.getAction());
//		if (event.getAction() == KeyEvent.ACTION_UP) {
//			switch (keyCode) {
//			case KeyEvent.KEYCODE_ENTER:
//				current++;
//				if (current == list.length) {
//					current = 0;
//				}
//
//				// i.setImageResource(list[current]);
//				i.setImageURI(Uri.parse(list[current]));
//
//			default:
//				break;
//			}
//		}
//		return super.onKeyDown(keyCode, event);
//	}

	
	@Override
	protected void onPause() {
		Log.i(TAG, "onPause");
		this.finish();
		System.exit(0);
		super.onPause();
	}
}

 定义的XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.lenovo.halo.gallery.MainActivity" >

    
    <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY" />

    <ImageView
        android:id="@+id/up_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/up"/>

    <ImageView
         android:id="@+id/down_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/down" />
    <TextView 
         android:id="@+id/no_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        
        />
    

</RelativeLayout>

 在mainfast.xml里边定义读取的sdk权限

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics