1

I had gone through similar answers and what i came up with is i hav tried coding my first app. Now i am running into errors.

I have an ImageView in one activity, which onclick calls a method (defined in corresponding class)

this method invokes camera , captures image and send it to another activity for display. when i try to display image in same activity where it was captured, it just works fine but when i try to pass it via intent it gives me error and erros.

here is my code Activity1 (fragment_selfin_review.xml)

    <RelativeLayout 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"
tools:context="com.ardortech.restofun.SelfinReview$PlaceholderFragment" 
android:background="@color/black"
>
    <ImageView
    android:id="@+id/click_selfie"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/blank_space"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/click_selfie"
    android:gravity="center"
    android:src="@drawable/ic_camera"
    android:clickable="true"
    android:onClick="clickSelfie" />
</RelativeLayout>

and here is the code for java class

    package com.mysite.myapp;

    import java.io.ByteArrayOutputStream;
    import java.io.File;

    import android.app.Activity;
    import android.content.ContentResolver;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.support.v4.app.Fragment;
    import android.support.v7.app.ActionBarActivity;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Toast;
    public class SelfinReview extends ActionBarActivity {
    public static final int TAKE_PICTURE = 0;
    private Uri mUri;
    private Bitmap mPhoto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selfin_review);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }
     /**  Code for onclick Camera Image Method
     * 
     */

    public void clickSelfie(View v){

        Intent i = new Intent("android.media.action.IMAGE_CAPTURE");

        File f = new File(Environment.getExternalStorageDirectory(),  "photo.jpg");
        i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
       mUri = Uri.fromFile(f);
      startActivityForResult(i, TAKE_PICTURE);
    }

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


           switch (requestCode) {
           case TAKE_PICTURE:
               if (resultCode == Activity.RESULT_OK) {
             getContentResolver().notifyChange(mUri, null);
                  ContentResolver cr = getContentResolver();
                  try {
                      Intent selfiSrc = new Intent(this,SelfiEdit.class);
                      selfiSrc.putExtra("imgurl", mUri.toString());
                       startActivity(selfiSrc);

                   } catch (Exception e) {
                        Toast.makeText(this, e.getMessage(),Toast.LENGTH_SHORT).show();
                      } 

                 }
           }
    }

       /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_selfin_review,
                    container, false);
            return rootView;
        }
    }       


     }

And in the receiving activity, here how i get my intent

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_selfi_edit);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
        .add(R.id.container, new PlaceholderFragment()).commit();
        }
    /**
     * Receiving image data from the Previous activity
     */
        ImageView previewThumbnail = (ImageView) findViewById(R.id.selfie_holder);
        String rcvimgpath=getIntent().getStringExtra("imgurl");
        previewThumbnail.setImageBitmap(BitmapFactory.decodeFile(rcvimgpath));

    }

but when i am running, i am getting this error like

06-19 08:38:25.108: D/(1112): HostConnection::get() New Host Connection established
    0xb8eae210, tid 1112
    06-19 08:38:25.238: W/EGL_emulation(1112): eglSurfaceAttrib not implemented
    06-19 08:38:25.288: D/OpenGLRenderer(1112): Enabling debug mode 0
    06-19 08:38:28.438: W/EGL_emulation(1112): eglSurfaceAttrib not implemented
    06-19 08:38:44.078: W/EGL_emulation(1112): eglSurfaceAttrib not implemented
    06-19 08:38:44.668: E/BitmapFactory(1112): Unable to decode stream:
   java.io.FileNotFoundException: /file:/storage/sdcard/photo.jpg: open failed: ENOENT (No
   such file or directory)
    06-19 08:38:44.758: D/AndroidRuntime(1112): Shutting down VM
    06-19 08:38:44.758: W/dalvikvm(1112): threadid=1: thread exiting with uncaught
    exception (group=0xb4a3fba8)
    06-19 08:38:45.018: E/AndroidRuntime(1112): FATAL EXCEPTION: main
    06-19 08:38:45.018: E/AndroidRuntime(1112): Process: com.mysite.myapp, PID: 1112
    06-19 08:38:45.018: E/AndroidRuntime(1112): java.lang.RuntimeException: Unable to start
    activity ComponentInfo{com.mysite.myapp/com.mysite.myapp.SelfiEdit}:
    java.lang.NullPointerException
    06-19 08:38:45.018: E/AndroidRuntime(1112):at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
    06-19 08:38:45.018: E/AndroidRuntime(1112):     at
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    06-19 08:38:45.018: E/AndroidRuntime(1112):     at
    android.app.ActivityThread.access$800(ActivityThread.java:135)
    06-19 08:38:45.018: E/AndroidRuntime(1112):     at
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    06-19 08:38:45.018: E/AndroidRuntime(1112):     at
    android.os.Handler.dispatchMessage(Handler.java:102)
    06-19 08:38:45.018: E/AndroidRuntime(1112):     at
    android.os.Looper.loop(Looper.java:136)
    06-19 08:38:45.018: E/AndroidRuntime(1112):     at
    android.app.ActivityThread.main(ActivityThread.java:5017)
    06-19 08:38:45.018: E/AndroidRuntime(1112):     at
    java.lang.reflect.Method.invokeNative(Native Method)
    06-19 08:38:45.018: E/AndroidRuntime(1112):     at
    java.lang.reflect.Method.invoke(Method.java:515)
    06-19 08:38:45.018: E/AndroidRuntime(1112):     at
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    06-19 08:38:45.018: E/AndroidRuntime(1112):     at
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    06-19 08:38:45.018: E/AndroidRuntime(1112):     at
    dalvik.system.NativeStart.main(Native Method)
    06-19 08:38:45.018: E/AndroidRuntime(1112): Caused by: java.lang.NullPointerException
    06-19 08:38:45.018: E/AndroidRuntime(1112):     at
    com.mysite.myapp.SelfiEdit.onCreate(SelfiEdit.java:47)
    06-19 08:38:45.018: E/AndroidRuntime(1112):     at
    android.app.Activity.performCreate(Activity.java:5231)
    06-19 08:38:45.018: E/AndroidRuntime(1112):     at
    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    06-19 08:38:45.018: E/AndroidRuntime(1112):     at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

i tried displaying image in the same activity where it was captured, and that worked like a charm. But its giving error when passing image url to another activity.

can anybody point me what is going wrong?? Thanks

echoashu
  • 912
  • 3
  • 14
  • 31
  • thank you @dumazy , but i wanted to use clickSlefie() as one of my custom method. i dont even know something like this exist ;-) well let me try ur suggestion and revert back. – echoashu Jun 20 '14 at 09:09
  • I followed some other answers threads on SOF and got my question resolved. Wondering how wud i post my solution ?? can i answer my own question? – echoashu Jun 25 '14 at 12:30

1 Answers1

2

Change the clickSelfie method like this

public void clickSelfie() {  
  Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(cameraIntent, TAKE_PICTURE); 
}

onactivityResult like this

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

    if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {  
        Uri u = data.getData();
        Intent selfiSrc = new Intent(this, SelfiEdit.class);
    selfiSrc.putExtra("imgurl", u);
    startActivity(selfiSrc);
    } 

}

And the receiving activities oncreate like this

Bundle extras= getIntent().getExtras();
if(extras!=null)
{
   path = (Uri) extras.get("imgurl");
}
ImageView img = (ImageView)findViewById(R.id.click_selfie);
 img.setImageURI(path);

It works like a charm in my device

this can help you more for wide number of devices

Community
  • 1
  • 1
Jiju Induchoodan
  • 4,236
  • 1
  • 22
  • 24
  • 1
    passing a bitmap which is going to cause the activity to force close due to OOM is not good practice, you are not scaling the image in anyway before you pass it to the other activity, best solution will be getting the uri path from the intent and pass it to the other activity.... since he is using onActivityResult.. – Kosh Jun 20 '14 at 08:10
  • k0sh you pointed correct. i already tried the bitmap method but that just not correct. However i too prefered passing image uri. let me try wid @Jiju Induchoodan's edited answer, and revert back. Thank You – echoashu Jun 20 '14 at 09:14
  • @Jiju Induchoodan , when trying ur suggestion, in onactivityResult 'Uri u = intent.getData();' gives error in eclipse. may be we have to receive intent first right? so shud i put this before 'Intent intent= getIntent()' and then try to receive data ? – echoashu Jun 20 '14 at 09:23
  • @Jiju Induchoodan , that gave me nullpointexception error. :-( – echoashu Jun 20 '14 at 09:37
  • @echoashu, it is data.Sorry i misplaced :( Updated the answer too – Jiju Induchoodan Jun 20 '14 at 09:46
  • thanks @Jiju Induchoodan for efforts but its giving me NullPoingException Again. Also the 'path' variable used in receiving activity, i defined it like 'private Uri path' but dosent work . – echoashu Jun 20 '14 at 10:00
  • @echoashu make sure you are using the intent object from the call back method. – Jiju Induchoodan Jun 20 '14 at 10:04
  • sorry @JijuInduchoodan but the code is exactly as you pointed. May be i cant get what u told "make sure using intent object from call back". do you mean in onActivityResult ? shall there be a change in way of getting image url ?? sorry i am new in android development and just want to learn it perfectly – echoashu Jun 20 '14 at 10:13
  • yeah you should use the intent object from onActivityResult – Jiju Induchoodan Jun 20 '14 at 10:15
  • This is just frustatting! i just want to pass the captured image's url to another activity. whats wrong. nothing straightforward. i applied every method, tried passing bitmap, url but it just dont work. :-( – echoashu Jun 20 '14 at 11:38
  • Finally i end up with accepting this answer, it was actually helpful showing me simpler way to pass bitmap uri. I have also discovered a slightly different workaround, but i'm not sure where shud i share that Thanks @Jiju Induchoodan – echoashu Jun 26 '14 at 09:58