2012-01-02 75 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

相关问题