尝试保存图像时出现 Android 运行时错误
Posted
技术标签:
【中文标题】尝试保存图像时出现 Android 运行时错误【英文标题】:Android Runtime Error while trying to save Image 【发布时间】:2021-01-29 14:35:21 【问题描述】:我的 android 应用程序的 Logcat 显示错误:
2020-10-15 11:08:44.135 26149-26149/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.documentscanner, PID: 26149
java.lang.RuntimeException: Failure delivering result ResultInfowho=null, request=99, result=-1, data=Intent (has extras) to activity com.example.documentscanner/com.example.documentscanner.HomeActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:5183)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5224)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:151)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:111)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2213)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:238)
at android.app.ActivityThread.main(ActivityThread.java:7853)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:984)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
at com.example.documentscanner.HomeActivity.onActivityResult(HomeActivity.java:101)
at android.app.Activity.dispatchActivityResult(Activity.java:8309)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5176)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5224)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:151)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:111)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2213)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:238)
at android.app.ActivityThread.main(ActivityThread.java:7853)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:984)
package com.example.documentscanner;
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.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.scanlibrary.ScanActivity;
import com.scanlibrary.ScanConstants;
import java.io.IOException;
public class HomeActivity extends AppCompatActivity
private static final int PERMISSION_CODE = 1000;
ImageView scannedImageView = null;
int REQUEST_CODE = 99;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
public void checkCamera(View v)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (checkSelfPermission(Manifest.permission.CAMERA) ==
PackageManager.PERMISSION_DENIED || checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_DENIED)
String[] permission = Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE;
requestPermissions(permission, PERMISSION_CODE);
else
openCamera();
else
openCamera();
public void checkGallery(View v)
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_DENIED || checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_DENIED)
String[] permission = Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE;
requestPermissions(permission, PERMISSION_CODE);
else
openGallery();
else
openGallery();
public void openCamera()
int preference = ScanConstants.OPEN_CAMERA;
Intent intent = new Intent(this, ScanActivity.class);
intent.putExtra(ScanConstants.OPEN_INTENT_PREFERENCE, preference);
startActivityForResult(intent, REQUEST_CODE);
public void openGallery()
int preference = ScanConstants.OPEN_MEDIA;
Intent intent = new Intent(this, ScanActivity.class);
intent.putExtra(ScanConstants.OPEN_INTENT_PREFERENCE, preference);
startActivityForResult(intent, REQUEST_CODE);
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
Uri uri = data.getExtras().getParcelable(ScanConstants.SCANNED_RESULT);
Bitmap bitmap = null;
try
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
getContentResolver().delete(uri, null, null);
scannedImageView.setImageBitmap(bitmap);
catch (IOException e)
e.printStackTrace();
【问题讨论】:
【参考方案1】:scannedImageView
为空,使用前必须绑定视图。用您的 ImageView 在 XML 中的 id 替换。看OnCreate()
方法:
package com.example.documentscanner;
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.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.scanlibrary.ScanActivity;
import com.scanlibrary.ScanConstants;
import java.io.IOException;
public class HomeActivity extends AppCompatActivity
private static final int PERMISSION_CODE = 1000;
ImageView scannedImageView = null;
int REQUEST_CODE = 99;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//REPLACE WITH YOUR ID
scannedImageView = findViewById(R.id.YourId);
public void checkCamera(View v)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (checkSelfPermission(Manifest.permission.CAMERA) ==
PackageManager.PERMISSION_DENIED || checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_DENIED)
String[] permission = Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE;
requestPermissions(permission, PERMISSION_CODE);
else
openCamera();
else
openCamera();
public void checkGallery(View v)
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_DENIED || checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_DENIED)
String[] permission = Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE;
requestPermissions(permission, PERMISSION_CODE);
else
openGallery();
else
openGallery();
public void openCamera()
int preference = ScanConstants.OPEN_CAMERA;
Intent intent = new Intent(this, ScanActivity.class);
intent.putExtra(ScanConstants.OPEN_INTENT_PREFERENCE, preference);
startActivityForResult(intent, REQUEST_CODE);
public void openGallery()
int preference = ScanConstants.OPEN_MEDIA;
Intent intent = new Intent(this, ScanActivity.class);
intent.putExtra(ScanConstants.OPEN_INTENT_PREFERENCE, preference);
startActivityForResult(intent, REQUEST_CODE);
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
Uri uri = data.getExtras().getParcelable(ScanConstants.SCANNED_RESULT);
Bitmap bitmap = null;
try
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
getContentResolver().delete(uri, null, null);
scannedImageView.setImageBitmap(bitmap);
catch (IOException e)
e.printStackTrace();
【讨论】:
以上是关于尝试保存图像时出现 Android 运行时错误的主要内容,如果未能解决你的问题,请参考以下文章
使用 VBA 密码保护进行保存时出现错误消息“运行时错误‘1004’:对象‘_Workbook’的方法‘SaveAs’失败”