我正在尝试从我的图库中获取图像并将其设置为裁剪器视图。我使用this库作为我的裁剪器视图。
问题:如何解决此代码mBitmap.getWidth()
上的空对象引用错误(请参阅代码)。
我尝试了String fname=new File(absPath, "test.png").getAbsolutePath();
,但没有效果。我不认为我知道如何实现它。非常感谢我能得到的每一个帮助。提前谢谢!
代码:
public void addImage(View view) {
SharedPreferences galleryAccess_2 = getSharedPreferences("GALLERY_ACCESS", Context.MODE_PRIVATE);
boolean galleryAccessNum = galleryAccess_2.getBoolean("galleryAccess", false);
if (galleryAccessNum) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 2);
} else {
accessGallery();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 2:
if (resultCode == RESULT_OK) {
selectedImage = data.getData();
String absPath = selectedImage.toString();
//String fname=new File(absPath, "test.png").getAbsolutePath();
//String absPath = BitmapUtils.getFilePathFromUri(this, data.getData());
loadNewImage(absPath);
}
}
private void loadNewImage(String filePath) {
//Log.i(TAG, "load image: " + filePath);
mBitmap = BitmapFactory.decodeFile(filePath);
//Log.i(TAG, "bitmap: " + mBitmap.getWidth() + " " + mBitmap.getHeight());
int maxP = Math.max(mBitmap.getWidth(), mBitmap.getHeight()); //***ERROR*** on this line****
float scale1280 = (float)maxP / 1280;
if (mCropperView.getWidth() != 0) {
mCropperView.setMaxZoom(mCropperView.getWidth() * 2 / 1280f);
} else {
ViewTreeObserver vto = mCropperView.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
mCropperView.getViewTreeObserver().removeOnPreDrawListener(this);
mCropperView.setMaxZoom(mCropperView.getWidth() * 2 / 1280f);
return true;
}
});
}
mBitmap = Bitmap.createScaledBitmap(mBitmap, (int)(mBitmap.getWidth()/scale1280),
(int)(mBitmap.getHeight()/scale1280), true);
mCropperView.setImageBitmap(mBitmap);
}
编辑:此字符串是我的absPath返回的内容:content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F25732/ORIGINAL/NONE/272125879
解答:
我找到了答案。请参阅下面的代码更改。坦率地说,如果ΦXocę 웃 Пepeúpa ツ非常不负责任,我认为重复的标记。我的问题没有询问我是什么或为什么我得到NPE。我只是表明我在某个时候得到了一个NPE。这并不意味着我要求什么是NPE或为什么。
解决方案要求我使用decodeStream()而不是decodeFile()。标记重复的要点是确保对同一个问题的答案去往同一个地方,但这个答案肯定不属于那里。顺便说一下,那个建议的答案根本没有帮助。感谢@DEADMC至少试图提供帮助。
代码:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 2:
if (resultCode == RESULT_OK) {
try {
selectedImage = data.getData();
is = getContentResolver().openInputStream(selectedImage);
loadNewImage(is);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void loadNewImage(InputStream filePath) {
try {
mBitmap = BitmapFactory.decodeStream(filePath);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
//Log.i(TAG, "load image: " + filePath);
//Log.i(TAG, "bitmap: " + mBitmap.getWidth() + " " + mBitmap.getHeight());
int maxP = Math.max(mBitmap.getWidth(), mBitmap.getHeight());
float scale1280 = (float)maxP / 1280;
if (mCropperView.getWidth() != 0) {
mCropperView.setMaxZoom(mCropperView.getWidth() * 2 / 1280f);
} else {
ViewTreeObserver vto = mCropperView.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
mCropperView.getViewTreeObserver().removeOnPreDrawListener(this);
mCropperView.setMaxZoom(mCropperView.getWidth() * 2 / 1280f);
return true;
}
});
}
mBitmap = Bitmap.createScaledBitmap(mBitmap, (int)(mBitmap.getWidth()/scale1280),
(int)(mBitmap.getHeight()/scale1280), true);
mCropperView.setImageBitmap(mBitmap);
}