Loading pictures from AWS S3 takes too much time - android

In my Android app, I have a GridView which uses a BaseAdapter to load data from DynamoDB and S3 in order to set every cell.
This is the code which I'm using to get the items pictures from S3:
ExecutorService exec = Executors.newSingleThreadExecutor();
Future<Bitmap> future = exec.submit(new Callable<Bitmap>() {
#Override
public Bitmap call() {
try {
url = new URL(/*S3 url*/);
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
Log.e("Malformed", e.getMessage());
} catch (IOException e) {
Log.e("IO", e.getMessage());
}
return bmp;
}
});
try {
holder.itemImage.setImageBitmap(future.get());
} catch (ExecutionException e) {
Log.e("Execution", e.getMessage());
} catch (InterruptedException er) {
Log.e("Interrupted", er.getMessage());
}
The code gets a Bitmap from S3 URL, and sets the ImageView on the GridView cell layout as the .jpg file on S3 URL. The problem is that getting the images from S3 takes very long.
Is there any way to speed this process? Maybe writing this code at the beginning and load it there?

Related

Not able to set Bitmap to wallpaper using wallpaperManager

I have a string which consists an URL of the image, in the same activity I am viewing the image through URL. But to set the same image as my wallpaper, I am converting the string to Uri and then to Bitmap to use setBitmap.But I am still getting error telling No image was chosen.
Code is below:
newString has the URL of the image.
final String myUrlStr = newString;
URL url;
Uri uri=null;
try {
url = new URL(myUrlStr);
uri = Uri.parse( url.toURI().toString() );
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
try {
image = MediaStore.Images.Media.getBitmap(this.getContentResolver(),uri);
} catch (IOException e) {
e.printStackTrace();
}
setButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
WallpaperManager wallpaperManager=WallpaperManager.getInstance(getApplicationContext());
try {
// Set the image as wallpaper
if(image!=null)
wallpaperManager.setBitmap(image);
else
Toast.makeText(getApplicationContext(), "No image was chosen.", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
It seems like I can't comment. So i answer here.
I am not sure about your url being on internet or local. I don't find anything wrong with your code. So, my deduction is your onclicklistener is set before it can fetch image.(might need to use asyntask to save image) Are you displaying on your imageview from same image resource?

Display Photo in Android App from Server

There is no issue if I display only Image on any Activity as I will convert Image to Bitmap in doInBackGround().
But what If I am using Custom ListView and I want to display Image on each Item ?
I am using adapter.setViewBinder for this. But issue is that I can't use it in doInBackground() so I need to use in onPostExecute() and this is the big problem. Now, if there are more images then app will freeze for sometime till all images convert into Bitmap.
How to prevent this issue and complete all process in doInBackground() only ?
My Code :
adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Object data, String textRepresentation) {
if (view.getId() == R.id.imgHotelPhoto) {
String Photo = data.toString();
try {
url_Hotelhoto = new URL("" + WebsiteURL"/images/HotelSmallPhoto/" + Photo + "");
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
bmp_HotelPhoto = BitmapFactory.decodeStream(url_Hotelhoto.openConnection().getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
((ImageView) view).setImageBitmap(bmp_HotelPhoto);
return true;
}
return false;
}
});

unable to recursively download images

Hi i am trying to recursively download the images but i am unable to.
It only downloads the first image! does any one know why?
this is my code to download, I did a log to check if there are items in my list and yes, there are 20:
Log.d("imageList.size",String.valueOf(imageList.size()));
try
{
for (int i=0; i<=imageList.size(); i++)
{
String image= imageList.get(i);
Log.d("imageList.get(0)",image);
String filename = String.valueOf(image.hashCode());
Log.v("TAG FILE :", filename);
File f = new File(cacheDir, filename);
// Is the bitmap in our cache?
Bitmap bitmap = BitmapFactory.decodeFile(f.getPath());
if (bitmap != null)
return bitmap;
else {
// Nope, have to download it
try {
bitmap = BitmapFactory.decodeStream(new URL(image)
.openConnection().getInputStream());
// save bitmap to cache for later
writeFile(bitmap, f);
return bitmap;
} catch (FileNotFoundException ex) {
ex.printStackTrace();
Log.v("FILE NOT FOUND", "FILE NOT FOUND");
return null;
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
There seems to be a problem with your for loop, it is exiting on the first iteration of the loop, check your braces and ensure that you have the braces setup the way you intend it and the logic makes sense. You should NOT return until your for loop is done.
Line: 25 in your code, do not return before the loop ends.

Reading Image from internal memory android gives null pointer exception

I am quite new to android. I want to save image to internal memory and later retrieve the image from internal memory and load it to image view. I have successfully stored the image in internal memory using the following code :
void saveImage() {
String fileName="image.jpg";
//File file=new File(fileName);
try
{
FileOutputStream fOut=openFileOutput(fileName, MODE_PRIVATE);
bmImg.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
}
catch (Exception e)
{
e.printStackTrace();
}
}
using this code image is saved. But when i try to retrieve the image it gives me error. The code used to retrieve the image is :
FileInputStream fin = null;
ImageView img=new ImageView(this);
try {
fin = openFileInput("image.jpg");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] bytes = null;
try {
fin.read(bytes);
} catch (Exception e) {
e.printStackTrace();
}
Bitmap bmp=BitmapFactory.decodeByteArray(bytes,0,bytes.length);
img.setImageBitmap(bmp);
But i get a Null pointer exception.
I checked the file is there in internal memory at path :
/data/data/com.test/files/image.jpg
What am i doing wrong please help me out with this. I have gone through a lot of stack questions.
it is because your bytes array is null, instantiate it, and assign size.
byte[] bytes = null; // you should initialize it with some bytes size like new byte[100]
try {
fin.read(bytes);
} catch (Exception e) {
e.printStackTrace();
}
Edit 1: I am not sure but you can do something like
byte[] bytes = new byte[fin.available()]
Edit 2 : here is a better solution, as you are reading Image,
FileInputStream fin = null;
ImageView img=new ImageView(this);
try {
fin = openFileInput("image.jpg");
if(fin !=null && fin.available() > 0) {
Bitmap bmp=BitmapFactory.decodeStream(fin)
img.setImageBitmap(bmp);
} else {
//input stream has not much data to convert into Bitmap
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Helped by - Jason Robinson

Setting the Wallpaper from an Image on the SDCard

How would one go about setting the homescreen wallpaper from an image on the SDcard?
i.e.:
try {
wallpaperManager.setResource("/sdcard/wallpaper/olive.jpg");
finish();
} catch(IOException e) {
e.printStackTrace();
}
hasn't worked, returned an error: 'The method setResource(int) in the type WallpaperManager is not applicable for the arguments (String)'
Bitmap o = BitmapFactory.decodeFile("/sdcard/wallpapers/olive.jpg");
try {
wallpaperManager.setBitmap(o);
finish();
}
catch (IOException e) {
e.printStackTrace();
}

Categories

Resources