2011-10-22 29 views
1

我試圖從Amazon S3下載一組.jpg,並將它們存儲到內部存儲器(以便它們不能被惡意用戶複製)。我已經得到這麼多,但現在我被卡住了。我發現了多個與位圖或數組有關的問題,但沒有關於存儲圖像然後再訪問它的問題。任何人都知道我從哪裏出發?序列化對象(jpg)到內部存儲

String itemName = iconNames.getString(iconNames.getColumnIndexOrThrow(DbAdapter.KEY_ICON)); 
     itemName = itemName + ".jpg"; 
     GetObjectRequest getObject = new GetObjectRequest(bucket, itemName); 

     S3Object icon = mS3Client.getObject(getObject); 
     InputStream input = icon.getObjectContent(); 

我在這裏看開發者指南中,它給下面的代碼 http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

String FILENAME = "hello_file"; 
String string = "hello world!"; 

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); 
fos.write(string.getBytes()); 
fos.close(); 

但是,這是一個用於存儲字符串,而不是圖像...

回答

0

你有將InputStream複製到OutputStream。像這樣的:

InputStream input = icon.getObjectContent(); 
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); 

// Transfer bytes from in to out 
byte[] buf = new byte[1024]; 
int len; 
while ((len = input.read(buf)) > 0) { 
    fos.write(buf, 0, len); 
} 
input.close(); 
fos.close(); 
+0

工作!謝謝!...現在我必須弄清楚它在做什麼哈哈。 – easycheese

0

你可以做類似下面的事情。

Bitmap bitmapPicture = someBitmap 
String path = Environment.getExternalStorageDirectory().toString(); 
OutputStream fOut = null; 
File file = new File(path, "tmp.png"); 
fOut = new FileOutputStream(file); 

bitmapPicture.compress(Bitmap.CompressFormat.PNG, 100, fOut); 
fOut.flush(); 
fOut.close(); 

MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName()); 
+0

這是不是將它存儲到外部存儲? – easycheese

+0

是的,你是對的。在原來的問題中沒有看到那部分。 – broschb