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

How can get EditText from a class?
ViewHolder not setting the string as text of a TextView. Please see details
Store Images from URL in Array issue
How to handle YouTubePlayer.PlayerStateChangeListener.onError
Google IAB bizarre flow change
Distinguishing between same named values in android autocomplete view
Android : Class not found when View contains R.syleable
Change Navigation Drawer background color for the selected item
Android: How to get the Checked items of checkbox in RecyclerView to send it to server
Cannot see toolbar in lollipop and higher
How to put the title of the Activity on the right of the toolbar
Android React Native Height 0 on Animated.View
Per app proxy/'port forwarding'(iOS or Android)
Nesting classes with Typescript
Call SOAP service from android with ksoap2
Xamarin: Error executing task ConvertResourcesCases: Path is invalid

Categories

HOME
sonarqube
jmeter
wakanda
phpmyadmin
alexa-skills-kit
themes
composite-primary-key
socket.io
devise
edge
yandex
big-o
nested-if
flume
fabric.io
commonjs
appcelerator-studio
mod-security
line
x-cart
tput
pybossa
plink
bitnami
mathprog
feathersjs
fileserver
sse
connector
visual-glitch
rubymotion
ember-cli
r-mice
dapper-extensions
expressionengine3
oculus
wcag
datastax-enterprise-graph
tizen-tv
jaws
scatter3d
sql-server-2000
database-comparison
catch-unit-test
android-maps-extensions
dynamic-jasper
sharding
indy10
imx6
xajax
python-pptx
wurfl
android-augmented-reality
ods
demo
xv6
odoo-website
dbnull
glm-math
chronicle-map
bindingnavigator
xmldocument
oim
dapper-simplecrud
dynamic-linq
manova
modelsim
boxing
truezip
mach-o
cmp
builtins
actor-platform
pacemaker
tinkerpop3
qiime
boost-serialization
lz77
thread-sleep
user-management
jaunt-api
libvlc
sciruby
jdi
zen-cart
doctype
zscript
jsvc
bufferedinputstream
uvc
jquery-forms-plugin
showdialog
execve
filesplitting
metatrader5
cocos3d
nsmenu
gii
getopt
ebay-lms
font-awesome-4.0.0
sqlclr
redis-py
boost-msm
kqueue
isa-swizzling
cgimage
mysql-error-1205
django-paypal
cac
jqueryform
message-passing
isapi-extension
sifr
pagemethods
echo3
web-application-project
mathematical-notation
alsb
httpcookie

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