讀取sd卡上的圖片

讀取sd卡上的圖片
	ImageView im;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_layout);
		im = (ImageView) findViewById(R.id.iv_show);
		ArrayList<String> list = new ArrayList<String>();
		// 掃描sd卡中的照片、
		String str[] = { MediaStore.Images.Media._ID,
				MediaStore.Images.Media.DISPLAY_NAME,
				MediaStore.Images.Media.DATA, };
		Cursor cursor = Test.this.getContentResolver().query(
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI, str, null, null,
				null);

		while (cursor.moveToNext()) {
			// System.out.println(cursor.getString(0));
			// System.out.println(cursor.getString(1));
			// System.out.println(cursor.getString(2));
			list.add(cursor.getString(2));

		}

		// 設置圖片
		String path = list.get(1);
		System.out.println("path--->" + path);
		Bitmap bitmap = getBitmap2(path, 240, 320);
		im.setImageBitmap(bitmap);
	}

	/**
	 * @description 獲取壓縮圖片
	 * @param imageFilePath
	 * @param displayWidth
	 * @param displayHeight
	 * @return
	 */
	public static Bitmap getBitmap2(String imageFilePath, int displayWidth,
			int displayHeight) {
		BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
		bitmapOptions.inJustDecodeBounds = true;
		Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bitmapOptions);

		// 編碼後bitmap的寬高,bitmap除以屏幕寬度得到壓縮比
		int widthRatio = (int) FloatMath.ceil(bitmapOptions.outWidth
				/ (float) displayWidth);
		int heightRatio = (int) FloatMath.ceil(bitmapOptions.outHeight
				/ (float) displayHeight);

		if (widthRatio > 1 && heightRatio > 1) {
			if (widthRatio > heightRatio) {
				// 壓縮到原來的(1/widthRatios)
				bitmapOptions.inSampleSize = widthRatio;
			} else {
				bitmapOptions.inSampleSize = heightRatio;
			}
		}
		bitmapOptions.inJustDecodeBounds = false;
		bmp = BitmapFactory.decodeFile(imageFilePath, bitmapOptions);
		return bmp;
	}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章