android - Invalid URI after closing calling Activity -
i have activity multiple imageviews showing images retrieved calling camera intent. on onactivityresult store image uri returned later background processing.
the thing is: once finish calling activity, of stored uris (passed asynctask) invalid , when try load them (i.e. glide), raise nullpointerexception.
i can't keep activity running during background processing, takes time , gives users bad experience.
i've tried other methods when acquiring images thinking read permission availability , life cycle have calling method (created myself file in file system , passed camera intent, example), none of worked.
i've tried different devices , different android version , issue persists, , implicit pattern of android?
has of experienced problem?
tha camera , gallery intent calls:
public static void getcameraimage(activity activity) { intent takepictureintent = new intent(mediastore.action_image_capture); if (takepictureintent.resolveactivity(activity.getpackagemanager()) != null) { activity.startactivityforresult(takepictureintent, codes.camera_image); } } public static void getgalleryimage(activity activity){ intent intent = new intent(intent.action_get_content); intent.settype("image/*"); intent.addcategory(intent.category_openable); activity.startactivityforresult(intent, codes.gallery_image); }
onactivityresult:
@override public void onactivityresult(int requestcode, int resultcode, intent intent) { if (resultcode == result_ok && (requestcode == codes.camera_image || requestcode == codes.gallery_image)) { uri uri = intent.getdata(); glide.with(this) .load(uri) .fitcenter() .centercrop() .into((imageview) findviewbyid(myimageviewid)); saveduri = uri; } }
asynctask call:
//postimages extends asynctask postimages post = new postvehicleimages(saveduri); post.execute(); finish()
async task execution:
@override protected void doinbackground(void... params) { callservice(saveduri); return null; } private void callservice(uri saveduri) { bitmap image = null; try{ image = glide.with(app.getappcontext()) .load(saveduri) //<----- crashes here .asbitmap() .into(640, 640) .get(); }catch(exception e){ e.printstacktrace(); } retrofit retrofit = servicebuilder.getbuilder(); bytearrayoutputstream stream = new bytearrayoutputstream(); image.compress(bitmap.compressformat.jpeg, 100, stream); byte[] bytearray = stream.tobytearray(); requestbody file = requestbody.create(mediatype.parse("image/jpeg"), bytearray); vehicleservice service = retrofit.create(vehicleservice.class); call<void> call = service.sendimage(credentials, vehicleid, index, file); try { call.execute(); } catch (ioexception e) { e.printstacktrace(); } }
i call postimages after validate uri , user press button (send). asynctask receives uri, loads , sends webserver, asynctask.
i simplified little (when call asynctask pass 4 uris it), 1 or 2 sent correctly, never of them successful, raises nullpointerexception when loading bitmap uri @ statement .load(saveduri)
.
i've tried using pointed file path, apparently depends heavily on sdk version / device, , wields problems.
Comments
Post a Comment