簡體   English   中英

Android:讀取其他應用共享的圖像時出現問題

[英]Android: problems reading shared image by other apps

我正在開發一個使用與其他應用程序(例如畫廊,瀏覽器)共享的圖像的應用程序。

編碼:

    public void handleImage() {        
    Intent intent = getIntent();
    Uri imgUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);

    if (imgUri != null){
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            InputStream imgInputStream = context.getContentResolver().openInputStream(imgUri);
            Bitmap img = (BitmapFactory.decodeStream(imgInputStream));
            img.compress(Bitmap.CompressFormat.PNG, 100, stream);

            byte[] imgByteArray = stream.toByteArray();

            Bundle bundle = new Bundle();

            bundle.putByteArray("IMAGE_BYTEARRAY", imgByteArray);

            FragmentEntityEdit fragmentEntityEdit = new FragmentEntityEdit();
            fragmentEntityEdit.setArguments(bundle);
            changeFragment(fragmentEntityEdit,true,true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

當我第一次從瀏覽器共享圖像時,它可以正常工作。 片段開始,可以將圖像加載到ImageView。 但是第二次使用瀏覽器的共享選項,它沒有加載新圖像。 (僅當我手動清除應用程序數據時。)

UPDATE(fragmentEntityEdit中的ImageView圖像加載):在OnCreateView中:

    Bundle bundle = getArguments();
    if (bundle != null) {
        imgByteArray = bundle.getByteArray("IMAGE_BYTEARRAY");
    }

    ImageView imgOfEntity = view.findViewById(R.id.imageview_imgofentity);

    if (imgByteArray != null) {
        imgOfEntity.setImageBitmap(null);

        Glide
                .with(getActivity())
                .load(imgByteArray)
                .into(imgOfEntity);
    }

您知道我如何獲得較新的圖像嗎?

從您發布的代碼來看,問題與您的Glide通話有關。 由於其緩存, Glide不會將新請求加載到ImageView

為了解決這個問題,您可以嘗試執行以下操作:

Glide.with(imgOfEntity.getContext())
    .load(imgByteArray)
    .diskCacheStrategy(DiskCacheStrategy.NONE)
    .skipMemoryCache(true)
    .into(imgOfEntity);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM