我正在使用Universal Image loader(UIL)开发相机应用程序。
我遇到的问题是拍照后(它会存储在SD卡中),gridFragment不会刷新SD卡,因此不会显示新图片。
有什么想法吗?代码如下。谢谢你的阅读!
public class MainActivity extends AppCompatActivity {
private final int CAMERA_CODE = 100;
private DisplayImageOptions options;
private String[] imageurl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String ExternalStorageDirectoryPath = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)+ File.separator;
String targetPath = ExternalStorageDirectoryPath + "MyCameraApp/";
int frIndex = getIntent().getIntExtra(Constants.Extra.FRAGMENT_INDEX, 0);
Fragment fr;
String tag;
int titleRes;
tag = "hi";
fr = getSupportFragmentManager().findFragmentByTag(tag);
if (fr == null) {
fr = new ImageGridFragment();
fr.setArguments(getIntent().getExtras());
}
titleRes = R.string.app_name;
setTitle(titleRes);
getSupportFragmentManager().beginTransaction().replace(android.R.id.content, fr, tag).commit();
ImageLoader.getInstance()
.init(ImageLoaderConfiguration.createDefault(MainActivity.this));
imageurl = ImageGridFragment.getimages();
//ImageLoader.getInstance().displayImage(imageurl,MainActivity.this);
}
public class ImageGridFragment extends AbsListViewBaseFragment {
public static final int INDEX = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main, container, false);
listView = (GridView) rootView.findViewById(R.id.grid);
((GridView) listView).setAdapter(new ImageAdapter(getActivity()));
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startImagePagerActivity(position);
}
});
return rootView;
}
public static String[] getimages() {
String[] images=new String[175];
String ExternalStorageDirectoryPath = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)+ File.separator;
String targetPath = ExternalStorageDirectoryPath + "MyCameraApp/";
int counter=0;
String name;
File yourDir = new File(ExternalStorageDirectoryPath, "MyCameraApp/");
for (File f : yourDir.listFiles()) {
if (f.isFile()){
name = "file:///"+targetPath + f.getName().toString() ;
images[counter]=name;
// make something with the name
counter++;}
}
return images;
}
public static String[] IMAGE_URLS = getimages();
private static class ImageAdapter extends BaseAdapter {
String ExternalStorageDirectoryPath = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)+ File.separator;
String targetPath = ExternalStorageDirectoryPath + "MyCameraApp/";
//private List<File> files = getListFiles(new File(targetPath));
private LayoutInflater inflater;
private DisplayImageOptions options;
ImageAdapter(Context context) {
inflater = LayoutInflater.from(context);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_action_name)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_err)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
}
@Override
public int getCount() {
return IMAGE_URLS.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.item_grid_image, parent, false);
holder = new ViewHolder();
assert view != null;
holder.imageView = (ImageView) view.findViewById(R.id.image);
holder.progressBar = (ProgressBar) view.findViewById(R.id.progress);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
String imgPath = targetPath+"";
String decodedImgUri = Uri.fromFile(new File(imgPath)).toString();
ImageLoader.getInstance()
.displayImage(IMAGE_URLS[position], holder.imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
holder.progressBar.setProgress(0);
holder.progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
holder.progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder.progressBar.setVisibility(View.GONE);
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
holder.progressBar.setProgress(Math.round(100.0f * current / total));
}
});
return view;
}
}
static class ViewHolder {
ImageView imageView;
ProgressBar progressBar;
}
}