0

I am getting this exception every time and I don't know at which point I am wrong. Here is my Code:

package com.example.hp.hospiva;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;



public class MainActivity extends AppCompatActivity {

private boolean fileUri;
private int REQUEST_CAMERA;
private Bitmap bitmap;


public void clickPic (View view) {
    if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(intent, 100);
    } else {
        Toast.makeText(getApplication(), "Camera not Supported!", Toast.LENGTH_LONG).show();
    }
}


protected void onActivityResult(int RC, int RQC, Intent I) {

    super.onActivityResult(RC, RQC, I);

    if (RC == 1 && RQC == RESULT_OK && I != null && I.getData() != null) {

        Uri uri = I.getData();

        try {

            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);

            ImageView imageview = null;
            imageview.setImageBitmap(bitmap);

        } catch (IOException e) {

            e.printStackTrace();
        }
   /* super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQUEST_CAMERA) {
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        assert thumbnail != null;
        thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
        File destination = new File(Environment.getExternalStorageDirectory(),"temp.jpg");
        FileOutputStream fo;
        try {
            fo = new FileOutputStream(destination);
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        new uploadFileToServerTask().execute(destination.getAbsolutePath());*/
        /*Uri selectedImage = data.getData();
        Bitmap photo = (Bitmap) data.getExtras().get("data");

        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        assert cursor != null;
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        photo = (Bitmap) data.getExtras().get("data");
        ImageView imageView = (ImageView) findViewById(R.id.Image);
        imageView.setImageBitmap(photo);*/
    }
}




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

}

I am getting error at "setImageBitmap". What i want to do is to show image in imageview after capturing. It captures a picture and the picture goes to gallery direct and the imageview remains clear. Help me in identifying the mistake. Thanks

akhilesh0707
  • 6,709
  • 5
  • 44
  • 51

2 Answers2

0

Please write this code inside your OnCreate.

Imageview imageview = (Imageview) findViewById(R.id.imageview);
imageview.setImageBitmap(bitmap);
Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51
0

You are setting bitmap on null ImageView Object first your need to initialize your ImageView inside the onCreate()

ImageView imageview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageview=(ImageView) findViewById(R.id.imageview);
}

and Remove below line from onActivityResult

 ImageView imageview = null;
akhilesh0707
  • 6,709
  • 5
  • 44
  • 51