简单的工作相机应用程序,避免空意图问题
- 所有更改的代码均包含在此答复中;接近于Android教程
我花了很多时间解决这个问题,所以我决定创建一个帐户并与您分享我的结果。
官方的 Android 教程“简单拍照”没有完全实现其承诺。
那里提供的代码在我的设备上无法正常工作:三星 Galaxy S4 Mini GT-I9195 运行 Android 版本 4.4.2 / KitKat / API 级别 19。
我发现问题的主要原因是在捕获照片时调用的方法中的以下行(教程中的dispatchTakePictureIntent
):
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
这导致onActivityResult
捕获到的意图为空。
为了解决这个问题,我从此前在这里得到的回复和一些有用的Github帖子中获取了很多灵感(主要是由deepwinter撰写的这篇文章 - 非常感谢他;你可能也想看看他在一个密切相关的帖子上的回复)。
遵照这些建议,我选择了删除提到的putExtra
行,并在onActivityResult()
方法中执行相应的操作,从相机中获取拍摄的图片。
获取与图片相关联的位图的决定性代码行如下:
Uri uri = intent.getData();
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
} catch (IOException e) {
e.printStackTrace();
}
我创建了一个范例应用程序,它只具备拍照、保存到SD卡并显示的功能。我认为对于像我一样在遇到这个问题时处于同样困境的人们会有所帮助,因为当前的帮助建议大多参考了相当复杂的GitHub帖子,虽然能解决问题,但对于像我这样的新手来说不太容易理解。
就默认情况下Android Studio创建新项目时的文件系统而言,我只需更改三个文件即可实现我的目的:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.simpleworkingcameraapp.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="takePicAndDisplayIt"
android:text="Take a pic and display it." />
<ImageView
android:id="@+id/image1"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>
MainActivity.java :
package com.example.android.simpleworkingcameraapp;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.Image;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private ImageView image;
static final int REQUEST_TAKE_PHOTO = 1;
String mCurrentPhotoPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image1);
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmm").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName,
".jpg",
storageDir
);
mCurrentPhotoPath = image.getAbsolutePath();
Toast.makeText(this, mCurrentPhotoPath, Toast.LENGTH_LONG).show();
return image;
}
public void takePicAndDisplayIt(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
File file = null;
try {
file = createImageFile();
} catch (IOException ex) {
}
startActivityForResult(intent, REQUEST_TAKE_PHOTO);
}
}
@Override
protected void onActivityResult(int requestCode, int resultcode, Intent intent) {
if (requestCode == REQUEST_TAKE_PHOTO && resultcode == RESULT_OK) {
Uri uri = intent.getData();
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
} catch (IOException e) {
e.printStackTrace();
}
image.setImageBitmap(bitmap);
}
}
}
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.simpleworkingcameraapp">
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
请注意,我为该问题找到的解决方案还简化了Android清单文件:由于我没有在Java代码中使用任何提供程序,因此不再需要按照Android教程建议的更改。因此,只需添加一些标准行 - 主要涉及权限 - 到清单文件即可。
另外,值得指出的是,Android Studio的自动导入可能无法处理java.text.SimpleDateFormat和java.util.Date。我不得不手动导入它们。