11

我需要將使用我的應用拍攝的圖片保存在特定文件夾中。我已經閱讀了很多解決這個問題的方法,但是我無法讓它們工作,所以我尋求幫助。Android - 將圖像保存在特定文件夾中

MainActivity.java

public void onClick(View v) { 

    Intent camera = new Intent(
      android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

    //Folder is already created 
    String dirName = Environment.getExternalStorageDirectory().getPath() 
      + "/MyAppFolder/MyApp" + n + ".png"; 

    Uri uriSavedImage = Uri.fromFile(new File(dirName)); 
    camera.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); 
    startActivityForResult(camera, 1); 

    n++; 
} 

AndroidManifest.xml中

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

檢查這個問題的答案在這裏:http://stackoverflow.com/questions/12995185/android-taking-photos-and-saving-them-使用自定義名稱到自定義目標 – Razgriz

+0

你檢查過,如果該文件夾是真的創建? – msana

+0

退房的鏈接.. http://stackoverflow.com/questions/7887078/android-saving-file-to-external-storage/7887114#7887114 – Hariharan

回答

32

通過下面的代碼去,它的工作對我來說很好。

private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) { 

    File direct = new File(Environment.getExternalStorageDirectory() + "/DirName"); 

    if (!direct.exists()) { 
     File wallpaperDirectory = new File("/sdcard/DirName/"); 
     wallpaperDirectory.mkdirs(); 
    } 

    File file = new File(new File("/sdcard/DirName/"), fileName); 
    if (file.exists()) { 
     file.delete(); 
    } 
    try { 
     FileOutputStream out = new FileOutputStream(file); 
     imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out); 
     out.flush(); 
     out.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
+0

我怎樣才能得到一個位圖對象與我的照片? – Goblinch

+1

從相機拍攝圖像並寫入Bitmap photo =(位圖)data.getExtras()。get(「data」);在活動結果。 – mdDroid

+0

我剛剛發現它是我的操作系統(Ubuntu)的問題。它沒有顯示我的照片,但他們在正確的文件夾 – Goblinch

5

使用像這樣。它會爲你工作。

public void onClick(View v) { 
    Intent camera = new Intent(
    android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(camera, 1); 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case 1: 
     if(resultCode == RESULT_OK) { 
     Uri selectedImage = imageReturnedIntent.getData(); 
     String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

     Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
     cursor.moveToFirst(); 

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     //file path of captured image 
     filePath = cursor.getString(columnIndex); 
     //file path of captured image 
     File f = new File(filePath); 
     filename= f.getName(); 

     Toast.makeText(getApplicationContext(), "Your Path:"+filePath, 2000).show(); 
     Toast.makeText(getApplicationContext(), "Your Filename:"+filename, 2000).show(); 
     cursor.close(); 

     //Convert file path into bitmap image using below line. 
     // yourSelectedImage = BitmapFactory.decodeFile(filePath); 
     Toast.makeText(getApplicationContext(), "Your image"+yourSelectedImage, 2000).show(); 

     //put bitmapimage in your imageview 
     //yourimgView.setImageBitmap(yourSelectedImage); 

     Savefile(filename,filePath); 
    } 
    } 
} 

public void Savefile(String name, String path) { 
    File direct = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/"); 
    File file = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/"+n+".png"); 

    if(!direct.exists()) { 
    direct.mkdir(); 
    } 

    if (!file.exists()) { 
    try { 
     file.createNewFile(); 
     FileChannel src = new FileInputStream(path).getChannel(); 
     FileChannel dst = new FileOutputStream(file).getChannel(); 
     dst.transferFrom(src, 0, src.size()); 
     src.close(); 
     dst.close(); 

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

希望這會幫助你。供參考使用camera意圖。

+0

imageReturnedIntent.getData()返回null – amy

+0

@amy您聲明此清單中的<使用權限android:name =「android.permission.WRITE_EXTERNAL_STORAGE」/> – Nirmal

+0

此解決方案適用於我,因爲android 4.4文件保存已更改。有 ContextCompat.getExternalFilesDirs(上下文,名稱); http://stackoverflow.com/questions/7887078/android-saving-file-to-external-storage – amy

2

我用mdDroid的這樣的代碼:

public void startCamera() { 
    // Create photo 
    newPhoto = new Photo(); 
    newPhoto.setName(App.getPhotoName()); 

    //Create folder !exist 
    String folderPath = Environment.getExternalStorageDirectory() + "/PestControl"; 
    File folder = new File(folderPath); 
    if (!folder.exists()) { 
     File wallpaperDirectory = new File(folderPath); 
     wallpaperDirectory.mkdirs(); 
    } 
    //create a new file 
    newFile = new File(folderPath, newPhoto.getName()); 

    if (newFile != null) { 
     // save image here 
     Uri relativePath = Uri.fromFile(newFile); 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, relativePath); 
     startActivityForResult(intent, CAMERA_REQUEST); 
    } 
} 
+0

完美,爲什麼這不在頂部 – Ajay

相關問題