我使用此tutorial从图库加载图片并使用CheckBox在网格中显示。我想更改此代码以从/data/data/com.myexample.myfiles/files/
加载图片
我怎样才能实现它?请帮帮我。
import android.app.Activity;
public class AndroidCustomGalleryActivity extends Activity {
private int count;
private Bitmap[] thumbnails;
private boolean[] thumbnailsselection;
private String[] arrPath;
private ImageAdapter imageAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.thumbnails = new Bitmap[this.count];
this.arrPath = new String[this.count];
this.thumbnailsselection = new boolean[this.count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
arrPath[i]= imagecursor.getString(dataColumnIndex);
}
GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);
imagecursor.close();
final Button selectBtn = (Button) findViewById(R.id.selectBtn);
selectBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
final int len = thumbnailsselection.length;
int cnt = 0;
String selectImages = "";
for (int i =0; i<len; i++)
{
if (thumbnailsselection[i]){
cnt++;
selectImages = selectImages + arrPath[i] + "|";
}
}
if (cnt == 0){
Toast.makeText(getApplicationContext(),
"Please select at least one image",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"You've selected Total " + cnt + " image(s).",
Toast.LENGTH_LONG).show();
Log.d("SelectedImages", selectImages);
}
}
});
}
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(
R.layout.galleryitem, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkbox.setId(position);
holder.imageview.setId(position);
holder.checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v;
int id = cb.getId();
if (thumbnailsselection[id]){
cb.setChecked(false);
thumbnailsselection[id] = false;
} else {
cb.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
holder.imageview.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");
startActivity(intent);
}
});
holder.imageview.setImageBitmap(thumbnails[position]);
holder.checkbox.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
}
class ViewHolder {
ImageView imageview;
CheckBox checkbox;
int id;
}
}
答案 0 :(得分:0)
下面是可用于从任何sdcard文件夹加载图像的完整代码。我使用UniversalImageLoader库进行图像加载。
公共类GalleryScreenActivity扩展了SherlockActivity {
private DisplayImageOptions options;
private ImageAdapter imageAdapter;
private GridView gridView;
protected ImageLoader imageLoader = ImageLoader.getInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_Custom);
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_gallery_screen);
options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.image_for_empty_url)
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
gridView = (GridView) findViewById(R.id.gridview);
imageAdapter = new ImageAdapter();
gridView.setAdapter(imageAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startImageGalleryActivity(position);
}
});
gridView.setOnScrollListener(new PauseOnScrollListener(true, true));
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
if(Data.array == null){
getImageStrings();
imageAdapter.notifyDataSetChanged();
}
gridView.setVisibility(View.VISIBLE);
if(Data.array.size() == 0)
gridView.setVisibility(View.INVISIBLE);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
public void getImageStrings(){
File sdDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),Constants.PICTURE_SAVE_FOLDER);
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (!sdDir.exists()) {
if (!sdDir.mkdirs()) {
Log.d(TAG, "failed to create directory");
Toast.makeText(getApplicationContext(), "SD CARD ERROR: FIX IT AND TRY AGAIN", Toast.LENGTH_LONG).show();
}else{
}
}
File[] sdDirFiles = sdDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
return filename.contains(".jpg") || filename.contains(".png");
}
});
String tempUrl;
if(Data.array == null)
Data.array = new LinkedList<String>();
if(sdDirFiles != null)
for(File singleFile : sdDirFiles)
{
tempUrl = singleFile.getAbsolutePath();
Log.d("FILE", "file://"+tempUrl);
Data.array.add("file://"+tempUrl);
}
}
private void startImageGalleryActivity(int position) {
Intent intent = new Intent(this, GalleryDetailScreenActivity.class);
Data.galleryDetailPosition = position;
startActivity(intent);
overridePendingTransition(R.anim.appear_right, R.anim.hide_left);
finish();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Data.resetData();
overridePendingTransition(R.anim.appear_left, R.anim.hide_right);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.home:
finish();
overridePendingTransition(R.anim.appear_left, R.anim.hide_right);
break;
}
return true;
}
public class ImageAdapter extends BaseAdapter {
@Override
public int getCount() {
if(Data.array == null)
return 0;
return Data.array.size();
}
@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 FrameLayout imageLayout;
if(convertView == null)
imageLayout = (FrameLayout) getLayoutInflater().inflate(R.layout.activity_gallery_screen_item, null);
else
imageLayout = (FrameLayout) convertView;
final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
imageView.setImageBitmap(null);
final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
imageLoader.displayImage(Data.array.get(position), imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted() {
spinner.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(FailReason failReason) {
String message = null;
switch (failReason) {
case IO_ERROR:
message = "Input/Output error";
break;
case OUT_OF_MEMORY:
message = "Out Of Memory error";
break;
case UNKNOWN:
message = "Unknown error";
break;
}
Toast.makeText(GalleryScreenActivity.this, message, Toast.LENGTH_SHORT).show();
spinner.setVisibility(View.GONE);
imageView.setImageResource(android.R.drawable.ic_delete);
}
@Override
public void onLoadingComplete(Bitmap loadedImage) {
spinner.setVisibility(View.GONE);
}
});
return imageLayout;
}
}
}
答案 1 :(得分:0)
`文件yourDir =新文件(&#34; data / data / com.myexample.myfiles / files /&#34;);
yourDir.mkdir();
enter code here
ArrayList<GalleryHelper> dataT = null;
dataT = new ArrayList<GalleryHelper>();
for (File f : yourDir.listFiles()) {
if (f.isFile()) {
imageLoader.displayImage("data/data/com.myexample.myfiles/files" + f.getName(), imgSinglePick);
GalleryHelper item = new GalleryHelper();
item.sdcardPath = "data/data/com.myexample.myfiles/files" + f.getName();
dataT.add(item);
}
}
imgAdapter.addAll(dataT);
我的画廊助手类是:
公共类GalleryHelper {
public String sdcardPath;
public File file;
public boolean isSeleted = false;
}