android - Couldn't upload a picture using google photos -
i used piece of code image gallery , crop image before saving it. running , nicely android built in gallery giving nullpointerexception in onactivityresult method extras.getparcelable("data") on using google photos app on android. appreciated. in advance :d
//this called in oncreate() on clicking upload gallery button. intent galleryintent = new intent(intent.action_pick , android.provider.mediastore.images.media.external_content_uri); galleryintent.settype("image/*"); galleryintent.putextra("crop", "true"); startactivityforresult(galleryintent,pick_from_file); //this called on onactivityresult() method if (requestcode == pick_from_file && data != null) { bundle extras = data.getextras(); //get cropped bitmap extras bitmap thepic = extras.getparcelable("data"); //do whatever thepic }
it worked me.
//this onactivityresult method. if (resultcode == result_ok && data != null) { final uri selectedimage = data.getdata(); string root = environment.getexternalstoragedirectory().getabsolutepath() + "/"; file createdir = new file(root + "appname" + file.separator); if (!createdir.exists()) { createdir.mkdirs(); } simpledateformat s = new simpledateformat("ddmmyyyhhmmss"); string format = s.format(new date()); file file = new file(root + "appname" + file.separator + format); if (!file.exists()) { try { file.createnewfile(); copyfile(new file(getrealpathfromuri(selectedimage)), file); } catch (ioexception e) { e.printstacktrace(); } } string filepath = file.getabsolutepath(); bitmap bitmap = bitmapfactory.decodefile(filepath); bytearrayoutputstream bos = new bytearrayoutputstream(); bitmap.compress(bitmap.compressformat.jpeg, 85, bos); int height = bitmap.getheight(); int width = bitmap.getwidth(); bitmap bmp = bitmap.createscaledbitmap(bitmap, 100, 100, true); mimageview.setimagebitmap(bmp); } and copyfile method have used in this.
private void copyfile(file sourcefile, file destfile) throws ioexception { if (!sourcefile.exists()) { return; } filechannel source = null; filechannel destination = null; source = new fileinputstream(sourcefile).getchannel(); destination = new fileoutputstream(destfile).getchannel(); if (destination != null && source != null) { destination.transferfrom(source, 0, source.size()); } if (source != null) { source.close(); } if (destination != null) { destination.close(); } } hope works :d
Comments
Post a Comment