2015-07-02 76 views
1

再次我需要你的幫助。我有這個簡單的照片應用程序代碼,但此代碼將編輯後的圖像保存在SD卡上,但我想更改此設置以將圖像保存到手機的內部存儲器中。從getExternalStorageDirectory到內部存儲

private File captureImage() { 
    // TODO Auto-generated method stub 
    OutputStream output; 

    Calendar cal = Calendar.getInstance(); 

    Bitmap bitmap = Bitmap.createBitmap(ll1.getWidth(), ll1.getHeight(), 
      Config.ARGB_8888); 

    /* 
    * bitmap = ThumbnailUtils.extractThumbnail(bitmap, ll1.getWidth(), 
    * ll1.getHeight()); 
    */ 
    Canvas b = new Canvas(bitmap); 
    ll1.draw(b); 

    // Find the SD Card path 
    File filepath = Environment.getExternalStorageDirectory(); 

    // Create a new folder in SD Card 
    File dir = new File(filepath.getAbsolutePath() + "/background_eraser/"); 
    dir.mkdirs(); 

    mImagename = "image" + cal.getTimeInMillis() + ".png"; 

    // Create a name for the saved image 
    file = new File(dir, mImagename); 

    // Show a toast message on successful save 
    Toast.makeText(SelectedImgActivity.this, "Image Saved to SD Card", 
      Toast.LENGTH_SHORT).show(); 

    try { 

     output = new FileOutputStream(file); 
     // Compress into png format image from 0% - 100% 
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, output); 
     output.flush(); 
     output.close(); 
    } 

    catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return file; 

} 

任何建議如何做到這一點?我想我只能將Environment.getExternalStorageDirectory更改爲其他的東西,但是什麼?

謝謝!

編輯:

我改變此行File filepath = Environment.getDataDirectory(); and I think this works. But this make new folder in root folder...I want it in pictures... How to archive this?

編輯2:

現在我是編輯的代碼到這個

private File captureImage() { 
    // TODO Auto-generated method stub 
    OutputStream output; 

    Calendar cal = Calendar.getInstance(); 

    Bitmap bitmap = Bitmap.createBitmap(ll1.getWidth(), ll1.getHeight(), 
      Config.ARGB_8888); 

    /* 
    * bitmap = ThumbnailUtils.extractThumbnail(bitmap, ll1.getWidth(), 
    * ll1.getHeight()); 
    */ 
    Canvas b = new Canvas(bitmap); 
    ll1.draw(b); 

    // Find the SD Card path 
    File filepath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
// File filepath = Environment.getDataDirectory(Environment.DIRECTORY_PICTURES); 

    // Create a new folder in SD Card 
    File dir = new File(filepath.getAbsolutePath() + "/Background Remover/"); 

    dir.mkdirs(); 

    mImagename = "image" + cal.getTimeInMillis() + ".png"; 

    // Create a name for the saved image 
    file = new File(dir, mImagename); 

    // Show a toast message on successful save 
    Toast.makeText(SelectedImgActivity.this, "Image Saved", 
      Toast.LENGTH_SHORT).show(); 

    try { 

     output = new FileOutputStream(file); 
     // Compress into png format image from 0% - 100% 
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, output); 
     output.flush(); 
     output.close(); 
    } 

    catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return file; 

} 

一切工作正常,除了麪包秀。 ..

回答

1

替換:

File dir = new File(filepath.getAbsolutePath() + "/background_eraser/"); 

有了:

File dir = context.getFilesDir().getAbsolutePath() + File.separator + "background_eraser"; 

您可以使用:

FileInputStream fis = context.openFileInput(name); 

在API級別1

返回目錄的絕對路徑上的文件系統,其中文件用openFileOutput(String,int)創建存儲。