Asynctask Get A Wrong String Parameter
I'm trying to load bitmaps in the asyncTask, in the onClick I will execute myAsyncTask and get bitmap as result. I'm stuck with NullPointerException, because String parameter, that
Solution 1:
Change this:
Bitmapbm= BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/MANUAL/workflow/" + bmt);
to this:
Bitmapbm= BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/MANUAL/workflow/" + bmt[0]);
You have to get first element of String... bmt
string array.
Solution 2:
You should use the onPostExecute
method to set the bitmap to the ImageView
.
It will be executed on the UI
thread when the task is completed.
Try something like this:
publicclassBitmapTaskextendsAsyncTask {
private ImageView imageView;
@Overrideprotected Bitmap doInBackground(String... bmt) {
Bitmapbm= BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/MANUAL/workflow/" + bmt);
inttargetWidth= bm.getWidth() / 1;
inttargetHeight= bm.getHeight() / 1;
Bitmapsize= Bitmap.createBitmap(bm, 0, 0, targetWidth, targetHeight, matrix(), true);
return size;
}
@OverrideprotectedvoidonPostExecute(Bitmap result) {
this.imageView.setImageBitmap(result);
}
publicvoidsetImageView(ImageView imageView){
this.imageView = imageView;
}
}
and
Log.d("Files", "DATA: " + data[position]);
try{
ImageViewmImg= (ImageView) vi.findViewById(R.id.imageView);
BitmapTaskbtmt=newBitmapTask();
btmt.setImageView(mImg);
btmt.execute(data[position]);
} catch (Exception e){
e.printStackTrace();
}
return vi;
Solution 3:
The file located at Environment.getExternalStorageDirectory() + "/MANUAL/workflow/" + bmt does not exist, and throws a FileNotFoundexcption in BitmapTask. Surround with try catch to avoid crash :)
Solution 4:
Use
public class BitmapTask extends AsyncTask<Check params> {
}
Check the [params]
Use <String, Integer, Void>
as you need
The three types used by an asynchronous task are the following:
Params, the typeof the parameters sent to the task upon execution.
Progress, the typeof the progress units published during the background computation.
Result, the typeof the result of the background computation.
Also add
StringfileName= bmt[0];
Environment.getExternalStorageDirectory() + "/MANUAL/workflow/" + fileName;
Post a Comment for "Asynctask Get A Wrong String Parameter"