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,512
  • 3
  • 18
  • 48

1 Answers1

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,679
  • 1
  • 20
  • 33