0

I have tried so many different ways of making a picture show up in my imageView but it just refuses to do so. I have no idea what is going on. My app needs to do more than just take a picture and show it. Basically all of this code is straight from the Android website.

private File createImageFile() throws IOException 
{
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

private void dispatchTakePictureIntent() 
{
    // Create the File where the photo should go
    File photoFile = null;
    try {
        photoFile = createImageFile();
   //   Log.d("myTag", "MADE IMAGE FILE");
   } catch (IOException ex) {
   //   Log.d("myTag", "ERROR CREATING IMAGE FILE");
   }
   // Continue only if the File was successfully created
    if (photoFile != null) {
        Uri photoURI = FileProvider.getUriForFile(this,"com.company.app.fileprovider.READ",photoFile);
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
     // Log.d("myTag", "PUTEXTRA");
        startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
     // Log.d("myTag", "ABOUTTOSTARTACTIVITY");
    }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == REQUEST_TAKE_PHOTO) {
        setPic();
    }
}

The only thing I can note in setPic() is that mImageView.getWidth() and getHeight() return 0 and error. I've tried showing the image without scaling but still the same results (or lack thereof). I've also tried getMeasuredWidth() and it doesn't error but still..no image.

private void setPic() {
// Get the dimensions of the View
int targetW = mImageView.getWidth(); //getMeasuredWidth()
int targetH = mImageView.getHeight(); //getMeasuredHeight()

// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;

// Determine how much to scale down the image
// int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

//Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
//bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;

Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
mImageView.setImageBitmap(bitmap);

}

My mCurrentPhotoPath is no longer null. Image STILL doesn't show up. All pictures I've taken are in my phone's gallery though, so it is saving. The xml for the imageView in question is here:

ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/main_picture"
    android:paddingLeft="250dp"
    android:paddingBottom="250dp"
    android:adjustViewBounds="true"
    android:layout_alignParentBottom="true"
    android:layout_toLeftOf="@+id/home_button"
    android:layout_marginRight="41dp"
    android:layout_marginEnd="41dp" />

I have the following line in my onCreate:

mImageView = (ImageView) findViewById(R.id.main_picture);
rafvasq
  • 1,428
  • 3
  • 16
  • 38

2 Answers2

1

If you need to take a photo with the camera and display it in image view (which is what i understand) i think that this can help you

Capture Image from Camera and Display in Activity

EDIT: Put the all code that work for me

Call the intent

 private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File

        }
        // Continue only if the File was successfully created
        if (photoFile != null) {

            Uri photoURI = Uri.fromFile(photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

Method that create the file

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp;
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

Method that save and restore the state of the activity

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {

    //savedInstanceState.putString("filePath", mCurrentPhotoPath);
    savedInstanceState.putString("filePath", mCurrentPhotoPath);
    BitmapDrawable drawable = (BitmapDrawable) img.getDrawable();
    if(drawable!= null) {
        Bitmap bitmap = drawable.getBitmap();
        savedInstanceState.putParcelable("image", bitmap);
    }

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    if(savedInstanceState != null) {
        mCurrentPhotoPath= savedInstanceState.getString("filePath");
        Bitmap bitmap = savedInstanceState.getParcelable("image");
        img.setImageBitmap(bitmap);
    }
}

The activity result (your setPic() method)

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

        try {
            Bitmap mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
            img.setImageBitmap(mImageBitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

hope this can help you.

Community
  • 1
  • 1
MrCroco
  • 46
  • 5
  • A the very basic level -- I'd like that to happen since I have nothing yet. But my app will need to save the picture in order to be able to display it in the ImageView the next time the app opens. So I'm trying to display the image after having saved it too (I'm assuming this is how to go about that..) – rafvasq Jul 23 '16 at 16:59
  • I updated my question with the things I've added. mCurrentPhotoPath is no longer null in setPic()... Still no image :( – rafvasq Jul 23 '16 at 17:57
  • Could it have to do with the fact that my app is in landscape, camera opens up portrait and it has to go back to landscape after? – rafvasq Jul 23 '16 at 19:19
  • Yeah probably you are right. i edited my answer this should fix it – MrCroco Jul 23 '16 at 20:16
  • Made the changes you recommended. I still have yet to see the image display even once at all. :( Added my xml code to my original post hoping it helps. – rafvasq Jul 23 '16 at 20:30
  • I edited my answer with the code that i tried and it work for me. i think that the problem is in your onActivityResult() method . i put the all the code just to make sure – MrCroco Jul 24 '16 at 07:21
0

try this it is working for me

final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 0;
            File imageFile = new File("/sdcard/Cyclops/cyclops.jpg");
            bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
           imageView.setImageBitmap(bitmap);
Shahzain ali
  • 1,552
  • 1
  • 19
  • 32