gpt4 book ai didi

java - Android 从图库中选择图像 java.lang.NullPointerException : Attempt to invoke virtual method 'boolean Bitmap.compress()' on a null object

转载 作者:行者123 更新时间:2023-12-01 19:11:19 24 4
gpt4 key购买 nike

我正在创建从选择图库到服务器的图像上传。一周前一切正常,但今天不起作用并出现此错误。

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { dat=content://media/external/images/media/61489 flg=0x1 (has extras) }} to activity {com.example.pmb/com.example.pmb.Uploadlagi}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4619)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4661)
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:1957)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7077)
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 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
at com.example.pmb.Uploadlagi.BitMapToString(Uploadlagi.java:140)
at com.example.pmb.Uploadlagi.onActivityResult(Uploadlagi.java:134)

这里是脚本

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 2) {
Uri selectedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
IDProf.setImageBitmap(thumbnail);
BitMapToString(thumbnail); <------- error mention to this line
}
}
}
public String BitMapToString(Bitmap userImage1) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
userImage1.compress(Bitmap.CompressFormat.JPEG, 80, baos);
byte[] b = baos.toByteArray();
Document_img1 = Base64.encodeToString(b, Base64.DEFAULT);
return Document_img1;
}

什么问题?以及必须修复什么?

ps:我正在三星A6上测试

最佳答案

我找到了解决方案,当我向上滚动日志时,我发现未授予权限,因此在 onCreate() 函数中,我创建了权限请求。

if (ContextCompat.checkSelfPermission(Uploadlagi.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(Uploadlagi.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}

并创建一个覆盖函数,

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode==1){
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(),"Ijin diperbolehkan, silahkan pilih gambar",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),"Ijin gagal, Anda tidak bisa upload dokumen",Toast.LENGTH_LONG).show();
}
return;
}
}

关于java - Android 从图库中选择图像 java.lang.NullPointerException : Attempt to invoke virtual method 'boolean Bitmap.compress()' on a null object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59479473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com