11

Tôi cần lưu các hình ảnh được chụp bằng ứng dụng của mình trong một thư mục cụ thể. Tôi đã đọc nhiều giải pháp cho vấn đề này nhưng tôi không thể thực hiện bất kỳ giải pháp nào trong số đó vì vậy tôi yêu cầu trợ giúp.Android - Lưu hình ảnh trong một thư mục cụ thể

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

Kiểm tra câu trả lời cho câu hỏi này ở đây: http://stackoverflow.com/questions/12995185/android-taking-photos-and-saving-them- with-a-custom-name-to-a-custom-destinati – Razgriz

+0

Bạn đã kiểm tra xem thư mục có thực sự được tạo không? – msana

+0

Kiểm tra các liên kết .. http://stackoverflow.com/questions/7887078/android-saving-file-to-external-storage/7887114#7887114 – Hariharan

Trả lời

32

Đi qua đoạn mã sau, làm việc của nó tốt cho tôi.

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

Làm thế nào tôi có thể nhận được một đối tượng Bitmap với hình ảnh của tôi? – Goblinch

+1

chụp ảnh từ máy ảnh và ghi ảnh bitmap = (Bitmap) data.getExtras(). Get ("data"); trong kết quả hoạt động. – mdDroid

+0

Tôi vừa phát hiện ra rằng đó là một vấn đề với hệ điều hành của tôi (Ubuntu). Nó không hiển thị cho tôi những hình ảnh nhưng chúng nằm trong đúng thư mục – Goblinch

5

Sử dụng như thế này. Nó sẽ làm việc cho bạn.

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(); 
     } 
    } 
    } 

Hy vọng điều này sẽ giúp bạn. để tham chiếu đến mục đích sử dụng camera.

+0

imageReturnedIntent.getData() trả về null – amy

+0

@amy bạn đã tuyên bố điều này trong manifest – Nirmal

+0

Giải pháp này phù hợp với tôi, kể từ khi android 4.4 tiết kiệm tập tin đã được thay đổi. có ContextCompat.getExternalFilesDirs (ngữ cảnh, tên); http: //stackoverflow.com/questions/7887078/android-saving-file-to-external-storage – amy

2

tôi đã sử dụng mã mdDroid của như thế này:

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

Tuyệt vời, tại sao điều này không nằm ở đầu – Ajay

Các vấn đề liên quan