Attempt to invoke virtual method 'void android.widget.ImageView.setImageURI(android.net.Uri)' on a null object reference

huangapple go评论176阅读模式
英文:

Attempt to invoke virtual method 'void android.widget.ImageView.setImageURI(android.net.Uri)' on a null object reference

问题

以下是您提供的代码的翻译:

  1. package com.example.foodforpoor;
  2. import androidx.annotation.NonNull;
  3. import androidx.annotation.Nullable;
  4. import androidx.appcompat.app.AppCompatActivity;
  5. import androidx.core.app.ActivityCompat;
  6. import androidx.core.content.ContextCompat;
  7. import androidx.core.content.FileProvider;
  8. import android.Manifest;
  9. import android.app.Activity;
  10. import android.content.Intent;
  11. import android.content.pm.PackageManager;
  12. import android.graphics.Bitmap;
  13. import android.net.Uri;
  14. import android.os.Bundle;
  15. import android.os.Environment;
  16. import android.provider.MediaStore;
  17. import android.util.Log;
  18. import android.view.View;
  19. import android.widget.Button;
  20. import android.widget.ImageView;
  21. import android.widget.Toast;
  22. import com.google.android.gms.dynamic.IFragmentWrapper;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.net.URI;
  26. import java.net.URL;
  27. import java.text.SimpleDateFormat;
  28. import java.util.Date;
  29. public class Upload extends AppCompatActivity {
  30. private static final String TAG = "Upload Activity";
  31. public static final int CAMERA_PERMISSION_CODE = 101;
  32. public static final int CAMERA_REQUEST_CODE = 102;
  33. String currentPhotoPath;
  34. private Button mCamera, mGallery;
  35. private ImageView mImageView;
  36. @Override
  37. protected void onCreate(Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. setContentView(R.layout.activity_upload);
  40. mCamera = (Button) findViewById(R.id.cameraBtn);
  41. mGallery = (Button) findViewById(R.id.galleryBtn);
  42. mCamera.setOnClickListener(new View.OnClickListener() {
  43. @Override
  44. public void onClick(View v) {
  45. askCameraPermission();
  46. }
  47. });
  48. mGallery.setOnClickListener(new View.OnClickListener() {
  49. @Override
  50. public void onClick(View v) {
  51. showToast("Gallery button clicked");
  52. }
  53. });
  54. }
  55. private void askCameraPermission() {
  56. if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
  57. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_CODE);
  58. } else {
  59. dispatchTakePictureIntent();
  60. }
  61. }
  62. @Override
  63. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  64. if (requestCode == CAMERA_PERMISSION_CODE) {
  65. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  66. dispatchTakePictureIntent();
  67. } else {
  68. showToast("We need camera to use this feature");
  69. }
  70. }
  71. }
  72. private void openCamera() {
  73. showToast("Camera Open Request");
  74. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  75. startActivityForResult(intent, CAMERA_REQUEST_CODE);
  76. }
  77. @Override
  78. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  79. super.onActivityResult(requestCode, resultCode, data);
  80. if (requestCode == CAMERA_REQUEST_CODE) {
  81. if (resultCode == Activity.RESULT_OK) {
  82. Uri uri = Uri.parse(currentPhotoPath);
  83. mImageView.setImageURI(uri);
  84. }
  85. }
  86. }
  87. private File createImageFile() throws IOException {
  88. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  89. String imageFileName = "JPEG_" + timeStamp + "_";
  90. File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
  91. File image = File.createTempFile(
  92. imageFileName,
  93. ".jpg",
  94. storageDir
  95. );
  96. currentPhotoPath = image.getAbsolutePath();
  97. return image;
  98. }
  99. private void dispatchTakePictureIntent() {
  100. Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  101. if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
  102. File photoFile = null;
  103. try {
  104. photoFile = createImageFile();
  105. } catch (IOException ex) {
  106. }
  107. if (photoFile != null) {
  108. Uri photoURI = FileProvider.getUriForFile(this,
  109. "com.example.android.fileprovider",
  110. photoFile);
  111. takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
  112. startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
  113. }
  114. }
  115. }
  116. private void showToast(String msg) {
  117. Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
  118. }
  119. }

请注意,我已经根据您的要求,只返回了翻译好的代码部分。如果您有其他翻译需求或问题,请随时提问。

英文:

Here is my Activity.
The main goal of this activity is

  1. open camera
  2. take a picture and save it to external storage
  3. load that image into mImageView using setImageUri

This activity contains 2 buttons(mCamera, mGallery) and 1 imageView (mImageView). if mCamera is clicked, firstly permission for opening camera is asked. if user allows the permission, dispatchTakePictureIntent() is called.

  1. package com.example.foodforpoor;
  2. import androidx.annotation.NonNull;
  3. import androidx.annotation.Nullable;
  4. import androidx.appcompat.app.AppCompatActivity;
  5. import androidx.core.app.ActivityCompat;
  6. import androidx.core.content.ContextCompat;
  7. import androidx.core.content.FileProvider;
  8. import android.Manifest;
  9. import android.app.Activity;
  10. import android.content.Intent;
  11. import android.content.pm.PackageManager;
  12. import android.graphics.Bitmap;
  13. import android.net.Uri;
  14. import android.os.Bundle;
  15. import android.os.Environment;
  16. import android.provider.MediaStore;
  17. import android.util.Log;
  18. import android.view.View;
  19. import android.widget.Button;
  20. import android.widget.ImageView;
  21. import android.widget.Toast;
  22. import com.google.android.gms.dynamic.IFragmentWrapper;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.net.URI;
  26. import java.net.URL;
  27. import java.text.SimpleDateFormat;
  28. import java.util.Date;
  29. public class Upload extends AppCompatActivity {
  30. private static final String TAG = "Upload Activity";
  31. public static final int CAMERA_PERMISSION_CODE = 101; //It is used to identify the particular permission
  32. public static final int CAMERA_REQUEST_CODE = 102;
  33. String currentPhotoPath;
  34. private Button mCamera, mGallery;
  35. private ImageView mImageView;
  36. @Override
  37. protected void onCreate(Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. setContentView(R.layout.activity_upload);
  40. mCamera = (Button) findViewById(R.id.cameraBtn);
  41. mGallery = (Button) findViewById(R.id.galleryBtn);
  42. mCamera.setOnClickListener(new View.OnClickListener() {
  43. @Override
  44. public void onClick(View v) {
  45. //showToast("Camera button clicked");
  46. askCameraPermission();
  47. }
  48. });
  49. mGallery.setOnClickListener(new View.OnClickListener() {
  50. @Override
  51. public void onClick(View v) {
  52. showToast("Gallery button clicked");
  53. }
  54. });
  55. }
  56. private void askCameraPermission() {
  57. if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
  58. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_CODE);
  59. } else{
  60. // openCamera();
  61. dispatchTakePictureIntent();
  62. }
  63. }
  64. @Override
  65. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  66. if (requestCode == CAMERA_PERMISSION_CODE) {
  67. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  68. //if both of these condition are true that means the user has given the permission
  69. // and now we should open the camera
  70. dispatchTakePictureIntent();
  71. } else{
  72. showToast("We need camera to use this feature");
  73. }
  74. }
  75. }
  76. private void openCamera() {
  77. showToast("Camera Open Request");
  78. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// a specific request to OS in order to open the default camera app
  79. startActivityForResult(intent, CAMERA_REQUEST_CODE);
  80. }
  81. @Override
  82. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  83. super.onActivityResult(requestCode, resultCode, data);
  84. if (requestCode == CAMERA_REQUEST_CODE) {
  85. if (resultCode == Activity.RESULT_OK) {
  86. //File f = new File(currentPhotoPath);
  87. Log.d(TAG, "onActivityResult : resultcode == result ok and currentPhotoPath = " + currentPhotoPath);
  88. Uri uri = Uri.parse(currentPhotoPath);
  89. Log.d(TAG, "onActivityResult : uri.getPath() : "+uri.getPath());
  90. mImageView.setImageURI(uri);
  91. }
  92. }
  93. }
  94. private File createImageFile() throws IOException{
  95. //Create an image file name
  96. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  97. String imageFileName = "JPEG_" + timeStamp + "_";
  98. File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
  99. File image = File.createTempFile(
  100. imageFileName, /*prefix*/
  101. ".jpg", /*suffix*/
  102. storageDir /*directory*/
  103. );
  104. // Save a file: path for use with ACTION_VIEW intents
  105. currentPhotoPath = image.getAbsolutePath();
  106. Log.d(TAG, "onCreateImageFile() : currentPhotoPath : " + currentPhotoPath);
  107. return image;
  108. }
  109. private void dispatchTakePictureIntent(){
  110. Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  111. //Ensure that there is a camera activity to handle the intent
  112. Log.d(TAG, "onDispatchTakePictureIntent");
  113. if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
  114. //Create the file where photo should go
  115. File photoFile = null;
  116. try{
  117. photoFile = createImageFile();
  118. } catch (IOException ex){
  119. }
  120. //continue only if the file was successfully created
  121. if (photoFile != null) {
  122. Uri photoURI = FileProvider.getUriForFile(this,
  123. "com.example.android.fileprovider",
  124. photoFile);
  125. Log.d(TAG,"on photoFile!= null"+ photoURI.toString());
  126. takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
  127. startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
  128. }
  129. }
  130. }
  131. private void showToast(String msg) {
  132. Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
  133. }
  134. }

答案1

得分: 2

mImageView = findViewById(R.id.whatever_image_id) 这部分缺失吗?

英文:
  1. mImageView = findViewById(R.id.whatever_image_id) is missing?

huangapple
  • 本文由 发表于 2020年7月23日 13:45:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63047642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定