简体   繁体   中英

Android: problems reading shared image by other apps

I am developing an app that uses images shared from other apps (eg gallery, browser).

The code:

    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();
        }
    }
}

When I share image from browser for the first time, it works fine. Fragment is started and image can be loaded to ImageView. But for the second time I use share option of the browser, it is not loading the new image. (Only if I clear application data manually.)

UPDATE (ImageView image load in fragmentEntityEdit): in 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);
    }

Do you know how can I reach newer images?

From the code you have posted, the issue is related to your Glide call. Glide is not loading the new request into your ImageView , due to its cache.

In order to fix it, you can try something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM