2010-04-23 71 views
8

人,我仍然無法保存圖片時,我送一個意圖,要求相片才能作出。下面是我在做什麼:問題保存照片文件

  1. 做一個URI代表路徑

    android.content.Context c = getApplicationContext(); 
    
    String fname = c.getFilesDir().getAbsolutePath()+"/parked.jpg"; 
    
    java.io.File file = new java.io.File(fname); 
    
    Uri fileUri = Uri.fromFile(file); 
    
  2. 創建意圖(!不要忘了PKG名稱),並開始活動

    private static int TAKE_PICTURE = 22; 
    
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    
    intent.putExtra("com.droidstogo.boom1." + MediaStore.EXTRA_OUTPUT, fileUri); 
    startActivityForResult(intent, TAKE_PICTURE); 
    
  3. 攝像頭活動開始,我可以拍照並批准它。我的onActivityResult()然後被調用。但是我的文件沒有寫入。 該URI是:file:///data/data/com.droidstogo.boom1/files/parked.jpg

  4. 我可以創建縮略圖確定(通過不要將額外的意圖),並可以寫文件確定,然後再讀回)。

任何人都可以看到我在做什麼簡單的錯誤嗎? logcat中沒有任何明顯的表現 - 相機清晰地拍攝了照片。謝謝,

彼得


我要指出,我有適當的權限在AndroidManifest.xml文件中設置:

<uses-permission android:name="android.permission.READ_OWNER_DATA" /> 
    <uses-permission android:name="android.permission.WRITE_OWNER_DATA" /> 

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

    <uses-feature android:name="android.hardware.camera" /> 
    <uses-library android:name="com.google.android.maps" /> 



</application> 

任何想法?任何關於嘗試的想法,以獲得有關該問題的更多信息?

+0

可以請你把一些代碼,以更好地瞭解問題 – the100rabh 2010-04-29 17:17:30

+0

也許這[問題](http://stackoverflow.com/questions/649057/how-do-i-save-data-from-camera-to- disk-using-mediastore-on-android)可以提供幫助。 – kgiannakakis 2010-04-28 10:57:59

回答

1

是不是因爲你需要添加額外的點:

​​3210

相反的:

intent.putExtra("com.droidstogo.boom1" 
1

你的問題可能與你想存儲文件的目錄將文件保存到SD卡上不需要任何特殊權限,但獲取文件夾引用的方式與完成該操作的方式不同。它還取決於您是否想要以MediaStore可以檢索的方式保存圖像(即圖庫或專輯應用程序或任何其他依賴這些圖像來查找圖像的應用程序)。假設你希望它在MediaStore上市,這裏的代碼來做到這一點:

ContentValues newImage = new ContentValues(2); 
newImage.put(Media.DISPLAY_NAME, "whatever name you want shown"); 
newImage.put(Media.MIME_TYPE, "image/png"); 

Uri uri = contentResolver.insert(Media.EXTERNAL_CONTENT_URI, newImage); 

try { 
    Bitmap bitmap = //get your bitmap from the Camera, however that's done 
    OutputStream out = contentResolver.openOutputStream(uri); 
    boolean success = bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); 
    out.close(); 
    if (success){ 
     Log.d("Image Writer", "Image written successfully.");     
    } else { 
     Log.d("Image Writer", "Image write failed, but without an explanation."); 
    } 

} catch (Exception e){ 
    Log.d("Image Writer", "Problem with the image. Stacktrace: ", e); 
} 

在我的模擬器上運行V1.5,這是成功保存的位圖到與DCIM/Camera文件夾在SD卡的文件名稱是當前時間。 (自1970年1月1日,也被稱爲「時代」出於某種原因,節省時間以毫秒爲單位)。

+0

只是爲了挑選...將文件保存到SD卡你必須有權限。我認爲這是在api級別4中更改的。 – 2010-04-30 01:49:56

+0

@fiXedd:只是爲了挑剔作爲回報:P證明你是對的,他們介紹了具有寫入SD卡的write_external_storage權限的要求。在v1.5上,它沒有運行正常,但在v2.1上它需要許可。但是,要寫入媒體商店,在2.1和1.5上都不需要權限。 – 2010-04-30 10:18:00

7
  1. 正如史蒂夫^ h說你不能只使用file:///數據/數據/ com.droidstogo.boom1/files/parked.jpg。這是你的應用程序私人目錄,相機不能在那裏寫。例如,您可以使用一些SD卡文件 - 它適用於所有人。

  2. 正如stealthcopter說,意圖額外只是MediaStore.EXTRA_OUTPUT沒有你的包名。

  3. 僅供參考不是問題。我猜這個操作實際上不需要你指定的任何權限。

這裏是我的代碼示例:

final int REQUEST_FROM_CAMERA=1; 

private File getTempFile() 
{ 
    //it will return /sdcard/image.tmp 
    return new File(Environment.getExternalStorageDirectory(), "image.tmp"); 
} 

private void getPhotoClick() 
{ 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(getTempFile())); 
    startActivityForResult(intent, REQUEST_FROM_CAMERA); 
} 


protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQUEST_FROM_CAMERA && resultCode == RESULT_OK) { 
    InputStream is=null; 

    File file=getTempFile(); 
    try { 
     is=new FileInputStream(file); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    //On HTC Hero the requested file will not be created. Because HTC Hero has custom camera 
    //app implementation and it works another way. It doesn't write to a file but instead 
    //it writes to media gallery and returns uri in intent. More info can be found here: 
    //http://stackoverflow.com/questions/1910608/android-actionimagecapture-intent 
    //http://code.google.com/p/android/issues/detail?id=1480 
    //So here's the workaround: 
    if(is==null){ 
     try { 
      Uri u = data.getData(); 
      is=getContentResolver().openInputStream(u); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
    } 

    //Now "is" stream contains the required photo, you can process it 
    DoSomeProcessing(is); 

    //don't forget to remove the temp file when it's not required. 
    } 

} 
0

正如史蒂夫說,你應該保存您的圖片在你的SD卡。你試圖保存的目錄是私人的,除非你有你的設備植根,你不會寫在那裏。 嘗試替換此行:

String fname = c.getFilesDir().getAbsolutePath()+"/parked.jpg"; 

這一行

String fname = Environment.getExternalStorageDirectory().getAbsolutePath() + "somePathYouKnownExists" + +"/parked.jpg"; 

這應該是足夠的。