java - Storing a compressed image -
i able save image in internal storage unable store compressed one.
this java code
i have used getfileuri() store image in pictures directory of phone , using getimage bytes() compress file not getting error nor able compress image. able save normal image unable save compressed on.
public class mainactivity extends appcompatactivity implements view.onclicklistener { private static final string tag = "mainactivity"; private button button, buttoncompress; private string encoded_string, image_name; private bitmap bitmap; private imageview img; private file file; private uri file_uri; private progressdialog mprogressdialog; intent data; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); activitycompat.requestpermissions(this, new string[] {android.manifest.permission.write_external_storage}, 1);//for writing internal storage. img = (imageview) findviewbyid(r.id.imageview); button = (button) findviewbyid(r.id.btncam); buttoncompress = (button) findviewbyid(r.id.btncomp); button.setonclicklistener(this); buttoncompress.setonclicklistener(this); } @override public void onclick(view view) { switch (view.getid()) { case r.id.btncam: intent intentcamera = new intent(mediastore.action_image_capture); getfileuri(); intentcamera.putextra(mediastore.extra_output, file_uri); startactivityforresult(intentcamera, 10); break; case r.id.btncomp: getimagebytes(img); getfileuri(); break; } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == 10 && resultcode == result_ok) { img.setimageuri(file_uri); log.d("path is", file.getpath()); toast.maketext(mainactivity.this, "successfully written !!", toast.length_short).show(); } } private void getfileuri() { image_name = "picture1.jpg"; file = new file(environment.getexternalstoragepublicdirectory(environment.directory_pictures) + file.separator + image_name ); file_uri = uri.fromfile(file); log.d("uri is", file_uri.tostring()); } private byte[] getimagebytes(@nonnull imageview imageview) { bitmap bitmap = ((bitmapdrawable) imageview.getdrawable()).getbitmap(); bytearrayoutputstream bos = new bytearrayoutputstream(); bitmap.compress(bitmap.compressformat.jpeg, 50, bos); return bos.tobytearray(); } }
this xml file
<imageview android:id="@+id/imageview" android:layout_width="257dp" android:layout_height="300dp" app:srccompat="@mipmap/ic_launcher" tools:layout_editor_absolutex="32dp" tools:layout_editor_absolutey="98dp" android:adjustviewbounds="true" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" /> <button android:id="@+id/btncam" android:layout_width="94dp" android:layout_height="36dp" android:text="camera" tools:layout_editor_absolutey="448dp" tools:layout_editor_absolutex="145dp" android:layout_marginleft="31dp" android:layout_marginstart="31dp" android:layout_below="@+id/imageview" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_margintop="60dp" /> <button android:id="@+id/btncomp" android:layout_width="76dp" android:layout_height="41dp" android:text="compress" tools:layout_editor_absolutex="-9dp" tools:layout_editor_absolutey="-5dp" android:layout_alignbottom="@+id/btncam" android:layout_alignright="@+id/imageview" android:layout_alignend="@+id/imageview" />
you haven't written code save it. try below code image imageview, compress , save in storage directory. hope work you.
bitmap bitmapsave = null; //get image bitmap imageview bitmapsave = ((bitmapdrawable) imageview.getdrawable()).getbitmap(); //create folder want store compressed image file file = new file(environment.getexternalstoragedirectory().getpath(), "/yourappname"); if (!file.exists()) { file.mkdirs(); } // name image string filename = file.getabsolutepath() + "/" + system.currenttimemillis() + ".jpg"; fileoutputstream out = null; try { out = new fileoutputstream(filename); bitmapsave.compress(bitmap.compressformat.jpeg, 50, out); out.close(); toast.maketext(getapplicationcontext(), "saved successfully", toast.length_short).show(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); }
Comments
Post a Comment