我做了一个简单的相机应用程序,前几天工作正常。现在我今天再试一次,我不断在文件上得到一个nullpointer。我在代码中使用断点来查看出错的地方。
filefotoFile = maakFotoBestand();
public File maakFotoBestand() throws IOException
{
//maak een unieke filename aan aan de hand van een timestamp en zet de file storage klaar
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format (new Date());
String imgFileName = "IMAGE_" + timeStamp + "_";
File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
//sla de foto op in het geheugen aan de hand van de genoemde namen
File image = File.createTempFile(imgFileName, ".JPG", storageDirectory);
//Zeg de app waar de foto staat
imageFileLocation = image.getAbsolutePath();
return image;
}
他似乎停在了File file = File.create .........
这一行他已经回到那条线上了。如果我不是看图像的内容它的NULL我做错了吗?
EDIT ============================================== ===== 这是整个代码和logcat
package nl.compict.cameraapp;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 implements View.OnClickListener
{
private static final int ACTIVITY_START_CAMERA_APP = 1;
private ImageView mFotoCapturedImageView;
private String imageFileLocation;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFotoCapturedImageView = (ImageView) findViewById(R.id.imageView01);
}
@Override
public void onClick(View v)
{
Intent callCameraApplicationIntent = new Intent();
//Roep de Android Camera App aan met de actie om een foto te maken
callCameraApplicationIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
//
File fotoFile = null;
try
{
fotoFile = maakFotoBestand();
}
catch(IOException e)
{
//geef de error weer als die plaatsvind
e.printStackTrace();
}
//roep de CameraApp nog een keer aan en onthoud het bestand dat is aangemaakt
callCameraApplicationIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fotoFile));
//Start een activity met een request code zodat je die wee kan afvangen en wacht op een resultaat
startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMERA_APP);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
//Kijk of de request en resultcode overeenkomen met wat wij willen doen
if(requestCode == ACTIVITY_START_CAMERA_APP && resultCode == RESULT_OK)
{
//Haal de foto op aan de hand van de imageFileLocation
Bitmap fotoCaptureBitmap = BitmapFactory.decodeFile(imageFileLocation);
mFotoCapturedImageView.setImageBitmap(fotoCaptureBitmap);
}
}
public File maakFotoBestand() throws IOException
{
//maak een unieke filename aan aan de hand van een timestamp en zet de file storage klaar
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format (new Date());
String imgFileName = "IMAGE_" + timeStamp + "_";
File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
//sla de foto op in het geheugen aan de hand van de genoemde namen
File image = File.createTempFile(imgFileName, ".JPG", storageDirectory);
//Zeg de app waar de foto staat
imageFileLocation = image.getAbsolutePath();
return image;
}
}
logcat的
01-20 10:50:24.749 2398-2398/nl.compict.cameraapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: nl.compict.cameraapp, PID: 2398
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: file
at android.net.Uri.fromFile(Uri.java:452)
at nl.compict.cameraapp.MainActivity.onClick(MainActivity.java:58)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:1)
检查您的应用是否有可用的外部存储空间。其他但最不可能的是您的应用程序没有权限访问外部存储。为权限和存储可用性添加了一些检查块。
答案 1 :(得分:1)
我测试了你的代码,它运行得很完美,你只需要在清单中添加权限:
<manifest ...>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
但我必须添加一个按钮来触发捕获,如下所示:
在布局中:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="59dp" />
在课堂上:
...
buttonTakePicture = (Button) findViewById(R.id.button);
buttonTakePicture.setOnClickListener(this);
...
@Override
public void onClick(View v)
{
if(v == buttonTakePicture) {
Intent callCameraApplicationIntent = new Intent();
//Roep de Android Camera App aan met de actie om een foto te maken
callCameraApplicationIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
//
File fotoFile = null;
try {
fotoFile = maakFotoBestand();
} catch (IOException e) {
//geef de error weer als die plaatsvind
e.printStackTrace();
}
//roep de CameraApp nog een keer aan en onthoud het bestand dat is aangemaakt
callCameraApplicationIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fotoFile));
//Start een activity met een request code zodat je die wee kan afvangen en wacht op een resultaat
startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMERA_APP);
}
}