次のコードは、Amazon S3 からデバイスに画像をダウンロードし、その画像をギャラリーにロードしようとします。私の問題は、デバイス (Nexus 7) とエミュレーターでは異なります。以下の私のコードは、スタックオーバーフローの回答を読んだ累積です。
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() );
}
}
});