Ask Your Question
0

Null object reference while trying to show image on emulator, OpenCV4Android

asked 2015-08-30 04:22:41 -0600

Hi!

After installing Android Studio and setting up the OpenCv4Android 3.0, I tried to run a simple application which purpose was to read a image from the emulators hard disk and then show it in the app. Unfortunately I got the error "Null object reference".

This is a sample of code I have been using:

public class MainActivity extends AppCompatActivity {

static {

    if (!OpenCVLoader.initDebug()){
        Log.i("opencv", "opencv initialization failed");
    } else {
        Log.i("opencv", "opencv initialization successful");        }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    ImageView imgView = (ImageView) findViewById(R.id.sampleImageView);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Mat imgRGB = imread("../data/androidlogo.jpg");
    if(imgRGB==null){
        Log.i("opencv","Image read failed");
    }
    else {
        Log.i("opencv", "Image read successful, height: "+ imgRGB.height() + " width: " + imgRGB.width());
        Bitmap img = Bitmap.createBitmap(imgRGB.cols(), imgRGB.rows(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(imgRGB, img);
        imgView.setImageBitmap(img);
    }
}

And this is how I declared the imgview object in the xml:

ImageView android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/sampleImageView"

android:layout_centerHorizontal="true"

android:layout_toEndOf="@+id/textView"

And this is the logcat:

08-30 08:50:26.925 2715-2715/com.example.robin.opencvexample I/OpenCV/StaticHelper﹕ ----------------------------------------------------------------- 08-30 08:50:26.925 2715-2715/com.example.robin.opencvexample I/opencv﹕ opencv initialization successful 08-30 08:50:27.051 2715-2715/com.example.robin.opencvexample I/opencv﹕ Image read successful, height: 350 width: 480 08-30 08:50:27.063 2715-2715/com.example.robin.opencvexample D/AndroidRuntime﹕ Shutting down VM 08-30 08:50:27.063 2715-2715/com.example.robin.opencvexample E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.robin.opencvexample, PID: 2715 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.robin.opencvexample/com.example.robin.opencvexample.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference


Conclusion:

From reading the log, the image and the opencv was sucessfully initialized, and the height and width of the image is correct as well. But its when I try to show the image where it fails. Any suggestion?

Regards Robin

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2015-08-30 04:54:07 -0600

berak gravatar image

updated 2015-08-30 04:56:30 -0600

imread does not return null on failure, but an empty() Mat (without any pixels)

so please check: if(imgRGB.empty()) instead of if(imgRGB==null)

also, it cannot read files from the apk(zip). if you want to do that, please use loadResource

then, to load i flie from sdcard (using imread) please rater use Environment.getExternalStorageDirectory().getPath() + "/my.png"

edit flag offensive delete link more

Comments

I tried right now, but its gives the same error (which I expected, because it could read the height and width of the image correctly)

recoilRobb gravatar imagerecoilRobb ( 2015-08-30 04:56:18 -0600 )edit

I tried to put the image in the folder "sdcard" but got the error: [2015-08-30 11:49:28] Failed to push selection: Read-only file system So I tried insted to put it in the folder "data", and that went just fine. So the pathdef should be this: "/mnt/data/my.jpg" ???

recoilRobb gravatar imagerecoilRobb ( 2015-08-30 04:56:55 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-08-30 04:22:41 -0600

Seen: 3,136 times

Last updated: Aug 30 '15