how to display images saved in sdcard folder in android
You can get the path of files from a particualr folder as below
Once you get the path of files you ca display the images in gridview
ArrayList<String> f = new ArrayList<String>();// list of file paths
File[] listFile;
public void getFromSdcard()
{
File file= new File(android.os.Environment.getExternalStorageDirectory(),"TMyFolder");
if (file.isDirectory())
{
listFile = file.listFiles();
for (int i = 0; i < listFile.length; i++)
{
f.add(listFile[i].getAbsolutePath());
}
}
}
Remember to add permissionin manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
By having write permission will have read permission by default.
Example
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<GridView
android:id="@+id/PhoneImageGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</RelativeLayout>
gelleryitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:id="@+id/thumbImage" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_centerInParent="true" />
<CheckBox android:id="@+id/itemCheckBox" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
AndroidCustomGalleryActivity.java
public class AndroidCustomGalleryActivity extends Activity {
private int count;
private Bitmap[] thumbnails;
private boolean[] thumbnailsselection;
private String[] arrPath;
private ImageAdapter imageAdapter;
ArrayList<String> f = new ArrayList<String>();// list of file paths
File[] listFile;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getFromSdcard();
GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);
}
public void getFromSdcard()
{
File file= new File(android.os.Environment.getExternalStorageDirectory(),"MapleBear");
if (file.isDirectory())
{
listFile = file.listFiles();
for (int i = 0; i < listFile.length; i++)
{
f.add(listFile[i].getAbsolutePath());
}
}
}
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return f.size();
}
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);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
holder.imageview.setImageBitmap(myBitmap);
return convertView;
}
}
class ViewHolder {
ImageView imageview;
}
}
Snap shot
Displaying images from specific folder in sd card in grid view
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
Android: Display Image from SD CARD
I would rather use a BitmapFactory
to decode the Image from the file-path:
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
jpgView.setImageBitmap(bitmap);
The Docs say:
If the specified file name is null, or cannot be decoded into a
bitmap, the function returns null.
Can you check if the code works with another image and if you can open your image on your PC thought. Maybe the file is corrupt.
How does appear saved SD card images in Android's Gallery?
Your problem lies here:
context.getActivity()
.sendBroadcast(
new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://"
+ Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES))));
You are asking Android to re-index all of the files in Environment.DIRECTORY_PICTURES
.
Asking to scan a whole directory tree is wasteful. In your case, it it even more wasteful, since you are not writing your file to that directory. Instead, you are writing that file to:
root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "folder_name" + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");
Hence, your scan will not pick up this file, which you are writing to some random spot on external storage.
You need to decide the proper place to store your file, then index that one file.
How to show images in list view that are stored in my SD Card particular location
Answer is for reference of the above question and other answer.
Just take a look at LazyList project. Now we make a some modification in it..
MainActivity.java
private String[] mFileStrings;
private File[] listFile;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File file = new File("<Directory path from sdcard>");
if (file.isDirectory())
{
listFile = file.listFiles();
mFileStrings = new String[listFile.length];
for (int i = 0; i < listFile.length; i++)
{
mFileStrings[i] = listFile[i].getAbsolutePath();
}
}
list = (ListView) findViewById(R.id.list);
adapter = new LazyAdapter(this, mFileStrings);
list.setAdapter(adapter);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(listener);
}
in ImageLoader.java
private Bitmap getBitmap(String url)
{
File f = new File(url);
//from SD cache
Bitmap b = decodeFile(f);
if (b != null)
{
return b;
}
return null;
}
How to see the images saved in sd card android
If you are seeing in emulator then go to
DDMS->file Explorer->mnt->Sdcard
and if it is device then
DCIM or Download folder in sd Card...
See this screenshot
Related Topics
When Exactly Are Onsaveinstancestate() and Onrestoreinstancestate() Called
How to Listen for Preference Changes Within a Preferencefragment
Android - Play Sound on Button Click - Null Pointer Exception
Java.Lang.Illegalstateexception: Fragment Not Attached to Activity
Installation Failed with Message Invalid File
How to Create Topic in Fcm Notifications
Download Multiple Files with a Progress Bar in Listview Android
Missing Styles. Is the Correct Theme Chosen for This Layout
Using JSON File in Android App Resources
Position of Dialogfragment in Android
Padding Between Actionbar's Home Icon and Title
Cache.Properties (The System Cannot Find the File Specified)
Eclipse Indigo - Cannot Install Android Adt Plugin
Viewpager Intercepts All X-Axis Ontouch Events. How to Disable