英文:
Attempt to invoke virtual method 'void android.widget.ImageView.setImageURI(android.net.Uri)' on a null object reference
问题
以下是您提供的代码的翻译:
package com.example.foodforpoor;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.dynamic.IFragmentWrapper;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Upload extends AppCompatActivity {
private static final String TAG = "Upload Activity";
public static final int CAMERA_PERMISSION_CODE = 101;
public static final int CAMERA_REQUEST_CODE = 102;
String currentPhotoPath;
private Button mCamera, mGallery;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
mCamera = (Button) findViewById(R.id.cameraBtn);
mGallery = (Button) findViewById(R.id.galleryBtn);
mCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
askCameraPermission();
}
});
mGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showToast("Gallery button clicked");
}
});
}
private void askCameraPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_CODE);
} else {
dispatchTakePictureIntent();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == CAMERA_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
dispatchTakePictureIntent();
} else {
showToast("We need camera to use this feature");
}
}
}
private void openCamera() {
showToast("Camera Open Request");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
Uri uri = Uri.parse(currentPhotoPath);
mImageView.setImageURI(uri);
}
}
}
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 ex) {
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
}
}
}
private void showToast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
}
请注意,我已经根据您的要求,只返回了翻译好的代码部分。如果您有其他翻译需求或问题,请随时提问。
英文:
Here is my Activity.
The main goal of this activity is
- open camera
- take a picture and save it to external storage
- 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.
package com.example.foodforpoor;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.dynamic.IFragmentWrapper;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Upload extends AppCompatActivity {
private static final String TAG = "Upload Activity";
public static final int CAMERA_PERMISSION_CODE = 101; //It is used to identify the particular permission
public static final int CAMERA_REQUEST_CODE = 102;
String currentPhotoPath;
private Button mCamera, mGallery;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
mCamera = (Button) findViewById(R.id.cameraBtn);
mGallery = (Button) findViewById(R.id.galleryBtn);
mCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//showToast("Camera button clicked");
askCameraPermission();
}
});
mGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showToast("Gallery button clicked");
}
});
}
private void askCameraPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_CODE);
} else{
// openCamera();
dispatchTakePictureIntent();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == CAMERA_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//if both of these condition are true that means the user has given the permission
// and now we should open the camera
dispatchTakePictureIntent();
} else{
showToast("We need camera to use this feature");
}
}
}
private void openCamera() {
showToast("Camera Open Request");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// a specific request to OS in order to open the default camera app
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
//File f = new File(currentPhotoPath);
Log.d(TAG, "onActivityResult : resultcode == result ok and currentPhotoPath = " + currentPhotoPath);
Uri uri = Uri.parse(currentPhotoPath);
Log.d(TAG, "onActivityResult : uri.getPath() : "+uri.getPath());
mImageView.setImageURI(uri);
}
}
}
private File createImageFile() throws IOException{
//Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /*prefix*/
".jpg", /*suffix*/
storageDir /*directory*/
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
Log.d(TAG, "onCreateImageFile() : currentPhotoPath : " + currentPhotoPath);
return image;
}
private void dispatchTakePictureIntent(){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Ensure that there is a camera activity to handle the intent
Log.d(TAG, "onDispatchTakePictureIntent");
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
//Create the file where photo should go
File photoFile = null;
try{
photoFile = createImageFile();
} catch (IOException ex){
}
//continue only if the file was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
Log.d(TAG,"on photoFile!= null"+ photoURI.toString());
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
}
}
}
private void showToast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
}
答案1
得分: 2
mImageView = findViewById(R.id.whatever_image_id) 这部分缺失吗?
英文:
mImageView = findViewById(R.id.whatever_image_id) is missing?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论