我正在通过以下方式将Url转换为Uri
imgUrl = intent.getStringExtra(getString(R.string.selected_image));
Log.d(TAG, "Image URL" + imgUrl);
imageUri = Uri.parse(imgUrl) ;
Log.d(TAG, "Image URI" + imageUri);
然后将其传递给此函数
BackgroundImageResize backgroundImageResize = new BackgroundImageResize(bitmap);
backgroundImageResize.execute(imageUri);
和BackgroundImageResize将参数视为
public class BackgroundImageResize extends AsyncTask<Uri, Integer, byte[]>
现在这个方法
Log.d(TAG, "doInBackground: megabytes before compression: " + mBitmap.getByteCount() / 1000000 );
bytes = getBytesFromBitmap(mBitmap, 100);
Log.d(TAG, "doInBackground: megabytes before compression: " + bytes.length / 1000000 );
正在返回此错误
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getByteCount()' on a null object reference
在之前与一个非常聪明的人的讨论中,可以得出结论,也许,Uri是不正确的
如何传递正确的Uri?
这是doInBackground()以防万一
@Override
protected byte[] doInBackground(Uri... params) {
Log.d(TAG, "doInBackground: started.");
if(mBitmap == null){
try{
mBitmap = MediaStore.Images.Media.getBitmap(NextActivity.this.getContentResolver(), params[0]);
}catch (IOException e){
Log.e(TAG, "doInBackground: IOException: " + e.getMessage());
}
}
byte[] bytes = null;
Log.d(TAG, "doInBackground: megabytes before compression: " + mBitmap.getByteCount() / 1000000 );
bytes = getBytesFromBitmap(mBitmap, 100);
Log.d(TAG, "doInBackground: megabytes before compression: " + bytes.length / 1000000 );
return bytes;
}
我怀疑这行代码
mBitmap = MediaStore.Images.Media.getBitmap(NextActivity.this.getContentResolver(), params[0]);
没有做它的工作:P
答案 0 :(得分:1)
请在doInBackGround()中添加带有++++++++++++++++++++++++++++++++++++++
@Override
protected byte[] doInBackground(Uri... params) {
Log.d(TAG, "doInBackground: started.");
Log.d(TAG, "+++++++++++++ params[0]: " + params[0]);
if(mBitmap == null){
try{
mBitmap = MediaStore.Images.Media.getBitmap(NextActivity.this.getContentResolver(), params[0]);
}catch (IOException e){
Log.e(TAG, "doInBackground: IOException: " + e.getMessage());
}
}
Log.d(TAG, "+++++++++++++ mBitmap: " + mBitmap);
byte[] bytes = null;
Log.d(TAG, "doInBackground: megabytes before compression: " + mBitmap.getByteCount() / 1000000 );
bytes = getBytesFromBitmap(mBitmap, 100);
Log.d(TAG, "doInBackground: megabytes before compression: " + bytes.length / 1000000 );
return bytes;
}
答案 1 :(得分:1)
事实证明,Locdoc01一直都是正确的,Uri是错的,所有我必须做的就是将文件://附加到url,然后再将其解析为Uri ......
像这样imgUrl = intent.getStringExtra(getString(R.string.selected_image));
ImgUrlAppended = "file://" + imgUrl;
Log.d(TAG, "Image URL" + imgUrl);
imageUri = Uri.parse(ImgUrlAppended) ;
Log.d(TAG, "Image URI" + imageUri);
图片现在压缩了!
感谢所有支持,StackOverflow! 希望这有助于某人