2014-10-06 146 views
6

我可以按下按鈕,打開本機照相機應用程序,併成功拍照。但是,當我檢查手機上的圖庫或照片原生應用程序時,圖片不會保存在那裏。我對Android非常陌生,所以很可能我在代碼中丟失了一些重要的東西。拍照後照相機未保存

問題:

1)這些圖片在哪裏被保存?

2)我是否可以修改下面的代碼以保存到內部存儲器,所以使用我的應用程序拍攝的所有照片都是私人的,只能通過我的應用程序訪問? 3)如果我想將這些圖片保存到一個對象,並附上一些文本/其他輸入,那麼最好的方法是什麼?我應該保存一個Uri還是一些標識符以後引用圖像,或者保存實際的BitMap圖像?

任何幫助非常感謝,謝謝!

這裏是我的代碼拍照:從Google developer guides直取

mImageButton.setOnClickListener(new View.OnClickListener() 
{ 
    public void onClick(View v) 
    { 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     imageUri = CameraUtils.getOutputMediaFileUri(CameraUtils.MEDIA_TYPE_IMAGE); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 
     startActivityForResult(intent, REQUEST_IMAGE); 
    } 
} 

CameraUtils類:

public static Uri getOutputMediaFileUri(int type) 
{ 
    return Uri.fromFile(getOutputMediaFile(type)); 
} 

public static File getOutputMediaFile(int type) 
{ 
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES), "camera"); 

    if (!mediaStorageDir.exists()) 
    { 
     if (!mediaStorageDir.mkdirs()) 
     { 
      return null; 
     } 
    } 

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    File mediaFile; 
    if (type == MEDIA_TYPE_IMAGE) 
    { 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
       "IMG_" + timeStamp + ".jpg"); 
    } 
    else if(type == MEDIA_TYPE_VIDEO) 
    { 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
       "VID_" + timeStamp + ".mp4"); 
    } 
    else 
    { 
     return null; 
    } 

    return mediaFile; 
} 

回答

4

1)通過查看代碼,我期望的照片是保存在一個名爲'相機'的目錄中,該目錄可以在設備上的圖片文件夾(外部存儲)中找到。這些可能不會立即出現在您的畫廊中,但是在更高版本的Android中(Kitkat,也許果凍豆雖然我現在無法驗證),但您應該能夠打開照片應用程序並在其中找到它們。如果情況並非如此,那麼啓動一個文件瀏覽器應用程序(例如應用程序是ASTRO文件管理器或X-Plore)並瀏覽到圖片/照相機目錄,您應該在其中看到圖像。下次您的媒體重新編入索引時(手機重新啓動,或從其他地方觸發的重新索引),您應該在您的圖庫/照片應用中看到這些照片。如果您想以編程方式刷新媒體,here可能會有所幫助。最後,請確保您的Android清單中具有READ_EXTERNAL_STORAGE權限(如指定this(Android doc))。 2)如果要將圖像保存爲僅供應用程序使用,則需要將它們保存到應用程序的內部數據目錄中。直接從Android文檔中查看this。確保使用MODE_PRIVATE標誌。

3)爲此,您希望將文件路徑存儲在您的應用程序可訪問的某處。您可以將文件路徑保存爲包含其他文本數據的文本文件,也可以使用sqlite數據庫。最後,你可以使用像ORMLite這樣的ORM來保存一個java對象,它可以爲你的圖片保存數據,並且有一些你想要保留的字段(標題,描述,路徑等)。 Herehere是介紹如何開始使用Android中的SQLite數據庫(直接從官方文檔)。如果你想使用ORMLite,他們的網站上有大量的信息here。開發人員花了很多時間回答StackOverflow的問題。

所有的問題都可以用一些簡單的Google搜索來解答。它們是Android中非常標準和基本的事情,所以你應該能夠在網上找到大量的信息和教程。

編輯

在回答你關於第二個問題發表評論。這是我可能會做的(或類似的):

請注意,我沒有測試這個。它來自我的頭頂。如果您有更多問題,請點擊此處!

活動代碼...

mImageButton.setOnClickListener(new View.OnClickListener() 
{ 
    public void onClick(View v) 
    { 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     imageUri = CameraUtils.getOutputMediaFileUri(currentActivity, CameraUtils.MEDIA_TYPE_IMAGE); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 
     startActivityForResult(intent, REQUEST_IMAGE); 
    } 
} 

public void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    if (requestCode == REQUEST_IMAGE) 
    { 
     if (resultCode == RESULT_OK) 
     { 
      String pathToInternallyStoredImage = CameraUtils.saveToInternalStorage(this, imageUri); 
      // Load the bitmap from the path and display it somewhere, or whatever 
     } 
     else if (resultCode == RESULT_CANCELED) 
     { 
      //Cancel code 
     } 
    } 
} 

CameraUtils類代碼...

public static Uri getOutputMediaFileUri(int type) 
{ 
    return Uri.fromFile(getOutputMediaFile(type)); 
} 

public static File getOutputMediaFile(int type) 
{ 
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES), "camera"); 

    createMediaStorageDir(mediaStorageDir); 

    return createFile(type, mediaStorageDir); 
} 

private static File getOutputInternalMediaFile(Context context, int type) 
{ 
    File mediaStorageDir = new File(context.getFilesDir(), "myInternalPicturesDir"); 

    createMediaStorageDir(mediaStorageDir); 

    return createFile(type, mediaStorageDir); 
} 

private static void createMediaStorageDir(File mediaStorageDir) // Used to be 'private void ...' 
{ 
    if (!mediaStorageDir.exists()) 
    { 
     mediaStorageDir.mkdirs(); // Used to be 'mediaStorage.mkdirs();' 
    } 
} // Was flipped the other way 

private static File createFile(int type, File mediaStorageDir) // Used to be 'private File ...' 
{ 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    File mediaFile = null; 
    if (type == MEDIA_TYPE_IMAGE) 
    { 
     mediaFile = new File(mediaStorageDir .getPath() + File.separator + 
       "IMG_" + timeStamp + ".jpg"); 
    } 
    else if(type == MEDIA_TYPE_VIDEO) 
    { 
     mediaFile = new File(mediaStorageDir .getPath() + File.separator + 
       "VID_" + timeStamp + ".mp4"); 
    } 
    return mediaFile; 
} 

public static String saveToInternalStorage(Context context, Uri tempUri) 
{ 
    InputStream in = null; 
    OutputStream out = null; 

    File sourceExternalImageFile = new File(tempUri.getPath()); 
    File destinationInternalImageFile = new File(getOutputInternalMediaFile(context).getPath()); 

    try 
    { 
     destinationInternalImageFile.createNewFile(); 

     in = new FileInputStream(sourceExternalImageFile); 
     out = new FileOutputStream(destinationInternalImageFile); 

     // Transfer bytes from in to out 
     byte[] buf = new byte[1024]; 
     int len; 
     while ((len = in.read(buf)) > 0) 
     { 
      out.write(buf, 0, len); 
     } 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
     //Handle error 
    } 
    finally 
    { 
     try { 
      if (in != null) { 
       in.close(); 
      } 
      if (out != null) { 
       in.close(); 
      } 
     } catch (IOException e) { 
      // Eh 
     } 
    } 
    return destinationInternalImageFile.getPath(); 
} 

所以,現在你有路徑指向您的內部存儲的圖像,你可以然後從你的onActivityResult操縱/加載。

+0

感謝您的回覆!關於#2,我已經看過那部分文檔,但我不確定如何將它應用到我的應用中。你知道任何示例說明如何去做嗎?我只能找到代碼片段。 – pez 2014-10-06 22:26:05

+0

再次感謝!在我的手機上運行您的代碼,在我拍攝照片後,它似乎總是在相機應用程序中凍結。使用'Log.d',似乎所有東西都被調用,並且logcat沒有顯示任何錯誤。但是在我拍照之後,它不會讓我用原生相機應用程序中的複選標記來確認它;我只能點擊「x」重新拍攝照片,或者按手機導航欄上的「主頁」或「後退」按鈕。 – pez 2014-10-06 23:35:26

+1

你的Android清單中有這三個權限嗎? '' '' '' 你真的只需要寫入權限來保存圖像(以及我相信的相機功能)。但通常閱讀也包括在內,因爲你可能需要它。 – maraci 2014-10-07 15:21:27