在我的应用程序中,我使用Android设备相机捕获图像。对于某些设备,它工作正常但有些则不然。我刚刚在LG nexus 4 E960上进行了测试,在我捕获图像后,我的应用程序崩溃而无法保存结果。 这是我的代码:
//Using intent to open camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,CAMERA_CAPTURE);
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode==RESULT_OK)
{
if(requestCode==CAMERA_CAPTURE)
{
Bitmap pictTaken = null ;
Bundle extras = data.getExtras();
if(extras.keySet().contains("data"))
{
pictTaken = (Bitmap) extras.get("data");
picUri = getIntent().getData();
}
else{
picUri = getIntent().getData();
try {
pictTaken = decodeUri(picUri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Intent cropIntent= new Intent (this, Crop.class);
cropIntent.putExtra("data", picUri.toString());
cropIntent.putExtra("pict", pictTaken);
cropIntent.putExtra("code","camera");
startActivity(cropIntent);
}
}
捕获并保存后,图像显示在名为Crop.class的下一个活动中 这是我的logcat
12-12 13:26:36.340: E/AndroidRuntime(23575): FATAL EXCEPTION: main
12-12 13:26:36.340: E/AndroidRuntime(23575): Process: com.example.cobaandroid, PID: 23575
12-12 13:26:36.340: E/AndroidRuntime(23575): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.cobaandroid/com.example.cobaandroid.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
12-12 13:26:36.340: E/AndroidRuntime(23575): at android.app.ActivityThread.deliverResults(ActivityThread.java:3368)
12-12 13:26:36.340: E/AndroidRuntime(23575): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3411)
12-12 13:26:36.340: E/AndroidRuntime(23575): at android.app.ActivityThread.access$1300(ActivityThread.java:138)
12-12 13:26:36.340: E/AndroidRuntime(23575): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
12-12 13:26:36.340: E/AndroidRuntime(23575): at android.os.Handler.dispatchMessage(Handler.java:102)
12-12 13:26:36.340: E/AndroidRuntime(23575): at android.os.Looper.loop(Looper.java:136)
12-12 13:26:36.340: E/AndroidRuntime(23575): at android.app.ActivityThread.main(ActivityThread.java:5050)
12-12 13:26:36.340: E/AndroidRuntime(23575): at java.lang.reflect.Method.invoke(Native Method)
12-12 13:26:36.340: E/AndroidRuntime(23575): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-12 13:26:36.340: E/AndroidRuntime(23575): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-12 13:26:36.340: E/AndroidRuntime(23575): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
12-12 13:26:36.340: E/AndroidRuntime(23575): at com.example.cobaandroid.MainActivity.onActivityResult(MainActivity.java:226)
12-12 13:26:36.340: E/AndroidRuntime(23575): at android.app.Activity.dispatchActivityResult(Activity.java:5433)
12-12 13:26:36.340: E/AndroidRuntime(23575): at android.app.ActivityThread.deliverResults(ActivityThread.java:3364)
12-12 13:26:36.340: E/AndroidRuntime(23575): ... 9 more
我在打开/使用大多数Android设备上工作的相机时遇到了问题,这个项目的主要目标很大程度上取决于相机的使用。请把你的帮助交给我,谢谢你。
答案 0 :(得分:12)
尝试以下代码,
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File file=getOutputMediaFile(1);
picUri = Uri.fromFile(file); // create
i.putExtra(MediaStore.EXTRA_OUTPUT,picUri); // set the image file
startActivityForResult(i, CAPTURE_IMAGE);
getOutputMediaFile(int)将在哪里,
/** Create a File for saving an image */
private File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyApplication");
/**Create the storage directory if it does not exist*/
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
return null;
}
}
/**Create a media file name*/
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == 1){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".png");
} else {
return null;
}
return mediaFile;
}
最后,
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
Intent i;
switch (requestCode) {
case CAPTURE_IMAGE:
//THIS IS YOUR Uri
Uri uri=picUri;
break;
}
}
}
干杯....:)
答案 1 :(得分:8)
对于那些无法用这里的任何答案解决这个问题的人来说,这就是方法。
您有一个成员picUri
,您可以在调用startActivityForResult
之前保存临时图像文件URI,以启动相机应用。
当应用暂停时,此信息可能会丢失(如我的情况),当您返回活动使用它来从相机获取保存的图像时,您会发现它为null
。
然后,您将获得NullPointerException
。
保留picUri
的正确方法是保存onSaveInstanceState(Bundle)
并将其恢复为onRestoreInstanceState(Bundle)
以防止信息丢失。
以下是如何操作:
// Save the activity state when it's going to stop.
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("picUri", picUri);
}
// Recover the saved state when the activity is recreated.
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
picUri= savedInstanceState.getParcelable("picUri");
}
让我头疼两个小时。
答案 2 :(得分:0)
替换
picUri = getIntent().getData();
与
picUri = data.getData();
尝试一次......
答案 3 :(得分:0)
您必须告诉相机保存图像路径,如下所示:
Uri picUri;
Intent intentAmbil = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/picture.jpg";
File imageFile = new File(imageFilePath);
picUri = Uri.fromFile(imageFile); // convert path to Uri
intentAmbil.putExtra( MediaStore.EXTRA_OUTPUT, picUri );
startActivityForResult(intentAmbil, REQUEST_MAKE_PHOTO);
@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_MAKE_PHOTO:
if (resultCode == Activity.RESULT_OK) {
// do something with mMakePhotoUri
}
return;
default: // do nothing
super.onActivityResult(requestCode, resultCode, data);
}
}
答案 4 :(得分:0)
试试这个
if (resultCode != RESULT_CANCELED) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
if (requestCode == CAMERA_REQUEST) {
if (data != null) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
Drawable drawable = imageView.getDrawable();
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
bitmap1 = bitmapDrawable.getBitmap();
}
}
}
if (bitmap1 != null) {
bitmap1 = Bitmap.createScaledBitmap(bitmap1, 512, 512, false);
imageView.setImageBitmap(bitmap1);
@SuppressWarnings("unused")
String path = SaveImage_Sta(bitmap1, "stat");
} else {
Log.i("bitmap............", "bitmap1 is null");
}
答案 5 :(得分:0)
在某些设备上onActivityResult被调用而不是活动恢复而是活动在从相机意图回调后重新启动,因此picurl可以再次为null - 我们可以使用应用程序类来保持变量的引用
我们可以创建自己的应用程序类
1.在Android清单中我们必须指定我们将要使用的应用程序类
<application android:name=".MyApplication"
>
<activity
........
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity>
....
</activity>
....
</application>
2.创建应用程序类
public class MyApplication extends Application {
private Uri picUri;
public void getPicUri()
{
return picUri;
}
public void setPictUri(Uri auri)
{
picUri = aUri;
}
}
3.Inside the Activity use
//call the camera intent and pass the created a tempfile uri and use the tempfile uri
MyApplication myApplication = (MyApplication)getApplication();
myApplication.setPictUri(aUri);
....
....
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_MAKE_PHOTO:
if (resultCode == Activity.RESULT_OK) {
// do something with mMakePhotoUri
MyApplication myApplication = (MyApplication)getApplication();
myApplication.getPicUri();// use the pic uri
}
return;
default: // do nothing
super.onActivityResult(requestCode, resultCode, data);
}
}
答案 6 :(得分:0)
我在我的应用中遇到了同样的问题。 问题是当执行onActivityResult时,数据为空,因此您无法从相机Activity获取任何内容。 应该有另一种方法来解决这个问题,而不是使用Application类,因为当你处理多张照片时它会变得非常复杂。
答案 7 :(得分:0)
看到我做了一件事可能会节省你的时间。问题是onActivityResult()中的数据对象是要查看的主要内容。在操作下一步的p =代码时,数据对象为空。你要做的就是把if-else条件放在那个并运行你的项目。
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(data !=null){
if(resultCode==RESULT_OK)
{
if(requestCode==CAMERA_CAPTURE)
{
Bitmap pictTaken = null ;
Bundle extras = data.getExtras();
if(extras.keySet().contains("data"))
{
pictTaken = (Bitmap) extras.get("data");
picUri = getIntent().getData();
}
else{
picUri = getIntent().getData();
try {
pictTaken = decodeUri(picUri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Intent cropIntent= new Intent (this, Crop.class);
cropIntent.putExtra("data", picUri.toString());
cropIntent.putExtra("pict", pictTaken);
cropIntent.putExtra("code","camera");
startActivity(cropIntent);
}
}
}else{
//on cancel event or something else you want to do
}
}
检查一下,这很简单,可以随时随地完成工作。