Search code examples
androidandroid-camera-intent

Not able to display and save captured image on my screen in android studio


My code is working when i just display the capture image , its not working when i add the code to save captured image in gallery.

public class MainActivity extends Activity {
    private static int RESULT_LOAD_IMG = 1;
    String imgDecodableString;
    ImageView iv;
    Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.imgView);
        Button b = (Button) findViewById(R.id.buttonLoadPicture);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path = Environment.getExternalStorageDirectory() + "/CameraImages/example.jpg";
                File file = new File(path);
                Uri outputFileUri = Uri.fromFile(file);
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
                startActivityForResult(intent, 1);
            }
        });
    }

    Bitmap bmp;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (data != null && data.getExtras() != null) {
            bmp = (Bitmap) data.getExtras().get("data");

            iv.setImageBitmap(bmp);
        }

    }
}

please help me out, i was struck

EDIT: This is My Logcat

12-23 12:44:07.701 8180-8180/com.prgguru.example D/dalvikvm﹕ Late-enabling CheckJNI 
12-23 12:44:07.741 8180-8180/com.prgguru.example D/ActivityThread﹕ handleBindApplication:com.prgguru.example 
12-23 12:44:07.741 8180-8180/com.prgguru.example D/ActivityThread﹕ setTargetHeapUtilization:0.25 
12-23 12:44:07.741 8180-8180/com.prgguru.example D/ActivityThread﹕ setTargetHeapMinFree:2097152 
12-23 12:44:07.751 8180-8180/com.prgguru.example D/ActivityThread﹕ setTargetHeapConcurrentStart:2097152 
12-23 12:44:07.761 8180-8180/com.prgguru.example V/HwPolicyFactory﹕ : success to get AllImpl object and return.... 
12-23 12:44:07.781 8180-8180/com.prgguru.example V/HwWidgetFactory﹕ : successes to get AllImpl object and return.... 
12-23 12:44:07.931 8180-8180/com.prgguru.example I/Adreno-EGL﹕ : EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LNX.LA.3.7.2.1_RB1.04.04.04.157.009_msm8916_32_LNX.LA.3.7.2.1_RB1__release_AU () OpenGL ES Shader Compiler Version: E031.24.02.07 Build Date: 07/30/14 Wed Local Branch: mybranch3995205 Remote Branch: quic/LNX.LA.3.7.2.1_rb1 Local Patches: NONE Reconstruct Branch: AU_LINUX_ANDROID_LNX.LA.3.7.2.1_RB1.04.04.04.157.009 + NOTHING 
12-23 12:44:07.951 8180-8180/com.prgguru.example D/OpenGLRenderer﹕ Enabling debug mode 0 
12-23 12:44:08.011 8180-8180/com.prgguru.example I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41d32720 time:84097175 
12-23 12:44:14.891 8180-8180/com.prgguru.example V/AudioManager﹕ playSoundEffect effectType: 0 
12-23 12:44:14.891 8180-8180/com.prgguru.example V/AudioManager﹕ querySoundEffectsEnabled... 
12-23 12:44:14.891 8180-8180/com.prgguru.example D/StubController﹕ holdAndGetPermissionType permissionType:1024 uid:10095 pid:8180 
12-23 12:44:14.891 8180-8180/com.prgguru.example D/StubController﹕ addRequestCount, mRequestCount =1 mPhoneIDRequestCount: 0 mLocationRequestCount: 0 permissionType is: 1024 
12-23 12:44:14.891 8180-8180/com.prgguru.example D/StubController﹕ holdForGetPermissionSelection mRequestCount:1 
12-23 12:44:14.901 8180-8180/com.prgguru.example D/StubController﹕ beforeShowDialogCheckResult:1 
12-23 12:44:14.901 8180-8180/com.prgguru.example D/StubController﹕ minusRequestCount, mRequestCount =0 mPhoneIDRequestCount: 0 mLocationRequestCount: 0 permissionType is: 1024 
12-23  12:44:20.441 8180-8180/com.prgguru.example I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41d32720 time:84109606 
12-23 12:44:23.131 8180-8180/com.prgguru.example I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41d32720 time:84112293

MY XML this is my xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imgView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ImageView>

<Button
    android:id="@+id/buttonLoadPicture"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="0"
    android:onClick="loadImagefromGallery"
    android:text="Load Picture" >
</Button>


Solution

  • This code works for capturing image using Camera and then storing it. If you want to store the image to a specific location, you can make the appropriate changes.

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK) {
                if (requestCode == REQUEST_CAMERA) {
                    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    assert thumbnail != null;
                    thumbnail.compress(Bitmap.CompressFormat.PNG, 100, stream);
    
                    File destination = new File(Environment.getExternalStorageDirectory(),
                            System.currentTimeMillis() + ".png");
    
                    FileOutputStream fo;
                    try {
                        destination.createNewFile();
                        fo = new FileOutputStream(destination);
                        fo.write(stream.toByteArray());
                        fo.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }