File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Android and the fly likes How to display Images stored on sdcard? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "How to display Images stored on sdcard?" Watch "How to display Images stored on sdcard?" New topic
Author

How to display Images stored on sdcard?

Prajkti Khadse
Ranch Hand

Joined: Apr 02, 2009
Posts: 52
hi all,
I've some images stored on sdcard and through my Application i want to display that images on a Activity

I can retrieve the Image (File) Absolute path but not getting how to give that image to Display....??!!!

can anybody please please help me out...???!!!
Pratik Goswami
Ranch Hand

Joined: Mar 02, 2009
Posts: 136

Hi Prajakti

Use Content Resolver and use Media Store class, you will get all images from external media inside a cursor.

Thanks & Regards
Pratik Goswami


iOS * Android Developer
Prajkti Khadse
Ranch Hand

Joined: Apr 02, 2009
Posts: 52
Thanks Pratik,

But can you please provide some sample code???
i'm not getting any clue how to use that Content Resolver and Media Store class....
Pratik Goswami
Ranch Hand

Joined: Mar 02, 2009
Posts: 136

Hi Prajakti,

I have never displayed those images but once I have deleted those using Content Resolver. Here is the code to get all images in cursor.


After getting cursor take get id of each and every image and then set it to image view.



Thanks & Regards
Pratik Goswami.
Prajkti Khadse
Ranch Hand

Joined: Apr 02, 2009
Posts: 52
Thanks a lot Pratik,
Pratik Goswami
Ranch Hand

Joined: Mar 02, 2009
Posts: 136

Hi prajakti,

Are you able to get cursor of images those are stored in Phone memory? I have pushed images inside /Data/data folder but no result at all. Cursor doesn't fetch anything. Am I doing something wrong?

Thanks & Regards
Pratik Goswami
Prajkti Khadse
Ranch Hand

Joined: Apr 02, 2009
Posts: 52
Hi Pratik,
Actually, i didn't use cursor....

Here's m the code i'm working with...
Hope it'd help you........


Pratik Goswami
Ranch Hand

Joined: Mar 02, 2009
Posts: 136

Hi Prajakti,

Thanks for your reply, I will check out this code soon.

Where are you pushing your images? In /Data/data folder? or at somewhere else? Earlier I developed ordinary File IO method to search all image files. While doing so I was not able to get images in Data folder, as we have no access right on /Data folder.

Edit- While searching images i was taking "/" as root directory. I had to push images in /dev folder then and then I was able to get images.

Thanks & Regards
Pratik Goswami

beta jane
Greenhorn

Joined: Oct 07, 2009
Posts: 14
public class demo extends Activity {

private Uri[] mUrls;
String[] mFiles=null;

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.images);

File images = Environment.getDataDirectory();
File[] imagelist = images.listFiles(new FilenameFilter(){
@Override
public boolean accept(File dir, String name)
{
return ((name.endsWith(".jpg"))||(name.endsWith(".png")));
}
});

mFiles = new String[imagelist.length];

for(int i= 0 ; i< imagelist.length; i++)
{
mFiles[i] = imagelist[i].getAbsolutePath();
}
mUrls = new Uri[mFiles.length];

for(int i=0; i < mFiles.length; i++)
{
mUrls[i] = Uri.parse(mFiles[i]);
}

Gallery g = (Gallery) findViewById(R.id.addToDictionary);
g.setAdapter(new ImageAdapter(this));
g.setFadingEdgeLength(40);

}
public class ImageAdapter extends BaseAdapter{

int mGalleryItemBackground;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount(){
return mUrls.length;
}
public Object getItem(int position){
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
ImageView i = new ImageView(mContext);

i.setImageURI(mUrls[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(260, 210));
return i;
}
private Context mContext;
}
}


Hi,this a modified code to display images.
but i get this error that images in layout is not resolved which is highlighted in red


Please help
Thanks in advance
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

Hi,

Welcome to JavaRanch!

A few things:

1. Do you have an images.xml file in your layout folder?
2. Can you check your imports? The imported "R.java" file should belong to one in your package; the import statement should not read import android...R;

You are getting the error because the compiler cannot find the images file.

Note: please use Code tags when posting code; the post becomes easier to read.

[List of FAQs] | [Android FAQ] | [Samuh Varta]
beta jane
Greenhorn

Joined: Oct 07, 2009
Posts: 14
hi yeah i have an images.xml file

you asked me to check my imports.

It is like this:
import android.R;

What should i do?

Thanks
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

There would be a file created automatically on sucessful build of your workspace. This file is named "R.java" and is contained within "gen" folder.
You need to import your version of the file in the namespace.

Replace the import android.R; line
with : <you app pckg name>.R;
beta jane
Greenhorn

Joined: Oct 07, 2009
Posts: 14
Hi errors are rectified but iam not getting output.
If the emulator is launched its force closed
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

You should start another thread and post your exception messages along with your code there.
Thay way people would be able to help you.
beta jane
Greenhorn

Joined: Oct 07, 2009
Posts: 14



My program runs fine.When emulator is launched its force closed
beta jane
Greenhorn

Joined: Oct 07, 2009
Posts: 14
please respond
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

The problem could be any thing: with the code, or missing permissions in the manifest. It is hard to pin point the problem unless you post the exception trace.
As I already said, it would be better if you could start a new thread and paste your code along with dump of the exception.

Note: If you are using Eclipse with ADT plugin, you can open "Logcat" view to see the logged exception messages.

Mahivardhan Singh
Greenhorn

Joined: Sep 25, 2012
Posts: 2
beta jane wrote:public class demo extends Activity {

private Uri[] mUrls;
String[] mFiles=null;

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.images);

File images = Environment.getDataDirectory();
File[] imagelist = images.listFiles(new FilenameFilter(){
@Override
public boolean accept(File dir, String name)
{
return ((name.endsWith(".jpg"))||(name.endsWith(".png")));
}
});

mFiles = new String[imagelist.length];

for(int i= 0 ; i< imagelist.length; i++)
{
mFiles[i] = imagelist[i].getAbsolutePath();
}
mUrls = new Uri[mFiles.length];

for(int i=0; i < mFiles.length; i++)
{
mUrls[i] = Uri.parse(mFiles[i]);
}

Gallery g = (Gallery) findViewById(R.id.addToDictionary);
g.setAdapter(new ImageAdapter(this));
g.setFadingEdgeLength(40);

}
public class ImageAdapter extends BaseAdapter{

int mGalleryItemBackground;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount(){
return mUrls.length;
}
public Object getItem(int position){
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
ImageView i = new ImageView(mContext);

i.setImageURI(mUrls[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(260, 210));
return i;
}
private Context mContext;
}
}


Hi,this a modified code to display images.
but i get this error that images in layout is not resolved which is highlighted in red


Please help
Thanks in advance
Mahivardhan Singh
Greenhorn

Joined: Sep 25, 2012
Posts: 2
Monu Tripathi wrote:Hi,

Welcome to JavaRanch!

A few things:

1. Do you have an images.xml file in your layout folder?
2. Can you check your imports? The imported "R.java" file should belong to one in your package; the import statement should not read import android...R;

You are getting the error because the compiler cannot find the images file.

Note: please use Code tags when posting code; the post becomes easier to read.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to display Images stored on sdcard?