android


Populate Android Gallery from image file paths?


I have some images that are saved to a directory on the Android device when the application starts--I would like to be able to display these images in a Gallery, but so far I haven't been able to do it.
I was following the sample Gallery code here, but it uses drawable resource IDs instead of file paths. I found this solution that is similar to what I'm looking for, except it uses ImageView instead of Gallery.
So the code using ImageView would look something like this:
File imgFile = new File(“/data/data/com.myproject.example/files/someImage.png”);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
The above code works, but I'm not sure how to do it with a Gallery. I have been searching for answers and trying different things, but I'm v new to Android development and I feel like I'm in a bit over my head.
Any help is appreciated. Thank you in advance.
Basically I think you just need to put your two examples together.
Use the HelloGallery example as a start, however you want to change the code inside the getView() method of the ImageAdapter to call setImageBitmap() on the ImageView instead of setImageResource().
You will need an array/collection of file paths of images you want to load.
What you need to do is something like this:
public ImageAdapter(Context c, int itemId) {
context = c;
imgArr = GlobalStore.getItem(itemId).getPhotos();
TypedArray attr = context.obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = attr.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
attr.recycle();
}
as you can see this is basically a copy from Gallery tutorial. In the constructor imgArr variable is loaded with an array of JPG file names. These were for example read from a database.
Then in the getView function you have something like this...
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
String tmpStr = appContext.getFilesDir() + File.separator + "photos" + File.separator + imgArr.get(position);
Bitmap bitmap = BitmapFactory.decodeFile(tmpStr);
imageView.setImageBitmap(bitmap);
imageView.setLayoutParams(new Gallery.LayoutParams(350, 300));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
As you can see getFilesDir() gets your applications data location where it stores files, then let's imagine all photos are in "photos" directory, you build a path and attach a file name from the imgArr array. Since this is called for every photo you just use the passed position variable.
If you don't have an array of photos then maybe the way is to build it by reading the directory where you store photos, load all of the filenames in an array and then do this.
Then you do the rest on the gallery side as in the gallery tutorial.

Related Links

SQLite : Insert Into Query with Where Clause is not working
NullPointer Exception while trying to update sql row
Translucent navigation + Low profile
How to parse this json string? [duplicate]
Android app setting linearlayout with 2 columns for inflation
How to perform AsyncTask for checking internet connection
Android setBackground on Animation end doesn't works
Parsing Yahoo Rss Feeds for android
share video link and play inline in facebook
Exending YouTubeBaseActivity throws java.lang.ClassCastException:
android json Asynctask- happen Android runtime error
Start a service when phone reboot completed
How to increase FPS in Android Game?
Sencha touch 2 load elements when scroll down
Merge remaining views with animation after deleting one of the view
How to abort upload image into server while it's in progress in android?

Categories

HOME
eclipse
eslint
google-spreadsheet
mstest
solver
data-visualization
nfs
mql5
google-schemas
gson
mod-security
stellar.js
plink
selectize.js
scsi
null-pointer
intershop
boost-icl
cgbitmapcontext
displaytag
beta
draft-js-plugins
surveymonkey
outliers
8051
handler
angular-translate
quantlib
comparable
ntp
jbutton
sendbird
python-import
modalviewcontroller
batching
swagger-php
xcode7.3
sessionstorage
loading
metaprogramming
metawidget
chef-solo
signal-strength
facebook-social-plugins
optimizely
xbmc
opennms
lampp
dtsearch
carrot2
context-switch
spring-data-couchbase
ngmaterial
dt
libjpeg
cd-burning
microstation
jodd
ng-grid
simevents
gcloud-ruby
yajsw
halcon
design-principles
atan2
r-package
forwarding
django-urls
dock
mod-proxy
rdflib
mirror
avahi
dnssec
infosphere-spl
rapidsvn
at-job
showdialog
entry-point
conditionaltagsupport
git-repo
syndicationfeed
gwt-openlayers
modular
cocoalibspotify-2.0
script#
beaker-testing
workitem
facebook-timeline
wp7test
preload
joomla3.1
dopostback
tree-balancing
nokia-s40
subgraph
virtualpathprovider
procedures
creole
opml
duplex
echo3
linfu-dynamicproxy
entitykey
unmanagedresources
online-storage
blind

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App