我试图从画廊中添加一张照片,用画布将图像弄圆,然后放入ImageView中,但是我总是遇到此错误:
致命异常:主要 流程:app.innova.cristobal.neton,PID:8016 java.lang.RuntimeException:无法交付结果 ResultInfo {who = null,request = 1,result = -1,data = Intent { dat = content://com.android.providers.media.documents/document/image:38 flg = 0x1}}活动 {app.innova.cristobal.neton / app.innova.cristobal.neton.Principal3Activity}: java.lang.NullPointerException:尝试调用虚拟方法'int 空对象引用上的android.graphics.Bitmap.getWidth()' 在android.app.ActivityThread.deliverResults(ActivityThread.java:3699) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 在android.app.ActivityThread.-wrap16(ActivityThread.java) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1393) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) 在java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 原因:java.lang.NullPointerException:尝试调用虚拟 空对象上的方法'int android.graphics.Bitmap.getWidth()' 参考 在android.graphics.Bitmap.createScaledBitmap(Bitmap.java:610) 在 app.innova.cristobal.neton.Principal3Activity.onActivityResult(Principal3Activity.java:271) 在android.app.Activity.dispatchActivityResult(Activity.java:6428) 在android.app.ActivityThread.deliverResults(ActivityThread.java:3695) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 在android.app.ActivityThread.-wrap16(ActivityThread.java) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1393) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) 在java.lang.reflect.Method.invoke(本机方法)
这是我的主要代码:
myimageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select image"), 1);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
imagepath = getPath(selectedImageUri);
Bitmap bitmap=BitmapFactory.decodeFile(imagepath);
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
//Resized image
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, 150, 150, true);
resizedBitmap.compress(Bitmap.CompressFormat.JPEG,50,bytearrayoutputstream);
//Round image
int min = Math.min(resizedBitmap.getWidth(), resizedBitmap.getHeight());
Bitmap bitmapRounded = Bitmap.createBitmap(min, min, resizedBitmap.getConfig());
Canvas canvas = new Canvas(bitmapRounded);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(new BitmapShader(resizedBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
canvas.drawRoundRect((new RectF(0.0f, 0.0f, min, min)), min / 2, min / 2, paint);
myimageView.setImageBitmap(bitmapRounded);
}
}
在清单文件中,我分配了以下权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
但是它总是返回一个空位图。
关于我的代码有什么问题的一些建议?
谢谢!
答案 0 :(得分:1)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
//Resized image
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, 150, 150, true);
resizedBitmap.compress(Bitmap.CompressFormat.JPEG,50,bytearrayoutputstream);
//Round image
int min = Math.min(resizedBitmap.getWidth(), resizedBitmap.getHeight());
Bitmap bitmapRounded = Bitmap.createBitmap(min, min, resizedBitmap.getConfig());
Canvas canvas = new Canvas(bitmapRounded);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(new BitmapShader(resizedBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
canvas.drawRoundRect((new RectF(0.0f, 0.0f, min, min)), min / 2, min / 2, paint);
myimageView.setImageBitmap(bitmapRounded);
}
您需要一个内容解析器来打开输入流到所选图像URI。 Uri.getpath()返回抽象路径。
答案 1 :(得分:0)
添加权限“ checkPermissionREAD_EXTERNAL_STORAGE”
first_object.attr1 = other_columns.attr1
...
} }