本文介绍了将Url转换为Uri并将其传递给AsyncTask<>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在按以下方式将Url转换为Uri
I am converting a Url to Uri in the following way
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将参数视为
and BackgroundImageResize takes arguments as
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是不正确的
in previous discussions with a really smart person, it has been concluded that, maybe, the Uri is incorrect
如何传递正确的Uri?
这是doInBackground()以防万一
this is the doInBackground() just in case
@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
推荐答案
请在doInBackGround()中添加带有+++++++++++++++++++++++++++++++ b $ b
Please add the 2 Logs with the ++++++++++++ lines in your doInBackGround() and post your Logcat:
@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;
}
这篇关于将Url转换为Uri并将其传递给AsyncTask<>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!