Mohhamad Hasham
Mohhamad Hasham

Reputation: 2032

Unable to get bitmap image to image view

The image is saved in the desired folder but it is not being displayed in imageView nor the Toast is being displayed.I have tried with with almost every method like using different XML files and changing class as well but the problem still resides.

package com.example.mohammadhasham.jtbopencameramodule;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.icu.text.SimpleDateFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.util.Date;

public class CameraActivity extends AppCompatActivity {

    ImageView img;
    String currentPath;
    File f;

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

        img = (ImageView) (findViewById(R.id.capture));


    }

    public void captureImage(View v) {

        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "image.jpg");
        Uri u = Uri.fromFile(f);
        i.putExtra(MediaStore.EXTRA_OUTPUT, u);
        i.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        startActivityForResult(i, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == 0) {
            switch (resultCode) {
                case Activity.RESULT_OK:
                {
                    if (f.exists())
                    {
                        Toast.makeText(this, "Image Saved To Gallery", Toast.LENGTH_LONG).show();
                         Bitmap b = BitmapFactory.decodeFile(f.getAbsolutePath());
                          img.setImageBitmap(b);


                    }
                    else
                    {
                        Toast.makeText(this, "Image Not Saved To Gallery", Toast.LENGTH_LONG).show();

                    }
                    break;
                }
                case Activity.RESULT_CANCELED:
                {
                    break;
                }
                default:
                    break;
            }
        }
    }

}

Upvotes: 0

Views: 364

Answers (2)

L.Velazquez
L.Velazquez

Reputation: 1736

Try this:

public class CameraActivity extends AppCompatActivity {

private final String APP_TAG = "MyCustomApp";
private static final int REQUEST_IMAGE_CAPTURE = 1;
private String photoFileName = "photo.jpg";

private ImageView img;

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


    img = (ImageView) (findViewById(R.id.imageView));
}

public void captureImage(View v) {

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(photoFileName));
        takePictureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Toast.makeText(this, "Image Saved To Gallery", Toast.LENGTH_LONG).show();

        Uri takenPhotoUri = getPhotoFileUri(photoFileName);
        Bitmap imageBitmap = BitmapFactory.decodeFile(takenPhotoUri.getPath());
        img.setImageBitmap(imageBitmap);
    } else {
        Toast.makeText(this, "Image Not Saved To Gallery", Toast.LENGTH_LONG).show();

    }
}

// Returns the Uri for a photo stored on disk given the fileName
public Uri getPhotoFileUri(String fileName) {
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()) {
        Log.d(APP_TAG, "failed to create directory");
    }

    // Return the file target for the photo based on filename
    return Uri.fromFile(new File(mediaStorageDir.getPath() + File.separator + fileName));
}

}

Upvotes: 0

Shadesblade
Shadesblade

Reputation: 782

I believe you have a mistake in your code:

You say

if (resultCode == 0) {
            switch (resultCode) {
                case Activity.RESULT_OK:
                {

You're restricting resultCode to the value 0, and then trying to execute a switch based on it's value.

Upvotes: 1

Related Questions