The following code downloads a image from Amazon S3 into the device, and then I try to load the image in gallery.
My issue is different for the Device (Nexus 7) and the emulator.
My code below is a cumulation of reading through stackoverflow answers.
1) In the emulator DDMS, I find the file test.jpg in the device under /data/data/myprojectname/file/test.jpg The size of the file is correct.
However, it says “Unfortunately camera has stopped working” when I try to load the image using the intent method below.
2) For the Nexus 7, gallery simply shows up with no image. I can’t really find the image “test.jpg” using Astro file manager, why is this so?
Also, why would the emulator and the device act differently?
This is driving me crazy, thanks in advance for your help.
Pier.
showInGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
// Ensure that the image will be treated as such.
ResponseHeaderOverrides override = new ResponseHeaderOverrides();
override.setContentType( "image/jpeg" );
// Generate the presigned URL.
Date expirationDate = new Date( System.currentTimeMillis() + 3600000 ); // Added an hour's worth of milliseconds to the current time.
GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest( Constants.getPictureBucket(), Constants.PICTURE_NAME );
urlRequest.setExpiration( expirationDate );
urlRequest.setResponseHeaders( override );
URL url = s3Client.generatePresignedUrl( urlRequest );
/* Open a connection to that URL. */
File file = new File(PATH + Constants.PICTURE_NAME);
URLConnection ucon = url.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
String fileName = "test.jpg";
//FileOutputStream fos = new FileOutputStream(fileName);
FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
fos.write(baf.toByteArray());
fos.close();
/* read the file */
File filePath = getFileStreamPath(fileName);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(filePath.toString()), "image/*");
startActivity(intent);
}
catch ( Exception exception ) {
S3UploaderActivity.this.displayAlert( "Browser Failure", exception.getMessage() );
}
}
});
You should consider using lazyloading instead,
take a look at this project:
https://github.com/thest1/LazyList