2013-06-28 41 views
0

我想將我的圖片保存在我選擇的文件夾中;我有以下代碼保存在我的設置的默認文件夾圖像;我怎樣才能修改我的應用程序的代碼?從onPictureTaken保存圖片在我的文件夾中的sd

PictureCallback myPictureCallback_JPG = new PictureCallback(){ 

     @Override 
     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(FotoActivity.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(); 
     }}; 
+0

沒有ü解決了這個問題? – KOTIOS

+0

不......你能幫我嗎? – Juppy

+0

是我可以嘗試幫助你 – KOTIOS

回答

0

將此代碼添加到您的onPictureTaken 日期現在=新的日期(); long nowLong = now.getTime()/ 9000; String fname = getPhotoDirectory(MyclassName.this)+「/」+ nowLong +「。jpg」;

 String why = ""; 
     try { 

      File ld = new File(getPhotoDirectory(MyclassName.this)); 
      if (ld.exists()) { 
       if (!ld.isDirectory()){ 
        MyclassName.this.finish(); 
       } 
      } else { 
       ld.mkdir(); 
      } 

      Log.d(TAG, "open output stream "+fname +" : " +data.length); 

      OutputStream os = new FileOutputStream(fname); 
      os.write(data,0,data.length); 
      os.close(); 
     } catch (FileNotFoundException ex0) { 
      why = ex0.toString(); 
      ex0.printStackTrace(); 
     } catch (IOException ex1) { 
      why = ex1.toString(); 
      ex1.printStackTrace(); 
     } 

在你的類中添加這個方法

public static String getPhotoDirectory(Context context) 
{ 
    //return Environment.getExternalStorageDirectory().getPath() +"/cbo-up"; 
    return context.getExternalFilesDir(null).getPath() +"/cbo-up"; 
} 
相關問題