问题
I had developed an application in which I have to display images from specific folder in sd card. My code is working fine but it is showing all images that are sd card. I had given selection args as my folder's name as it has been metioned the following post: Displaying images from a specific folder on the SDCard using a gridview
But, it didn't worked. Please, suggest me how to do this: Below I am posting my code.
public class Album1Activity extends Activity {
static Cursor imageCursor;
static int columnIndex;
GridView gridView;
Intent in;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
String[] imgColumnID = {MediaStore.Images.Thumbnails._ID};
Uri uri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
imageCursor = managedQuery(uri, imgColumnID, null, null,
MediaStore.Images.Thumbnails.IMAGE_ID);
columnIndex = imageCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(getApplicationContext()));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
String[] dataLocation = {MediaStore.Images.Media.DATA};
//String []dataLocation = {Environment.getExternalStorageDirectory().getAbsolutePath()};
imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
dataLocation, null, null, null);
columnIndex = imageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
imageCursor.moveToPosition(position);
String imgPath = imageCursor.getString(columnIndex);
in = new Intent(getApplicationContext(), FullScreen.class);
in.putExtra("id", imgPath);
startActivity(in);
}//onItemClickListener
});
}//onCreate
public static class ImageAdapter extends BaseAdapter{
private Context context;
//Album1Activity act = new Album1Activity();
public ImageAdapter(Context context){
this.context = context;
}//ImageAdapter
public int getCount() {
// TODO Auto-generated method stub
return imageCursor.getCount();
}//getCount
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}//getItem
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}//getItemId
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv;
if(convertView == null){
iv = new ImageView(context);
imageCursor.moveToPosition(position);
int imageID = imageCursor.getInt(columnIndex);
iv.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
"" +imageID));
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setPadding(5, 5, 5, 5);
iv.setLayoutParams(new GridView.LayoutParams(100, 100));
}//if
else{
iv = (ImageView)convertView;
}//else
return iv;
}//getView
}//ImageAdapter
}//Album1Activity
回答1:
Sorry for Answering late... Below I am posting the final solution
public class Album3 extends Activity {
File [] mediaFiles;
File imageDir;
static GridView gridView;
ImageAdapter adapter;
Intent in;
ImageButton btncam;
String name = null;
ArrayList<Bitmap> bmpArray = new ArrayList<Bitmap>();
ArrayList<String> fileName = new ArrayList<String>();
public static final String TAG = "Album3Activity";
public void onCreate(Bundle savedInstanceState)
{
imageDir = new File(Environment.getExternalStorageDirectory().toString()+
"/diplomat");
super.onCreate(savedInstanceState);
if((imageDir.exists()))
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.grid);
mediaFiles = imageDir.listFiles();
//Log.d("Length of images",""+mediaFiles.length);
for(File file : mediaFiles)
{
bmpArray.add(convertToBitmap(file));
fileName.add(readFileName(file));
//Log.d(TAG + "bmpArray Size", ""+bmpArray.size());
//Log.d(TAG, "call to convertToBitmap");
}//for
adapter = new ImageAdapter(this, bmpArray, fileName);
gridView = (GridView)findViewById(R.id.gridview);
gridView.setAdapter(adapter);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED
, Uri.parse(Environment.getExternalStorageDirectory().toString()+"/diplomat")));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
in = new Intent(getApplicationContext(), FullScreen.class);
in.putExtra("id", position);
startActivity(in);
}
});
}//if
else
{
setContentView(R.layout.no_media);
}
}//onCreate
public static Bitmap convertToBitmap(File file)
{
URL url = null;
try
{
url = file.toURL();
} catch (MalformedURLException e1)
{
//Log.d(TAG, e1.toString());
}//catch
Bitmap bmp = null;
try
{
bmp = BitmapFactory.decodeStream(url.openStream());
//bmp.recycle();
}catch(Exception e)
{
//Log.d(TAG, "Exception: "+e.toString());
}//catch
return bmp;
}//convertToBitmap
public String readFileName(File file){
String name = file.getName();
return name;
}//readFileName
}//class
回答2:
You can use
Uri.parse("path to sdcard folder")
method
回答3:
Your linked answer already has the answer. Use the selection argument correctly.
cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
MediaStore.Images.Media.DATA + " like ? ",
**new String[] {"%myimagesfolder%"},**
null);
回答4:
In the above code ur fetching all the images from gallery try this one
File sdImageMainDirectory ;
String imageName ;
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "MYPICS" + File.separator);
if (!root.exists()) {
root.mkdirs();
}
ImageView camera = (ImageButton) findViewById(R.id.imageButton1);
cam.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
imageName = "MyPic.png";
sdImageMainDirectory = new File(root, imageName);
i++;
Uri imgUri = Uri.fromFile(sdImageMainDirectory);
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
startActivityForResult(intent, 0);
// startCameraActivity();
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) {
case 0:
finish();
break;
case -1:
files = root.listFiles();
count = files.length;
try {
ImageView image = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(
200, 200);
image.setLayoutParams(vp);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
image.setPadding(8, 8, 8, 8);
image.setMaxHeight(50);
image.setMaxWidth(50);
image.setAdjustViewBounds(true);
BitmapFactory.Options option = new BitmapFactory.Options();
option.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeFile(root.getPath() + "/"
+ imageName,option);
image.setImageBitmap(bm);
linearImageView.addView(image);
} catch (Exception e) {
e.printStackTrace();
}
}
}
来源:https://stackoverflow.com/questions/10549417/displaying-images-from-specific-folder-in-sd-card-in-grid-view