拍摄和保存照片 Android 相机:照片未保存在我的设备上
我想要一个简单的应用程序,可以拍照并将其保存在图库中,如下所示: http://developer.android.com/training/camera/photobasics.html
我在我的设备(2.1)上测试了该应用程序,我拍了照片,他们要求我单击“确定” ,但是图片没有保存在图库中,你知道为什么吗?我怎么知道错误来自哪里? (模拟器没有任何“sd卡”,所以我无法真正调试该项目)。另外,这种技术与 getContentResolver().openOutputStream(uri);
的技术有什么区别:http://developer.android.com/guide/topics/providers/content-providers.html#modifying ?
另外,我可以尝试这个解决方案: 图片保存在模拟器中但不在设备上 但我想知道为什么代码在我的设备上不起作用......?
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:我实际上在“手机”中有图片,当我将设备插入计算机时,我可以看到一张新图片文件,名称正确,但它是空的,所以我无法打开它。另外,我的画廊中什么也没有出现。
谢谢
I'd like to have a simple app that takes a picture and saves it in the Gallery, as shown here : http://developer.android.com/training/camera/photobasics.html
I tested the app on my device (2.1), i took the picture, they asked me to click on "ok", but the picture is not saved in the Gallery, do you know why? and how i could know where the error comes from? (the emulator does not have any "sd card", so i cannot really debug the project). Also, what's the difference between this technique and the one with getContentResolver().openOutputStream(uri);
: http://developer.android.com/guide/topics/providers/content-providers.html#modifying ?
Also, i could try this solution : Picture saving in emulator but not on device but i was wondering why the code does not work on my 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);
}
}
edit: in case someone wants to test the app, i just added these 2 lines in the manifest :
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
edit 2 : i actually have the picture in the "phone", when i plug the device to my computer, i can see a new picture file, with the right name, but it's empty, so i cannot open it. Plus, nothing appears in my gallery.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试添加这个:
Try addding this:
您的 AndroidManafest.xml 文件中是否有这一行?
Do you have this line in your AndroidManafest.xml file?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />