My Android photo app will not display images from the camera because the images are too large -


ok, want app enable users take photo , photo viewable them later on, within app.

the problem photos taken camera large displayed in app.

this kind of error written logcat when attempt made display new photo in app:

bitmap large uploaded texture (5312x2988, max=4096x4096) 

so need scale/resize image before stored mynewapp folder on device.

see code below, try load mynewfile.jpg after has been stored on device, if edit file source reference image file 500x500, image will displayed. will not display mynewfile.jpg large.

can tell me how resize image before save on device?

many thanks

here code:

obviously have added appropriate permission androidmanifest.xml file

<uses-permission android:name="android.permission.read_external_storage" /> <uses-permission android:name="android.permission.write_external_storage" />  <uses-feature android:name="android.hardware.camera2" /> 

the following code in takephotodocactivity.java:

    package com.example.frankjones.glovebox;      import android.content.intent;     import android.graphics.drawable.drawable;     import android.net.uri;     import android.os.bundle;     import android.provider.mediastore;     import android.support.v7.app.appcompatactivity;     import android.view.menu;     import android.view.menuitem;     import android.view.view;     import android.widget.button;     import android.widget.imageview;     import android.widget.toast;      import java.io.file;      public class takephotodocactivity extends appcompatactivity {          // set camera request         static final int cam_request = 1;          // prepare variables         protected button mtakephotobtn;         protected imageview mtakephotoimg;          // start oncreate method         @override         protected void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);             setcontentview(r.layout.activity_take_photo_doc);              // set variables             mtakephotobtn = (button) findviewbyid(r.id.takephotobtn);             mtakephotoimg = (imageview) findviewbyid(r.id.takephotoimg);              // listen button             mtakephotobtn.setonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view v) {                     intent cameraintent = new intent(mediastore.action_image_capture);                     file file = getfile();                     cameraintent.putextra(mediastore.extra_output, uri.fromfile(file));                     startactivityforresult(cameraintent, cam_request);                 }             }); // end listen button          } // end oncreate method          private file getfile() {             // specify directory store file in             file folder = new file("/storage/emulated/0/mynewapp/");              // if directory doesn't exist, create             if (!folder.exists()) {                 folder.mkdir();             }              // specify filename directory             file imagefile = new file(folder, "mynewfile.jpg");              // return filename             return imagefile;         }          @override         protected void onactivityresult(int requestcode, int resultcode, intent data) {             // display new photo in imageview             mtakephotoimg.setimagedrawable(drawable.createfrompath("/storage/emulated/0/mynewapp/mynewfile.jpg"));              // toast success message             toast.maketext(this, "image saved!", toast.length_long).show();        }          @override         public boolean oncreateoptionsmenu(menu menu) {             // inflate menu; adds items action bar if present.             getmenuinflater().inflate(r.menu.menu_take_photo_doc, menu);             return true;         }          @override         public boolean onoptionsitemselected(menuitem item) {             // handle action bar item clicks here. action bar             // automatically handle clicks on home/up button, long             // specify parent activity in androidmanifest.xml.             int id = item.getitemid();              //noinspection simplifiableifstatement             if (id == r.id.action_settings) {                 return true;             }              return super.onoptionsitemselected(item);         }     } 

and lastly, layout file activity_take_photo_doc.xml

    <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"         xmlns:tools="http://schemas.android.com/tools"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:orientation="vertical"         android:paddingbottom="16dp"         android:paddingleft="16dp"         android:paddingright="16dp"         android:paddingtop="16dp"         tools:context="com.example.frankjones.glovebox.takephotodocactivity">          <button             android:id="@+id/takephotobtn"             android:layout_width="200dp"             android:layout_height="wrap_content"             android:layout_gravity="center_horizontal"             android:text="take photo" />          <imageview             android:id="@+id/takephotoimg"             android:layout_width="350dp"             android:layout_height="300dp"             android:layout_gravity="center_horizontal"             android:layout_margintop="20dp"             android:src="@drawable/ic_glovebox_launcher_v2" />      </linearlayout> 

any appreciated!

can tell me how resize image before save on device?

you can't. not saving image @ all. third-party camera app invoked saving image. did not write third-party camera app.

your primary choices are:

  1. load image in using bitmapfactory, bitmapfactory.options, , insamplesize, takes less memory.

  2. use a third-party image-loading library handle option #1 you.

  3. use third-party imageview replacement handle option #1 you, part of other functionality (e.g., pinch zoom), such this library

also:

  • please not hardcode paths. /storage/emulated/0/ wrong hundreds of millions of people. use framework-supplied methods (mostly on environment , context) getting @ root directories of interest.

  • please not clutter user's external storage own directory. use getexternalfilesdir() (a method on context, , available on activity) @ framework-supplied directory on external storage unique app.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -