android仿微信图片上传进度,android高仿微信发布动态(选择图片)
【實例簡介】Android 超高仿微信圖片選擇器
【實例截圖】
【核心代碼】
public class MainActivity extends Activity implements OnImageDirSelected
{
private ProgressDialog mProgressDialog;
/**
* 存儲文件夾中的圖片數量
*/
private int mPicsSize;
/**
* 圖片數量最多的文件夾
*/
private File mImgDir;
/**
* 所有的圖片
*/
private List mImgs;
private GridView mGirdView;
private MyAdapter mAdapter;
/**
* 臨時的輔助類,用于防止同一個文件夾的多次掃描
*/
private HashSet mDirPaths = new HashSet();
/**
* 掃描拿到所有的圖片文件夾
*/
private List mImageFloders = new ArrayList();
private RelativeLayout mBottomLy;
private TextView mChooseDir;
private TextView mImageCount;
int totalCount = 0;
private int mScreenHeight;
private ListImageDirPopupWindow mListImageDirPopupWindow;
private Handler mHandler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
mProgressDialog.dismiss();
// 為View綁定數據
data2View();
// 初始化展示文件夾的popupWindw
initListDirPopupWindw();
}
};
/**
* 為View綁定數據
*/
private void data2View()
{
if (mImgDir == null)
{
Toast.makeText(getApplicationContext(), "擦,一張圖片沒掃描到",
Toast.LENGTH_SHORT).show();
return;
}
mImgs = Arrays.asList(mImgDir.list());
/**
* 可以看到文件夾的路徑和圖片的路徑分開保存,極大的減少了內存的消耗;
*/
mAdapter = new MyAdapter(getApplicationContext(), mImgs,
R.layout.grid_item, mImgDir.getAbsolutePath());
mGirdView.setAdapter(mAdapter);
mImageCount.setText(totalCount "張");
};
/**
* 初始化展示文件夾的popupWindw
*/
private void initListDirPopupWindw()
{
mListImageDirPopupWindow = new ListImageDirPopupWindow(
LayoutParams.MATCH_PARENT, (int) (mScreenHeight * 0.7),
mImageFloders, LayoutInflater.from(getApplicationContext())
.inflate(R.layout.list_dir, null));
mListImageDirPopupWindow.setOnDismissListener(new OnDismissListener()
{
@Override
public void onDismiss()
{
// 設置背景顏色變暗
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 1.0f;
getWindow().setAttributes(lp);
}
});
// 設置選擇文件夾的回調
mListImageDirPopupWindow.setOnImageDirSelected(this);
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics outMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
mScreenHeight = outMetrics.heightPixels;
initView();
getImages();
initEvent();
}
/**
* 利用ContentProvider掃描手機中的圖片,此方法在運行在子線程中 完成圖片的掃描,最終獲得jpg最多的那個文件夾
*/
private void getImages()
{
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED))
{
Toast.makeText(this, "暫無外部存儲", Toast.LENGTH_SHORT).show();
return;
}
// 顯示進度條
mProgressDialog = ProgressDialog.show(this, null, "正在加載...");
new Thread(new Runnable()
{
@Override
public void run()
{
String firstImage = null;
Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver mContentResolver = MainActivity.this
.getContentResolver();
// 只查詢jpeg和png的圖片
Cursor mCursor = mContentResolver.query(mImageUri, null,
MediaStore.Images.Media.MIME_TYPE "=? or "
MediaStore.Images.Media.MIME_TYPE "=?",
new String[] { "image/jpeg", "image/png" },
MediaStore.Images.Media.DATE_MODIFIED);
Log.e("TAG", mCursor.getCount() "");
while (mCursor.moveToNext())
{
// 獲取圖片的路徑
String path = mCursor.getString(mCursor
.getColumnIndex(MediaStore.Images.Media.DATA));
Log.e("TAG", path);
// 拿到第一張圖片的路徑
if (firstImage == null)
firstImage = path;
// 獲取該圖片的父路徑名
File parentFile = new File(path).getParentFile();
if (parentFile == null)
continue;
String dirPath = parentFile.getAbsolutePath();
ImageFloder imageFloder = null;
// 利用一個HashSet防止多次掃描同一個文件夾(不加這個判斷,圖片多起來還是相當恐怖的~~)
if (mDirPaths.contains(dirPath))
{
continue;
} else
{
mDirPaths.add(dirPath);
// 初始化imageFloder
imageFloder = new ImageFloder();
imageFloder.setDir(dirPath);
imageFloder.setFirstImagePath(path);
}
int picSize = parentFile.list(new FilenameFilter()
{
@Override
public boolean accept(File dir, String filename)
{
if (filename.endsWith(".jpg")
|| filename.endsWith(".png")
|| filename.endsWith(".jpeg"))
return true;
return false;
}
}).length;
totalCount = picSize;
imageFloder.setCount(picSize);
mImageFloders.add(imageFloder);
if (picSize > mPicsSize)
{
mPicsSize = picSize;
mImgDir = parentFile;
}
}
mCursor.close();
// 掃描完成,輔助的HashSet也就可以釋放內存了
mDirPaths = null;
// 通知Handler掃描圖片完成
mHandler.sendEmptyMessage(0x110);
}
}).start();
}
/**
* 初始化View
*/
private void initView()
{
mGirdView = (GridView) findViewById(R.id.id_gridView);
mChooseDir = (TextView) findViewById(R.id.id_choose_dir);
mImageCount = (TextView) findViewById(R.id.id_total_count);
mBottomLy = (RelativeLayout) findViewById(R.id.id_bottom_ly);
}
private void initEvent()
{
/**
* 為底部的布局設置點擊事件,彈出popupWindow
*/
mBottomLy.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mListImageDirPopupWindow
.setAnimationStyle(R.style.anim_popup_dir);
mListImageDirPopupWindow.showAsDropDown(mBottomLy, 0, 0);
// 設置背景顏色變暗
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = .3f;
getWindow().setAttributes(lp);
}
});
}
@Override
public void selected(ImageFloder floder)
{
mImgDir = new File(floder.getDir());
mImgs = Arrays.asList(mImgDir.list(new FilenameFilter()
{
@Override
public boolean accept(File dir, String filename)
{
if (filename.endsWith(".jpg") || filename.endsWith(".png")
|| filename.endsWith(".jpeg"))
return true;
return false;
}
}));
/**
* 可以看到文件夾的路徑和圖片的路徑分開保存,極大的減少了內存的消耗;
*/
mAdapter = new MyAdapter(getApplicationContext(), mImgs,
R.layout.grid_item, mImgDir.getAbsolutePath());
mGirdView.setAdapter(mAdapter);
// mAdapter.notifyDataSetChanged();
mImageCount.setText(floder.getCount() "張");
mChooseDir.setText(floder.getName());
mListImageDirPopupWindow.dismiss();
}
}
總結
以上是生活随笔為你收集整理的android仿微信图片上传进度,android高仿微信发布动态(选择图片)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android读取assets中的htm
- 下一篇: android 首页6个碎片,Andro