2013-08-21 13 views
0

我已經搜查了與此相關的所有問題,但我沒有得到解決方案。我的要求是從我的Android應用程序,用戶可以選擇從手機畫廊的圖像,並將其設置爲他最喜歡的。我的問題是從畫廊拍攝的照片需要保存到應用程序本地目錄(該目錄對用戶不可見,因爲它將存儲在應用程序中)。如果用戶從手機庫中刪除圖像,那麼應用程序也應該顯示他最喜歡的圖像。所以我需要保存本地目錄。請幫助我。提前致謝。我知道以下代碼用於存儲到SD卡,但我需要將圖像從SD卡保存到本地應用程序文件夾。如何保存圖片(從手機庫導入到android應用程序)並保存到android應用程序本地目錄?

BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
     bmOptions.inSampleSize = 1; 
     Bitmap bm = LoadImage(imagePath, bmOptions); 
     String root = Environment.getExternalStorageDirectory().toString(); 
     File myDir = new File(root + "/MyFolder");  
     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); 
       bm.compress(Bitmap.CompressFormat.JPEG, 90, out); 
       out.flush(); 
       out.close(); 

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

回答

0

使用例如:

File path = getExternalFilesDir(Environment.DIRECTORY_DCIM); 
     File image = new File(path,imageFileName); 

你的形象會在你的應用程序目錄下保存。

相關問題