以下代码将图像从 Amazon S3 下载到设备中,然后我尝试将图像加载到图库中。我的问题与设备(Nexus 7)和模拟器不同。我下面的代码是阅读stackoverflow答案的累积。
1)在模拟器DDMS中,我在设备中的/data/data/myprojectname/file/test.jpg下找到文件test.jpg文件的大小是正确的。但是,当我尝试使用下面的意图方法加载图像时,它会显示“不幸的是相机已停止工作”。
2) 对于 Nexus 7,图库只是显示没有图像。我真的无法使用 Astro 文件管理器找到图像“test.jpg”,为什么会这样?
另外,为什么模拟器和设备会有不同的行为?
这让我发疯,提前感谢您的帮助。
码头。
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() );
}
}
});