【问题标题】:Image is uploaded to server but not shown in Imageview application android图像已上传到服务器但未在 Imageview 应用程序 android 中显示
【发布时间】:2017-09-19 08:34:31
【问题描述】:

我已经创建了照片上传功能。用户可以拍照并从文件中选择一张照片并将其上传到服务器。

此时照片已成功上传到服务器。但是当我查看我的应用程序时,它显示的是旧图像。图像未在我的应用程序的图像视图中更新。我听说过用户可以根据需要设置他们的图像的共享偏好。

我正在使用 Volley 库从我的应用中发布数据以上传照片,并使用 Glide 库下载该照片并将其显示到应用中。

我给出了我的一小部分代码,它与照片上传和下载有关。请帮助我识别并解决问题。

public void showDialog(){
        ........
        alertButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alert.dismiss();
            }
        });

        alertListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // ListViekw Clicked item index
                if (position == 0) {

                        cameraIntent();

                }
                else if (position == 1){

                        galleryIntent();

                }
            }
        });
    }


    private void galleryIntent()
    {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE);

    }

    public void cameraIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
            File cameraFolder;
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                cameraFolder = new File(Environment.getExternalStorageDirectory(), "image/");
            } else {
                cameraFolder = getActivity().getCacheDir();
            }
            if (!cameraFolder.exists()) {
                cameraFolder.mkdirs();
            }
            String imageFileName = System.currentTimeMillis() + ".jpg";
            File photoFile = new File(cameraFolder + imageFileName);
            currentPhotoPath = photoFile.getAbsolutePath();
            if (photoFile != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                startActivityForResult(takePictureIntent, REQUEST_CAMERA);
            }
        }
    }

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

        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_FILE){
                onSelectFromGalleryResult(data);

            }

            else if (requestCode == REQUEST_CAMERA) {
                if (!TextUtils.isEmpty(currentPhotoPath)) {
                    galleryAddPic();
                    onCaptureImageResult();
                }
            }
        }
    }

    private void galleryAddPic() {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(currentPhotoPath);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.getActivity().sendBroadcast(mediaScanIntent);
    }

    private void onCaptureImageResult() {
        Bitmap bitmap = getBitmapFromPath(currentPhotoPath, 200, 200);
        image.setImageBitmap(bitmap);
        compressBitMap(bitmap);
    }

    private void onSelectFromGalleryResult(Intent data) {
        ...
    }


    private void compressBitMap(Bitmap bitmap) {
       ...
    }

    public static Bitmap getBitmapFromPath(String photoPath, int targetW, int targetH) {
        ....
    }

    private void uploadImage(final byte[] bytesArray){


    }

}

【问题讨论】:

    标签: android sharedpreferences android-volley android-glide imagedownload


    【解决方案1】:

    将图像加载到服务器的一个常见错误是为新图像使用相同的 url。在这种情况下,glide 将使用它已经缓存的前一个图像。 有 2 个解决方案

    1. 一个很好的解决方案 - 服务器应该在分配给图像的 url 中使用时间戳。
    2. 一个糟糕的解决方案 - 通过滑行跳过缓存 (skipMemoryChache())。

    【讨论】:

    • 我已经绑定了第二个(一个糟糕的解决方案),但这根本不起作用。可以请解释一下更好的解决方案。这对我真的很有帮助。我从昨天开始就坚持这个问题。我在 android 开发方面还是个新手。
    • 上传新图片时有什么办法可以替换之前的图片,
    • 第一个解决方案是在服务器内部 - 当服务器为图像分配一个 url 时,您可能会在其中使用用户 ID,对吗?然后也输入一个时间,可能是这样的:http://mydomain/uploadimage&user=userid&time=20170920183312
    【解决方案2】:

    你进入了列表的第零位,所以显示的是旧图。

    【讨论】:

      猜你喜欢
      • 2022-01-26
      • 2020-11-26
      • 2014-07-31
      • 2015-04-27
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多