java - Capture a photo and display it full size in ImageView in Android -


i trying capture photo , display in imageview. first part done , image saved:

    public void takephoto (view view) {     string basedir = environment.getexternalstoragedirectory().getabsolutepath();     string filename = "myphoto.jpg";     intent intent = new intent(mediastore.action_image_capture);     imagefile = new file(basedir + file.separator + filename);     uri uri = uri.fromfile(imagefile);     intent.putextra(mediastore.extra_output, uri);     intent.putextra(mediastore.extra_video_quality, 1);     startactivityforresult(intent, 0); } 

however, cannot image displayed in full size in imageview:

     protected void onactivityresult(int requestcode, int resultcode, intent data) {             if (requestcode == 0)         {             if(imagefile.exists())                     {                         toast.maketext(this,"file saved!",toast.length_short).show();                      imageview myimage = (imageview) findviewbyid(r.id.imageview1);                         bitmap bmimg = bitmapfactory.decodefile("imagefile");                         myimage.setimagebitmap(bmimg);                     }                      else                     {                         toast.maketext(this,"file not saved!",toast.length_short).show();                     }         } } 

the xml layout is:

<?xml version="1.0" encoding="utf-8"?>  <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:paddingbottom="@dimen/activity_vertical_margin"      android:paddingleft="@dimen/activity_horizontal_margin"      android:paddingright="@dimen/activity_horizontal_margin"      android:paddingtop="@dimen/activity_vertical_margin"      android:orientation="vertical"      tools:context="com.example.android.trial3.mainactivity">        <button          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="shoot"          android:onclick="takephoto"/>           <imageview          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:id="@+id/imageview1"/>    </linearlayout>

and manifest.xml file:

<?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"            package="com.example.android.trial3">        <application          android:allowbackup="true"          android:icon="@mipmap/ic_launcher"          android:label="@string/app_name"          android:supportsrtl="true"          android:theme="@style/apptheme">          <activity android:name=".mainactivity">              <intent-filter>                  <action android:name="android.intent.action.main"/>                    <category android:name="android.intent.category.launcher"/>              </intent-filter>          </activity>      </application>    </manifest>

i'm getting following error:

bitmapfactory: unable decode stream: java.io.filenotfoundexception: /storage/emulated/0 

any appreciated.

check out docs on decodefile method's pathname parameter.

string: complete path name file decoded.

you need pass absolute path of image file bitmapfactory.decodefile().

replace following line:

bitmapfactory.decodefile("imagefile"); 

with:

bitmapfactory.decodefile(imagefile.getabsolutepath()); 

also, seem missing following permissions in manifest.xml:

android.permission.write_external_storage 

so manifest should this:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"           package="com.example.android.trial3">      <uses-permission android:name="android.permission.write_external_storage" />      <application         android:allowbackup="true"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:supportsrtl="true"         android:theme="@style/apptheme">         <activity android:name=".mainactivity">             <intent-filter>                 <action android:name="android.intent.action.main"/>                  <category android:name="android.intent.category.launcher"/>             </intent-filter>         </activity>     </application>  </manifest> 

on api level 23+ need request permission @ runtime.

check this link out on how request runtime permissions.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -