[Android Toast打开画廊并选择图像后未显示

问题描述 投票:0回答:1

我想在单击“ btnSearchImg”按钮时显示Toast(或进度条)。如果在上传图像之前,按钮单击说“从图库中首先选择图像”,而在上传图像之后单击按钮说“等待”。上传图像前的吐司工作正常,但上传后效果不佳!我的整个活动代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_by_image);
    Toasty.Config.getInstance().setTextSize(15).apply();

    mSharedPreferences = getSharedPreferences("PREFERENCE", Context.MODE_PRIVATE);
    mEditor = mSharedPreferences.edit();
    mEditor.putString(PREF_SKIP,null);
    if(mSharedPreferences.contains(PREF_SKIP)){
        Log.i("payment", "true");
    } else {
        try {

        }catch (Exception e){
            Toast.makeText(getApplicationContext(), "ERORR", Toast.LENGTH_LONG).show();
        }
    }

    context = getApplicationContext();
    pbImgRetrvialProccess = (ProgressBar)findViewById(R.id.pbImgRetrvialProccess);
    tvPermissionLoadImg = (TextView)findViewById(R.id.tvPermissionLoadImg);
    tvPermissionLoadImg.setTypeface(Base.getIranSansFont());
    TextView tvSearchImageToolBarText = (TextView) findViewById(R.id.tvSearchImageToolBarText);
    tvSearchImageToolBarText.setTypeface(Base.getIranSansFont());
    ivGalleryImgLoad = (ImageView) findViewById(R.id.ivGalleryImgLoad);
    btnSearchImgLoad = (Button) findViewById(R.id.btnSearchImgLoad);
    btnSearchImg = (Button) findViewById(R.id.btnSearchImg);
    btnSearchImgLoad.setOnClickListener(this);
    btnSearchImg.setOnClickListener(this);
}

public StringBuilder uniformQuantization( File filePath ){...}

private StringBuilder chHistogram( Mat newImage ){...}

private void CopyAssets(String filename){...}

private void copyFile(InputStream in, OutputStream out) throws IOException {...}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btnSearchImgLoad:
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PICK_IMAGE);}
            OpenGallery();
            break;

        case R.id.btnSearchImg:
            Toasty.success(getBaseContext(), "Waiting...", Toast.LENGTH_LONG).show();
            FindSimilarRequestedImage();              
            break;
    }
}

private void OpenGallery() {
    Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(gallery, PICK_IMAGE);
}


private void FindSimilarRequestedImage() {

    if (ivGalleryImgLoad.getDrawable() != null) {
        File loadedImageFilePath = new File(getRealPathFromURI(selectedImageUri));
        queryFeatureX = uniformQuantization(loadedImageFilePath);
        dateiLesenStringBuilder();
    } else {
        Toasty.error(getBaseContext(), "first pick image from gallary", Toast.LENGTH_LONG).show();
    }

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK && requestCode == PICK_IMAGE && data != null) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PICK_IMAGE);

        } else {
            selectedImageUri = data.getData();
            ivGalleryImgLoad.setImageURI(selectedImageUri);
            tvPermissionLoadImg.setVisibility(View.INVISIBLE);
        }
    }
}

private String getRealPathFromURI(Uri contentURI) {
    String result;
    Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) { // Source is Dropbox or other similar local file path
        result = contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        result = cursor.getString(idx);
        cursor.close();
    }
    return result;
}

private void dateiLesenStringBuilder() {
    FEATURE_PATH = context.getCacheDir().getPath() + "/" + FEATURE_NAME;
    try {
        FileOutputStream out = new FileOutputStream(FEATURE_PATH);
        InputStream in = context.getAssets().open("coins/"+FEATURE_NAME);
        byte[] buffer = new byte[1024];
        int ch;
        while ((ch = in.read(buffer)) > 0){
            out.write(buffer, 0, ch);
        }
        out.flush();
        out.close();
        in.close();
    }catch (Exception e){
        System.out.println(e);
    }
    final File featureList = new File(FEATURE_PATH);
    Runnable runner = new Runnable() {
        BufferedReader in = null;
        @Override
        public void run() {
            try {
                String sCurrentLine;
                int lines = 0;
                BufferedReader newbw = new BufferedReader(new FileReader(featureList.getAbsolutePath()));
                while (newbw.readLine() != null)
                    lines++;
                in = new BufferedReader(new FileReader(featureList.getAbsolutePath()));
                ArrayList<Double> nDistVal = new ArrayList<Double>();
                ArrayList<String> nImagePath = new ArrayList<String>();
                int count = 0;
                while ((sCurrentLine = in.readLine()) != null) {
                    String[] featX = sCurrentLine.split(";")[1].split(" ");
                    nImagePath.add(sCurrentLine.split(";")[0]);
                    nDistVal.add(Distance.distL2(featX, queryFeatureX.toString().split(" ")));
                    count++;
                }
                ArrayList<Double> nstore = new ArrayList<Double>(nDistVal); // may need to be new ArrayList(nfit)
                double maxDistVal = Collections.max(nDistVal);
                Collections.sort(nDistVal);
                sortedNImagePath = new ArrayList<String>();
                for (int n = 0; n < nDistVal.size(); n++) {
                    int index = nstore.indexOf(nDistVal.get(n));
                    sortedNImagePath.add(nImagePath.get(index));
                    sortedNImageDistanceValues.add(String.valueOf(nDistVal.get(n) / maxDistVal));
                    String filePath = sortedNImagePath.get(0);
                    String minDistanceImg = sortedNImageDistanceValues.get(n);
                    FileNameFromPath = filePath.substring(filePath.lastIndexOf("/") + 1);
                    System.out.println("Distance values -> " + FileNameFromPath.toString());
                    System.out.println("Distance values -> " + minDistanceImg.toString());
                }
            } catch (Exception ex) {
                String x = ex.getMessage();
                System.out.println("ERORR" + x);
            }
            if (in != null) try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                Intent imgSearchedCoinNameIntent = new Intent(SearchByImageActivity.this, ImageSearchedCoinActivity.class);
                imgSearchedCoinNameIntent.putExtra("imgSearchedCoinName", FileNameFromPath);
                startActivity(imgSearchedCoinNameIntent);
            }
        }
    };
    Thread t = new Thread(runner, "Code Executer");
    t.start();
  }
}

[如果在FindSimilarRequestedImage()之后放“ Waiting ...”吐司,将显示吐司,但是单击btnSearchImg后,我需要立即显示吐司。

注意:同样在dateiLesenStringBuilder()中,我删除了这部分代码以正常流程和串行运行的线程t,但没有任何变化!

android android-toast
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.