How display images using Imageloader on ImageView from sdcard path? - android

I have downloaded all images in sdcard from URL. Now I want to display images in GridView using ImageLoader but I am unable to do this.
My image sdcard path like : /mnt/sdcard/DPImages/image_product3.jpg

private String[] mFileStrings;
private File[] listFile;
public void getFromSdcard()
{
File file= new File(android.os.Environment.getExternalStorageDirectory(),"DPImages");//sdcard/DpImages
if (file.isDirectory())//if files is directory
{
listFile = file.listFiles();
mFileStrings = new String[listFile.length];
for (int i = 0; i < listFile.length; i++)
{
mFileStrings[i] = listFile[i].getAbsolutePath();//mFileStrings with uri of images
}
}
}
You can use the mFileStrings to display the images in gridview.
In your getView of adapter.
ImageView image=(ImageView)convertView.findViewById(R.id.imageview);
Bitmap b = BitmapFactory.decodeFile(mFileStrings[position]);
image.setImageBitmap(b);

I'm using universal image downloader.
You can show the image with this:
String imageUri = "file:///mnt/sdcard/image.png"; // from SD card
imageLoader.displayImage(imageUri, imageView);
The link is this. https://github.com/nostra13/Android-Universal-Image-Loader

Related

how to display image file save on internal storage in android

i have saved drawable images in internal storage by compressing them in bitmap format. the problem is that i want to retrieve those image files and wants to display them in grid view i am able to list the total no of files but i am not able to display those files in image view. here is my code
Intent i = getIntent();
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.SingleView);
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
File[] imageList = directory.listFiles();
if(imageList == null){
imageList = new File[0];
}
Log.i("My","ImageList Size = "+imageList.length);
imageView.setImageResource(imageAdapter. (....?) );
set Image(From Sd card) to imageview get the path of the image which is in sdcard. and you can assign it by creating Bitmap
String filePath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + File.separator + "your_image_name.png";
Bitmap bmp = BitmapFactory.decodeFile(filePath);
imageView.setImageBitmap(bmp);
Another way to set imageview you can also use third party library also which may have resize and Scaling options too. like ImageLoader

how to display images using filename in android

i am using imageAdapter class in which i have an array that stores drawables
public Integer[] mThumbIds = {
R.drawable.blue, R.drawable.floral,
R.drawable.bluefloral };
in first activity when user click on save button i have saved those images in android internal memory like data/ data/ com.myapp.color , i have get the file name, and file path too and passed it to imageAdapter class i just wanted to know that through this file name and path how can i save these images in to this array. because through this array i am displaying images in gridview.
If the image inside the internal or external storage you can load it by this:
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
If the image inside the drawable folder inside the apk you can use:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.drawable_name);
If the image inside the assets folder you can use:
InputStream inputStream = getAssets().open("image_name.jpg");
Drawable drawable = Drawable.createFromStream(inputStream, null);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageDrawable(drawable);
If the image inside drawable folder and you have only the name you can get the resource id by using:
int drawableResourceId = this.getResources().getIdentifier("image_name_without_extension", "drawable", this.getPackageName());
This is how you can get the image id's using their file names. Using this id you can load the image in image view.
If the image name is my_image this method would return id value associated with R.id.my_image
public static int getImageIDFromName(String imageName)
{
int imageID = 0;
if(imageName == null
|| imageName.equalsIgnoreCase(""))
{
return 0;
}
try
{
#SuppressWarnings("rawtypes")
Class res = R.drawable.class;
Field field = res.getField(imageName);
imageID = field.getInt(null);
}
catch(Exception e)
{
}
return imageID;
}

Android: How to put an image file from sd card to HashMap with simpleadapter?

I am reading files from the selected folder on my phone like you can see in the following code.
And how could I get this work with an Imagefile?
At the end I like to have an imagelist with a preview of every image.
Like that:
[IMG] (imgview) - [Filename] (String)
for(int i=0; i < files.length; i++) {
File file = files[i];
map = new HashMap<String, String>();
if(!file.isHidden() && file.canRead()) {
path.add(file.getPath());
if(file.isDirectory()) {
map.put("img_list", ""+R.drawable.folder);
map.put("string_cell", file.getName()+"/");
your_array_list.add(map);
}else{
ImageFileFilter filefilter = new ImageFileFilter(file);
if(filefilter.accept(file)){
//Its an imagefile
// ==> I like to replace the ""+R.drawable.image with the file that I have read
map.put("img_list", ""+R.drawable.image);
} else {
//Its not an image file
}
map.put("string_cell", file.getName());
your_array_list.add(map);
}
}
}
SimpleAdapter mSchedule = new SimpleAdapter(this, your_array_list, R.layout.connected_upload_row,
new String[] {"img_list", "string_cell"}, new int[] {R.id.img_list, R.id.string_cell});
list.setAdapter(mSchedule);
In the following picture I like to replace the white "image" picture with the original picture called "41786486733.jpg" as preview.
So that the user can see what picture this is...
EDIT FLORIAN PILZ
if(filefilter.accept(file)){
Log.v("PATH1", file.getPath() );
ImageView myImageView = (ImageView) findViewById(R.id.img_list);
Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());
myImageView.setImageBitmap(bmp);
}
Looking at the picture, and assuming that the white image with the text "IMAGE" is an ImageView instance, then simply...
Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath);
myImageView.setImageBitmap(bmp);
Or did I completely misunderstood your question.
Anyhow this small example tries to do something similar, what your are trying to accomplish.
There is a similar question with an answer accepted. Check: Cannot open image file stored in sdcard
Snippet from there:
File directory = new File(extStorageDirectory, "myFolder");
File fileInDirectory = new File(directory, files[which]);
Bitmap bitmap = BitmapFactory.decodeFile(fileInDirectory.getAbsolutePath());
You have to implement custom listview check this example and seem storing path in hashmap creating issue,it will more helpful if you provide error log cat! anyhow can you try below trick to achieve you goal instead storing path in hashmap?,hope it will help to you.
File file = new File(Environment.getExternalStoragePath()+"/Folder/");
file imageList[] = file.listFiles();
for(int i=0;i<imageList.length;i++)
{
Log.e("Image: "+i+": path", imageList[i].getAbsolutePath());
Bitmap bitmap = BitmapFactory.decodeFile(imageList[i].getAbsolutePath());
myImageView.setImageBitmap(bitmap);
the solution for my problem is here:
How to show thumbnail from image path?
I had to create a customlistview (with help from ρяσѕρєя K) THANKS!!
create an custom adapter by extended baseadapter class instead of SimpleAdapter . as i think this will be easy or also you can create more custom UI instead of using inbuild

How to delete a bitmap image which is stored on the sd card in android

In my application I have a grid view of images and when a user clicks an image then it will open the image in fullscreen. The images are loaded from the SD card as follows:
File sdDir = new File("mnt/sdcard/Pictures");
File[] sdDirFiles = sdDir.listFiles();
for(File singleFile : sdDirFiles) {
String filePath = singleFile.getAbsolutePath();
Bitmap bmp = scaleBitmap(filePath);
photos.add(bmp);
}
mThumbIds = photos.toArray(new Bitmap[(photos.size())]);
}
Scale bitmap is a method which decodes each file into a bitmap and then scales the bitmap before returning it.
I then have another Activity which loads the images fullscreen once they have been clicked. I have a menu button "Delete", from which I would like to delete the file on the sdcard which represents the bitmap I see on the screen.
The problem I have is that there is no way to get the file name from the Bitmap object, so therefore I cannot delete the file.
Any help will be greatly appreciated.
You could extend the Bitmap class and add a filename field. Or, you could pass the filename to your new activity in the intent bundle.

Getting ID of bitmap on SD card

My app has a section where some images are displayed in a gridview.
Before the gridview is set up, the following is done:
Download list of image file names from the internet
Compare this list to items already present in the Drawable folder
If the image is not present in the Drawable folder, download it and save it to the SD card.
When setting up the gridview I do it by using the drawable IDs, which are easy to get as they are in a resource folder ("drawable").
But if I need to include the SD card images as well in the gridview, how can I retrive their IDs?
Files anywhere other than those packaged with your app (downloaded to the internal or external storage, for instance) don't have resource ids.
Resource ids are a shorthand way of referencing the packaged resources such as strings, images, whatever. These ids are created at build-time and only have meaning for internal resources.
In order to use images obtained from an external source, you access them by filename and, I presume, you'll then create ImageViews which are added to your GridView.
If you need to discern between the different images of the grid (perhaps for touch/click purposes) then it's the ImageView that you deal with rather than being concerned with resource ids or filenames of the images they contain.
I was having a code to get images from sdcard and convert them into an array of URIs, the ids are like URIs, use them.
private void getData() {
File f = new File("/mnt/sdcard/");
File[] imagelist = f.listFiles(new FilenameFilter(){
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]);
}
}
You can use the uri like this:-
ImageView imageView=new ImageView(this);
imageView.setImageURI(mUrls[i]);

Categories

Resources