我正在尝试使用Bundel将捕获的图像传递给ImageView,但出现错误“空对象引用”

时间:2019-08-14 13:20:17

标签: java android

我正在创建一个应用程序,该应用程序需要将从相机捕获的图像放入图像视图中,我尝试了我所知道的一切,但是无法解决问题。

首先,我认为这是Android Studio中模拟器的问题,因为使用它不会出现错误,因此我选择了手机尝试尝试,然后出现问题。 其次,我开始寻找很多类似的东西,但是找不到在我的代码中起作用的解决方案。 现在我被卡住了

public class TakeSelfActivity extends Activity {

    Button tirarFoto, continuar;
    ImageView fotoTirada;
    static final int REQUEST_IMAGE_CAPTURE = 1;
    static final int REQUEST_TAKE_PHOTO = 1;
    String currentPhotoPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_self);

        tirarFoto = findViewById(R.id.tirar_foto);
        continuar = findViewById(R.id.continuar_imagem);
        fotoTirada = findViewById(R.id.foto_pessoa);

        tirarFoto.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                dispatchTakePictureIntent();


            }

        });

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            fotoTirada.setImageBitmap(imageBitmap);
        }
    }


    private File createImageFile() throws IOException {

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(imageFileName,".jpg",storageDir);

        currentPhotoPath = image.getAbsolutePath();
        return image;
    }

    private void dispatchTakePictureIntent() {

        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            File photoFile = null;

            try {
                photoFile = createImageFile();

            } catch (IOException e) {

                e.printStackTrace();

            }

            if (photoFile != null) {

                Uri photoURI = FileProvider.getUriForFile(this, "com.luckyDay.android.fileprovider", photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);

            }
        }
    }

}

file_paths.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <paths>
        <external-files-path name="my_images" />
    </paths>
</PreferenceScreen>

manifest.xml


<uses-permission android:name="android.permission.INTERNET" />
    <uses-feature android:name="android.hardware.camera"
        android:required="true" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="18"/>

 <application>

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.luckyDay.android.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"></meta-data>
        </provider>

 </application>

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.luckyday, PID: 26166
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.luckyday/com.christian.luckyday.TakeSelfActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4605)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4647)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7045)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at com.christian.luckyday.TakeSelfActivity.onActivityResult(TakeSelfActivity.java:56)
        at android.app.Activity.dispatchActivityResult(Activity.java:7759)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4598)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4647) 
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7045) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964) 

0 个答案:

没有答案