How to refresh current picture from Gallery android - android

After searching an trying how to do it, I put this question due to my failures.
The point is I am trying to show in a Gallery several pictures that I get from a http server. The thing is when I open the activity for first time, if the picture is not in a specific folder, local cache, I display a default picture and them I launch a download of the picture by a AsyncTask. What I try is as soon as the picture has been downloaded and save in the folder, to update/refresh the gallery in the activity to display the picture. If I slip left/right the gallery the picture appear when the gallery is refresh, but I want this happen avoiding to slip the gallery.
Also comment that the base adapter is all the time with the right reference to the picture, the default picture is selected if the file does not exist in the folder.
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageBitmap(getPicturePath(position + 1));
i.setLayoutParams(new Gallery.LayoutParams(wHeight / 3, wHeight / 4));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
private Bitmap getPicturePath(int picture) {
String picturePath = Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ "/pictures/" + picture
+ ".jpg";
File f = new File(picturePath);
if (f.exists())
return BitmapFactory.decodeFile(picturePath);
else
return BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.no_image);
}
I try to invalidate() in the gallery and also notifyDataSetChanged() in the adapter, baseadapter, but it does not work, the pictures on the screen do not change.
Any suggestion how to solve this?
Many thanks.

Related

Read and load images from SDcard asynchronously - fail

I'd like to load image which is on SDCARD in folder to imageView of my cell in listView. I've tried different solutions but each of them fails. When I load images normally as it is in every base tutorial everything works. I've observed that, my application slows down when it has to load many images. Images are taken from photo camera of device. I'd like to load each of them asynchronously to avoid UI slow reaction. I've tried to use Thread, Asynctask but each of them throws error: "Only the oryginal thread that created a view hierarchy can touch its views". How to load images to avoid speed problems? SDImageLoader is a class which is possible to get from GITHUB. What I've tried is a standard code but is slows:
In a getView method in ListAdapter:
File imageFile = new File(Options.PATH_TO_FOLDER_PHOTOS_OF_APP + "test.png");
String imageFileString = Options.PATH_TO_FOLDER_PHOTOS_OF_APP + "test.png";
// "test.png" is a test file. Each cell will have different name of file to load.
if(imageFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
image.setImageBitmap(myBitmap);
// final SDImageLoader loader = new SDImageLoader(context);
// new SDImageLoader().load(imageFileString, image);
//UrlImageViewHelper.setUrlDrawable(image, url);
}
else
{
final SDImageLoader loader = new SDImageLoader();
Resources resources = context.getResources();
int resurceId;
resurceId = resources.getIdentifier("bad", "drawable",context.getPackageName());
loader.load(imageFileString, image);
image.setImageResource(resurceId);
}
Have you tried to refresh your project after adding an external library to your project? It doesn't matter with the fragment. You send exact context to the List Adapter - which should be fragment.this.getActivity().

load all images from internal memory to a viewPager android

i have designed an app to show images in a view Pager, now the thing is that user saves this images to its internal memory and the app gives a random no. to the image as name, upon clicking "View Favorite " button the user gets to view all the images in a view pager one by one, can any1 help me as to how i go about it??
private void loadImageFromStorage(String path)
{
try {
File f=new File(path, "image.png");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
Context context =getApplicationContext();
final ImageView imageView = new ImageView(getApplicationContext());
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setImageBitmap(b);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
this is the code to load the image but the thing is i need to load all the images not just one image.png but everything.png ;) how do i do it??
and also i wanna load with Picasso but i cant load the bitmap it says something like load is not for bitmap etc. etc. please help
I don't know if you have already solved this issue. But if you don't give your files a random number when you save them, you could give them sequential numbers like 1.jpg, 2.jpg .... then you could do a while loop to load each image. For example:
int counter = 0;
boolean imageExists = true;
while(imageExists)
{
File imageFile = new File (filePath + counter + ".jpg");
if(imageFile.exists())
{
Picasso.with(getBaseContext()).load(imgFile).fit().centerInside().into(imageView);
}
else
{
imageExists = false;
}
}
Hopefully this helps.

SetImageBitmap Not Working in Android

I'm developing an app for Android, I'm using the Gallery widget, and I've resized it to fullscreen mode, so it displays one image at a time.
<com.example.librosapp.MyGallery
android:id="#+id/examplegallery" android:layout_width="1920px"
android:layout_height="1020px"
android:padding="0px"
android:layout_marginTop="-20px"
/>
And here is a part of my Activity's code:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgView = new ImageView(cont);
//Here are my changes:
File imgFile = new File("sdcard/Libreria/0/0/0.JPG");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
//The app runs OK til here:
imgView.setImageBitmap(myBitmap);
//BOOM! Exception
}
imgView.setLayoutParams(new MyGallery.LayoutParams(1950, 1000));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
return imgView;
}
I don't know which exception am I getting, because I can't debug here, I'm using the .APK in my device. (The only way that I have to debug this, is with the virtual device, and I donnow why it runs really slow.
Am I doing something wrong?, that code works perfect if I use the same image, but as a project Resource (using setImageDrawable)
This is happening because myBitMap is null. myBitMap is null because the file path is invalid. My guess would be the file path should be /sdcard/Libreria/0/0/0.JPG

How to show images stored on an SD card in an Activity with ViewPager?

I have images on the SD card and I want to show these images in an Activity with a ViewPager.
On horizontal scroll the image should change.
With this methode you can retrive your picture from SDcard in a drwable object
public Drawable getImageFromSdCard(String imageName) {
Drawable d = null;
try {
String path = Environment.getExternalStorageDirectory().toString()
+ "/YourSubDirectory/";
Bitmap bitmap = BitmapFactory.decodeFile(path + "/" + imageName
+ ".png");
d = new BitmapDrawable(bitmap);
} catch (IllegalArgumentException e) {
// TODO: handle exception
}
return d;
}
and then just call this Drawable in your ViewPager.
G.Luck
use this: Retrieving only images from gallery when firing android image chooser intent to get image from gallery.
And use this to show it horizontally: http://developer.android.com/resources/tutorials/views/hello-gallery.html
You need to club both these codes together.

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.

Categories

Resources