Whats does [position] accomplish?
What does this method accomplish?
URL aURL = new URL(myRemoteImages[position]);
myRemoteImages is a String list, with 4 different variables. And position is a
int position;
EDIT: So if there anyway i could take the place of position with possibly something else?
possibly
myRemoteImages{1,2,3,4};?
EDIT: i am using this to get the the URI for each image in the cahce...
public void getImagesfromCache(){
ImageAdapter adapter = new ImageAdapter();
String[] cachImages = myRemoteImages;
try {
URL aURL2 = null;
aURL2 = new URL(cachImages[position]);
URI imageCacheUri = null;
try {
开发者_C百科 imageCacheUri = aURL2.toURI();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(new File(new File(this.getApplicationContext().getCacheDir(), "thumbnails"),"" + imageCacheUri.hashCode()).exists()){
Log.v("FOUND", "Images found in cache! Now being loaded!");
String cacheFile = this.getApplicationContext().getCacheDir() +"/thumbnails/"+ imageCacheUri.hashCode();
ImageView i = new ImageView(this.getApplicationContext());
FileInputStream fis = null;
try {
fis = new FileInputStream(cacheFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeStream(fis);
i.setImageBitmap(bm);
putBitmapInDiskCache(imageCacheUri, bm);
Log.v("Loader", "Image saved to cache");
((Gallery) findViewById(R.id.gallery))
.setAdapter(new ImageAdapter(MainMenu.this));
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
As you see where position is used, how could i go about another way to return the URL's to get the images in the cache?
Could i possibly store each URI in a SharePreference and fetch them in onCreate()??
It creates a URL object, from the url string specified by the element within the array (myRemoteImages) at the specific position.
The following syntax creates a URL object
URL aURL = new URL(<a string describing a URL>);
The following syntax gets the String value at the specified position
myRemoteImages[position]
So, to get a URL for each position, you could do
URL url1 = new URL(myRemoteImages[0]);
URL url2 = new URL(myRemoteImages[1]);
URL url3 = new URL(myRemoteImages[2]);
URL url4 = new URL(myRemoteImages[3]);
It would be far better though to have an array of URLs though. So it would be
URL [] urls = new URL[myRemoteImages.length];
for (int i = 0; i < myRemoteImages.length; i++) {
urls[i] = myRemoteImages[i];
}
With this array of URLs, even though you know there are only 4 images, it can be extended easily without having to change any code. Hard coding is VERY bad, and will usually lead to errors in the long run.
There are no list
variables in Java only List objects. This is an array and position is the value of the offset from the beginning of the array. Arrays are different than lists such as those found in Python and other languages. Arrays are of fixed size and are objects.
Let's say that position
is an int with the value of 3. The code you posted will then look like this when executed:
URL aURL = new URL(myRemoteImages[3]);
myRemoteImages[3]
means: give me the third item in the String list myRemoteImages
. new URL()
will create a new instance of URL with that third item as the spec parameter.
position
is the element in the array (not list) to access. If you have 4 strings then position should be an integer between 0 and 3.
myRemoteImages[0]
will be the first image, and myRemoteImages[3]
will be the fourth and last.
You are creating a new URL
from an element of an array, probably an array of String
items.
How is myRemoteImages
declared?
myRemoteImages
must be an array, not a List.
That is the java syntax for accessing the element of an array at that index.
精彩评论