• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to display Images stored on sdcard?

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...???!!!
 
Ranch Hand
Posts: 136
Android Mac Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Prajkti Khadse
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 136
Android Mac Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Pratik,
 
Pratik Goswami
Ranch Hand
Posts: 136
Android Mac Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 136
Android Mac Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
beta jane
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi errors are rectified but iam not getting output.
If the emulator is launched its force closed
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



My program runs fine.When emulator is launched its force closed
 
beta jane
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please respond
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic