【发布时间】:2015-10-19 02:49:12
【问题描述】:
我刚开始使用 Android 编程,我正在创建一个 Android 应用程序,用户可以在其中输入信息,使用 ImageView 拍照并保存信息。我已经达到了能够显示进入 ImageView 的图像的地步。但是,我无法将拍摄的图像保存到手机的内部存储器中。这意味着,每当我尝试保存信息时,包括 ImageView(来自相机的图像),只有图像会消失。我在http://www.androidhive.info/2013/09/android-working-with-camera-api/ 上遵循了本指南,但它不起作用,因为我的手机没有 SD 卡存储。
我在这里阅读了很多问题,但它们并没有解决问题。
因此,如果有人能给我详细说明如何编辑此代码,以便我可以将图像保存/读取到/从我的内部存储中,那就太好了!谢谢你。
/**
* returning image / video
*/
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
}
这是我当前的代码:
private static final String IMAGE_DIRECTORY_NAME = "Hello Camera";
private Uri fileUri; // file url to store image/video
private ImageView ItemPic;
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_entry);
date = (EditText) findViewById(R.id.date);
amount = (EditText) findViewById(R.id.amount);
item = (EditText) findViewById(R.id.item);
add = (Button) findViewById(R.id.add_b);
add.setOnClickListener(onAdd);
camera = (ImageButton) findViewById(R.id.camera);
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// capture picture
captureImage();
// Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
ItemPic = (ImageView) findViewById(R.id.itempic);
entryId = getIntent().getStringExtra("ID");
if (entryId != null) {
load();
}
}
/*
* Capturing Camera Image will lauch camera app requrest image capture
*/
private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
/**
* Here we store the file url as it will be null after returning from camera
* app
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save file url in bundle as it will be null on scren orientation
// changes
outState.putParcelable("file_uri", fileUri);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// get the file url
fileUri = savedInstanceState.getParcelable("file_uri");
}
/**
* Receiving activity result method will be called after closing the camera
* */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if the result is capturing Image
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// successfully captured the image
// display it in image view
previewCapturedImage();
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
}
}
/**
* Display image from a path to ImageView
*/
private void previewCapturedImage() {
try {
ItemPic.setVisibility(View.VISIBLE);
// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
options);
ItemPic.setImageBitmap(bitmap);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
/**
* ------------ Helper Methods ----------------------
* */
/**
* Creating file uri to store image/video
*/
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
/**
* returning image / video
*/
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
【问题讨论】:
-
“它无法工作,因为我的手机没有 SD 卡存储空间”——external storage 在过去五年制造的 Android 设备上很少出现 removable storage。如果您将 USB 数据线插入手机,并且在您的开发机器上显示一个名为“Internal Storage”的卷,这就是 Android SDK 所指的外部存储。
-
啊,我明白了,感谢您澄清这一点。从我的相机拍摄的图像根本没有保存到我创建的目录中,我认为这是我返回该活动时图像未显示的原因..
-
“从我的相机拍摄的图像根本没有保存到我创建的目录中”——因为我们没有您使用相机和保存图像的代码,我们不能真的可以帮助你。
-
对不起,我已经用我使用的编码编辑了我的原始帖子。