Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java AsyncTask获取错误的字符串参数_Java_Android_String_Bitmap_Android Asynctask - Fatal编程技术网

Java AsyncTask获取错误的字符串参数

Java AsyncTask获取错误的字符串参数,java,android,string,bitmap,android-asynctask,Java,Android,String,Bitmap,Android Asynctask,我正在尝试在asyncTask中加载位图,在onClick中,我将执行myAsyncTask并获取位图作为结果。我一直使用NullPointerException,因为我放入myAsyncTack的字符串参数被解码错误(请参阅logcat屏幕截图,第一行是字符串数据[position]的log.d,第二行是myAsyncTask解码方法)。在我的代码中,我需要在后台打开大量图像,如何使用myAsyncTask正确地执行此操作 执行: Log.d("Files", "DATA: "

我正在尝试在asyncTask中加载位图,在onClick中,我将执行myAsyncTask并获取位图作为结果。我一直使用NullPointerException,因为我放入myAsyncTack的字符串参数被解码错误(请参阅logcat屏幕截图,第一行是字符串数据[position]的log.d,第二行是myAsyncTask解码方法)。在我的代码中,我需要在后台打开大量图像,如何使用myAsyncTask正确地执行此操作

执行:

        Log.d("Files", "DATA: " + data[position]);
        try{
            ImageView mImg = (ImageView) vi.findViewById(R.id.imageView);
            BitmapTask btmt = new BitmapTask();
            btmt.execute(data[position]);
            mImg.setImageBitmap(btmt.get());
        } catch (Exception e){
            e.printStackTrace();
        }


        return vi;
异步:

public class BitmapTask extends AsyncTask {

        @Override
        protected Bitmap doInBackground(String... bmt) {
            Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/MANUAL/workflow/" + bmt);
            int targetWidth  = bm.getWidth() / 1;
            int targetHeight = bm.getHeight() / 1;

            Bitmap size = Bitmap.createBitmap(bm, 0, 0, targetWidth, targetHeight, matrix(), true);
            return size;
        }

        @Override
        protected void onPostExecute(Bitmap result) {

        }

    }
日志:


位于Environment.getExternalStorageDirectory()+“/MANUAL/workflow/”+bmt的文件不存在,并在BitmapTask中抛出FileNotFoundException。用try-catch环绕以避免碰撞:)

使用

公共类BitmapTask扩展了AsyncTask{
}

检查[参数]

根据需要使用

异步任务使用的三种类型如下:

Params, the type of the parameters sent to the task upon execution.
Progress, the type of the progress units published during the background computation.
Result, the type of the result of the background computation.
还加

String fileName = bmt[0];
Environment.getExternalStorageDirectory() + "/MANUAL/workflow/" + fileName;
更改此选项:

Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/MANUAL/workflow/" + bmt);
为此:

Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/MANUAL/workflow/" + bmt[0]);

您必须获取
字符串的第一个元素。。。bmt
字符串数组。

您应该使用
onPostExecute
方法将位图设置为
ImageView

任务完成后,它将在
UI
线程上执行

试着这样做:

public class BitmapTask extends AsyncTask {

    private ImageView imageView;

    @Override
    protected Bitmap doInBackground(String... bmt) {
        Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/MANUAL/workflow/" + bmt);
        int targetWidth  = bm.getWidth() / 1;
        int targetHeight = bm.getHeight() / 1;

        Bitmap size = Bitmap.createBitmap(bm, 0, 0, targetWidth, targetHeight, matrix(), true);
        return size;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        this.imageView.setImageBitmap(result);
    }

    public void setImageView(ImageView imageView){
        this.imageView = imageView;
    }


}


在logcat中,您可以看到文件为OK,请检查底部的add。您没有获得解决问题的paramsthanks,但我的图像加载时间仍然很慢,我的代码中还有其他问题吗?没有,但我建议尝试使用库快速加载图像。并且,很高兴提供帮助。享受编码。
Log.d("Files", "DATA: " + data[position]);
    try{
        ImageView mImg = (ImageView) vi.findViewById(R.id.imageView);
        BitmapTask btmt = new BitmapTask();
        btmt.setImageView(mImg);
        btmt.execute(data[position]);
    } catch (Exception e){
        e.printStackTrace();
    }


    return vi;