拍摄照片和显示缩略图时出错 android studio
error capture photo and display thumbnail android studio
你好,我正在为我的实习做一个应用程序,但我遇到了一个错误。
请帮我解决这个问题
这是activity.java
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intents
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.provider", photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
这是 onActivityResult() 代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == REQUEST_TAKE_PHOTO) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
if (resultCode == Activity.RESULT_OK){
if (requestCode == PICK_FILE_REQUEST){
if (data == null){
return;
}
Uri selectedFileUri = data.getData();
selectedFilePath = FilePath.getPath(this, selectedFileUri);
Log.i(TAG,"Selected File Path:"+selectedFilePath);
if (selectedFilePath != null && !selectedFilePath.equals("")){
tv_file_name.setText(selectedFilePath);
}else{
Toast.makeText(this, "Cannot upload file to server", Toast.LENGTH_SHORT).show();
}
}
}
}
我明白了
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { }} to activity {com.example.febryan.apptest/com.example.febryan.apptest.RequestActivity2}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4324)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4367)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1649)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference
at com.example.febryan.apptest.RequestActivity2.onActivityResult(RequestActivity2.java:311)
at android.app.Activity.dispatchActivityResult(Activity.java:7235)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4320)
请帮我解决这个问题。谢谢
默认 Android 相机应用程序 return 只有在 returned Intent 中传回缩略图时才是非空 Intent。如果您将 EXTRA_OUTPUT 与要写入的 URI 一起传递,它将 return 一个空意图,并且图片位于您传入的 URI 中。
因此,由于您的意图是通过 EXTRA_OUTPUT 位置,因此它将始终使用数据重新调整空值,即在您的情况下,现在照片/捕获的图像位于 photoURI
路径。
因此,与其查看 onActivityResult()
中的数据意图,不如从 photoURI 路径中获取它。像 below.Here 照片文件是文件 EXTRA_OUTPUT 您保存实际相机图像的位置
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == REQUEST_TAKE_PHOTO) {
Uri uri = Uri.fromFile(photoFile); // Here photo file is the file EXTRA_OUTPUT location where you saved the actual camera image
Bitmap bitmap;
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
bitmap = crupAndScale(bitmap, 300); // if you mind scaling
pofileImageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imageView.setImageBitmap(bitmap);
}
}
如果您想裁剪和缩放此图像,可能就像您提到的缩略图一样。
public static Bitmap crupAndScale (Bitmap source,int scale){
int factor = source.getHeight() <= source.getWidth() ? source.getHeight(): source.getWidth();
int longer = source.getHeight() >= source.getWidth() ? source.getHeight(): source.getWidth();
int x = source.getHeight() >= source.getWidth() ?0:(longer-factor)/2;
int y = source.getHeight() <= source.getWidth() ?0:(longer-factor)/2;
source = Bitmap.createBitmap(source, x, y, factor, factor);
source = Bitmap.createScaledBitmap(source, scale, scale, false);
return source;
}
你好,我正在为我的实习做一个应用程序,但我遇到了一个错误。
请帮我解决这个问题
这是activity.java
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intents
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.provider", photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
这是 onActivityResult() 代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == REQUEST_TAKE_PHOTO) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
if (resultCode == Activity.RESULT_OK){
if (requestCode == PICK_FILE_REQUEST){
if (data == null){
return;
}
Uri selectedFileUri = data.getData();
selectedFilePath = FilePath.getPath(this, selectedFileUri);
Log.i(TAG,"Selected File Path:"+selectedFilePath);
if (selectedFilePath != null && !selectedFilePath.equals("")){
tv_file_name.setText(selectedFilePath);
}else{
Toast.makeText(this, "Cannot upload file to server", Toast.LENGTH_SHORT).show();
}
}
}
}
我明白了
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { }} to activity {com.example.febryan.apptest/com.example.febryan.apptest.RequestActivity2}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4324)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4367)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1649)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference
at com.example.febryan.apptest.RequestActivity2.onActivityResult(RequestActivity2.java:311)
at android.app.Activity.dispatchActivityResult(Activity.java:7235)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4320)
请帮我解决这个问题。谢谢
默认 Android 相机应用程序 return 只有在 returned Intent 中传回缩略图时才是非空 Intent。如果您将 EXTRA_OUTPUT 与要写入的 URI 一起传递,它将 return 一个空意图,并且图片位于您传入的 URI 中。
因此,由于您的意图是通过 EXTRA_OUTPUT 位置,因此它将始终使用数据重新调整空值,即在您的情况下,现在照片/捕获的图像位于 photoURI
路径。
因此,与其查看 onActivityResult()
中的数据意图,不如从 photoURI 路径中获取它。像 below.Here 照片文件是文件 EXTRA_OUTPUT 您保存实际相机图像的位置
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == REQUEST_TAKE_PHOTO) {
Uri uri = Uri.fromFile(photoFile); // Here photo file is the file EXTRA_OUTPUT location where you saved the actual camera image
Bitmap bitmap;
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
bitmap = crupAndScale(bitmap, 300); // if you mind scaling
pofileImageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imageView.setImageBitmap(bitmap);
}
}
如果您想裁剪和缩放此图像,可能就像您提到的缩略图一样。
public static Bitmap crupAndScale (Bitmap source,int scale){
int factor = source.getHeight() <= source.getWidth() ? source.getHeight(): source.getWidth();
int longer = source.getHeight() >= source.getWidth() ? source.getHeight(): source.getWidth();
int x = source.getHeight() >= source.getWidth() ?0:(longer-factor)/2;
int y = source.getHeight() <= source.getWidth() ?0:(longer-factor)/2;
source = Bitmap.createBitmap(source, x, y, factor, factor);
source = Bitmap.createScaledBitmap(source, scale, scale, false);
return source;
}