2012-11-23 34 views
3

可能重複:
How to get camera result as a uri in data folder?如何將拍攝的照片保存到特定的文件夾?

我正在Picure,並保存在默認文件夾。

這是我PictureCallBack()功能:

PictureCallback myPictureCallback_JPG = new PictureCallback(){ 

     public void onPictureTaken(byte[] arg0, Camera arg1) {  

      Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues()); 

      OutputStream imageFileOS; 

      try { 

      imageFileOS = getContentResolver().openOutputStream(uriTarget); 
      imageFileOS.write(arg0); 
      imageFileOS.flush(); 
      imageFileOS.close(); 

      Toast.makeText(MainActivity.this,"Image saved: " + uriTarget.toString(),Toast.LENGTH_LONG).show(); 

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

      camera.startPreview(); 
     } 
    }; 

如何我可以將它保存在像"/sdcard/MyCustonApp/Photos/"特定文件夾?

回答

4

添加到您的代碼

String path = Environment.getExternalStorageDirectory().toString(); 
    File myNewFolder = new File(path + "/your/folder"); 
    myNewFolder.mkdir(); 
    File file = new File(path + "/your/folder", name + ".png"); 
    OutputStream imageFileOS = new FileOutputStream(file); 
+1

不錯,但現在的圖片不會顯示在畫廊或需要很長時間才能顯示。任何想法我做錯了什麼? –

+1

要顯示的圖片在畫廊必須與圖像庫添加文件:MediaStore.Images.Media.insertImage(context.getContentResolver(), \t \t \t \t \t file.getAbsolutePath(),file.getName(),\t文件。的getName()); – prozhyga

+0

你是如何解決它的?可能ypu在這裏複製你的所有代碼!?! –

1

這是我如何檢查,如果該文件夾我希望保存拍攝的圖像,不存在,或者如果它無法創建文件夾。

Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE"); 

File cameraFolder; 

if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
    cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"MyCustomApp/Photos"); 
else 
    cameraFolder = StatusUpdate.this.getCacheDir(); 
if(!cameraFolder.exists()) 
    cameraFolder.mkdirs(); 

File photo = new File(Environment.getExternalStorageDirectory(), "MyCustomApp/Photos/camera_snap.jpg"); 
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo)); 
Uri uri = Uri.fromFile(photo); 

startActivityForResult(getCameraImage, 1); 

這段代碼將捕獲的圖像保存到指定文件夾的名稱camera_snap與延伸:的.jpg

注意:這需要你申報下列權限在Manifest文件

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

我沒有使用意圖,我正在製作自定義相機應用程序。但是你檢查文件夾existir是否有用的方式。謝謝! –

+0

@BrunoAlmeida:哦。我的錯。很高興,它的一些有用的。 :-) –

0

嘗試一個文件夾,在這個簡單的代碼和存儲圖像:

String root = Environment.getExternalStorageDirectory().toString(); 
File myDir = new File(root + "/req_images"); 
myDir.mkdirs(); 
Random generator = new Random(); 
int n = 10000; 
n = generator.nextInt(n); 
String fname = "Image-" + n + ".jpg"; 
file = new File(myDir, fname); 
Log.i(TAG, "" + file); 
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(); 
} 

其中BM意味着位圖圖像名稱

1
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages"); 
imagesFolder.mkdirs(); 
File image = new File(imagesFolder, "image.jpg"); 
Uri uriSavedImage = Uri.fromFile(image); 
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); 

看一看這個鏈接。 How to save images from Camera in Android to specific folder?

+0

謝謝,就是這個,但別人之前回答過! –

相關問題