2012-01-02 63 views
2

我想有一個簡單的應用程序,拍照,並在畫廊保存它,如下所示:http://developer.android.com/training/camera/photobasics.html拍攝並保存圖片Android攝像頭:圖片不保存我的設備上

我測試應用程序在我的設備上(2.1),我拍了照片,他們讓我點擊「確定」,但照片沒有保存在畫廊中,你知道爲什麼?以及我怎麼知道錯誤來自哪裏? (模擬器沒有任何「SD卡」,所以我不能真正調試該項目)。此外,這種技術與getContentResolver().openOutputStream(uri);http://developer.android.com/guide/topics/providers/content-providers.html#modifying之間的區別是什麼?

另外,我可以試試這個解決方案:Picture saving in emulator but not on device但我想知道爲什麼代碼不能在我的設備上工作..?

package pack.one; 

import java.io.File; 
import java.io.IOException; 
import java.sql.Date; 
import java.text.SimpleDateFormat; 

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.ImageView; 

public class TestPictureActivity extends Activity { 
    private static final String JPEG_FILE_SUFFIX = ".jpg"; 
    Intent takePictureIntent; 
    String mCurrentPhotoPath; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     dispatchTakePictureIntent(11); 
    } 

    private void dispatchTakePictureIntent(int actionCode) { 
     takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     startActivityForResult(takePictureIntent, actionCode); 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
     try { 
      handleSmallCameraPhoto(data); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    private void handleSmallCameraPhoto(Intent intent) throws IOException { 
     Bundle extras = intent.getExtras(); 
     Bitmap mImageBitmap = (Bitmap) extras.get("data"); 
     LayoutInflater inflater = LayoutInflater.from(this); 
     View view = inflater.inflate(R.layout.viewlayout, null); 

     ImageView mImageView = (ImageView) view.findViewById(R.id.V); 
     mImageView.setImageBitmap(mImageBitmap); 

     File f = createImageFile(); 
     takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 

     galleryAddPic(); 
    } 

    private File createImageFile() throws IOException { 
     // Create an image file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(0)); 
     String imageFileName = timeStamp + "_"; 
     File image = File.createTempFile(
      imageFileName, 
      JPEG_FILE_SUFFIX, 
      null //default location for temporary files 
     ); 
     mCurrentPhotoPath = image.getAbsolutePath(); 
     return image; 
    } 

    private void galleryAddPic() { 
     Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
     File f = new File(mCurrentPhotoPath); 
     Uri contentUri = Uri.fromFile(f); 
     mediaScanIntent.setData(contentUri); 
     this.sendBroadcast(mediaScanIntent); 
    } 
} 

編輯:萬一有人要測試的應用程序,我只是說這兩條線在清單:

<uses-feature android:name="android.hardware.camera" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

編輯2:其實,我在「手機」的照片時,我將設備插入電腦,我可以看到一個新的圖片文件,名稱正確,但它是空的,所以我無法打開它。另外,我的畫廊中沒有任何內容。

謝謝

回答

0

你在你的AndroidManafest.xml文件中有這條線嗎?

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

+0

感謝比爾,我沒有把這一行,我重新安裝了這一行的應用程序,但畫面依然沒有在我的畫廊顯示.. – Paul 2012-01-02 09:25:02

1

嘗試addding這樣的:

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

感謝Seshu Vinay,仍然沒有工作,我上面編輯了我的帖子:沒有出現在Gallery中,但是我可以看到文件已經放入手機中,即使我無法打開此文件,因爲它是「空的」 – Paul 2012-01-02 13:08:52