2012-09-25 54 views
2

好吧,我有Gallery應用程序,裏面有很多圖片(res/drawable)。需要將res文件夾中的圖片複製到圖庫/文件夾

在選擇你可以設置爲壁紙按鈕,你會擁有它。

現在我想保存按鈕保存到手機或SD卡選擇此圖像。我怎樣才能管理。從應用程序文件夾的res複製到手機或SD卡。不想從ImageView中取出它,而只是將原件從res複製到Phone。

+0

你可以找到一個答案,應爲你在這裏做 http://stackoverflow.com/questions/939170/resources-openrawresource-issue-android – dorjeduck

回答

2

試試這個代碼:

String root = Environment.getExternalStorageDirectory().toString(); 
    File myDir = new File(root + "/saved_images");  
    myDir.mkdirs(); 
    Random generator = new Random(); 
    int n = 10000; 
    n = generator.nextInt(n); 
    String fname = "Image-"+ n +".jpg"; 
    File file = new File (myDir, fname); 
    if (file.exists()) file.delete(); 
    try { 
     FileOutputStream out = new FileOutputStream(file); 
     finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
     out.flush(); 
     out.close(); 

} catch (Exception e) { 
     e.printStackTrace(); 
} 

,並添加清單文件:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

這不是複製。 – Allen

1

以下步驟: -

代碼:

String path = Environment.getExternalStorageDirectory().toString(); 
OutputStream fOut = null; 
file = new File(path, "image.jpg"); 
fOut = new FileOutputStream(file); 
Bitmap bitmap = BitmapFactory.decodeResource (getResources(), R.drawable.xyz); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); 
fOut.flush(); 
fOut.close(); 
MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName()); 
+0

這一個工作。現在我怎樣才能設置文件名和什麼文件夾來保存,因爲我現在正在將圖像映射到/DCIM/Camera/somerandomnumber.jpg – Allen

+0

您正在獲取隨機數作爲名稱,因爲可能有一個圖像已經與'image.jpg'名稱。因此,使用一些獨特的名稱(如當前日期時間)。 檢查mehtod'MediaStore.Images.Media.insertImage' [這裏](http://developer.android.com/reference/android/provider/MediaStore.Images.Media.html),第三個參數是圖片。 – vKashyap

+0

我找到了,thnx無論如何:)愛學習;) – Allen

相關問題