Click here to Skip to main content
15,846,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been struggling with this problem for a few days now and cannot seem to get past it. Basically I have an activity that has 3 image views with a button that starts the camera intent

The problem is that while I am developing and running through the debugger, everything works perfectly but not when I run the app alone (no debugger, just on the phone) after accepting the picture preview it just shows a black screen and does nothing after that.

All that is needed is for the picture to display in the image views and save to the gallery, it does all of this while in debug mode but not when I need to run the app independently.

I am completely at a loss with this problem - please can someone / anyone assist me.

I think this may be an async issue but I am very new to android and dont know how to implement this, in my head and through the tutorials I have read / watched - this should be an easy process, however I cannot seem to get it to work

Here is my code on the layout for the activity:

XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PhotoActivity">

    <ImageView
        android:id="@+id/imgPhoto1"
        android:layout_width="236dp"
        android:layout_height="136dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/dds_logo" />

    <ImageView
        android:id="@+id/imgPhoto2"
        android:layout_width="236dp"
        android:layout_height="136dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imgPhoto1"
        app:srcCompat="@drawable/dds_logo" />

    <ImageView
        android:id="@+id/imgPhoto3"
        android:layout_width="236dp"
        android:layout_height="136dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imgPhoto2"
        app:srcCompat="@drawable/dds_logo" />

    <Button
        android:id="@+id/btnTakePhoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="52dp"
        android:text="Take Photo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imgPhoto3"
        app:layout_constraintVertical_bias="1.0" />

</android.support.constraint.ConstraintLayout>


Here is the code for the onClick event:
Java
public void onClick(View v) {
        try {
            //respond to clicks
            if (v.getId() == R.id.btnTakePhoto) {
                captureImage();
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }


Here is the call to the intent:

Java
private void captureImage() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(takePictureIntent, CAPTURE_IMAGE_REQUEST);

    }


Here is the onActivtyResult:

Java
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        try{

            if (!Debug.isDebuggerConnected()){
                Debug.waitForDebugger();
                Log.d("debug", "started"); // Insert a breakpoint at this line!!
            }
            if(numPhotos < 2) {
                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");

                String savedImageURL = MediaStore.Images.Media.insertImage(
                        getContentResolver(),
                        imageBitmap,
                        "Serial",
                        "Refresh Verification photo"
                );

                switch (numPhotos) {
                    case 0:
                        imgPhoto1.setImageBitmap(imageBitmap);
                        break;
                    case 1:
                        imgPhoto2.setImageBitmap(imageBitmap);
                        break;
                    case 2:
                        imgPhoto3.setImageBitmap(imageBitmap);
                        break;
                }
                numPhotos++;
            }
            else
            {
                numPhotos = 0;
                Toast.makeText(PhotoActivity.this, "Max # of ohotos reached", Toast.LENGTH_LONG).show();
                Intent i = new Intent(getApplicationContext(), MenuActivity.class);
                startActivity(i);
                finish();
            }
        }

        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }


What I have tried:

I have tried only saving the image and not displaying it in the imageview but still no luck. It still only works when connected to the IDE in debug mode and not when deployed to the device.
Posted
Updated 7-Apr-19 22:34pm
Comments
David Crow 4-Apr-19 9:03am    
So since you can't set breakpoints and step through the code, how about adding a ListView control to the layout and send debug text to it. That way you can know the values of the variables along the way and where the code execution is (not) going?
SheldonShane 5-Apr-19 3:41am    
Thanks for the suggestion David, however even the listview does not update - its almost as if the application is getting stuck and not processing further. I have tried creating a new thread in the OnActivityResult - now I get taken back to the original screen but the listview and imageviews still do not update
Pratishkumar Kushwaha 5-Apr-19 7:55am    
You can send the Application Link I can check it.
SheldonShane 5-Apr-19 8:58am    
Hi Pratishkumar, thank you for the offer of assistance - I am very much open to sharing the application with you, how can I reach you?
Pratishkumar Kushwaha 5-Apr-19 10:55am    
Thank you for the requesting my email id is pratishbloge@gmail.com
you can send the application by this email id,i can help you.

You can check the cat log in android mobile and check exception.
Then the Exception is Memory out of exception than your imgPhoto1 is very high resolution.
Then You can compress the file and again use is this problem can you solve it.
 
Share this answer
 
Comments
SheldonShane 5-Apr-19 8:42am    
Thank you Pratishkumar but I am afraid I am not following you on the solution, I am open to sharing the application with you, is there a way I can securely do so?
[no name] 10-Apr-19 11:47am    
1
Good Day All,

I have managed to fix the application thanks to the comment by Pratishkumar Kushwaha, after compressing the image, everything works fine.

OnActivityResult:

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

        try {
            Bundle extras = data.getExtras();
            final Bitmap imageBitmap = (Bitmap) extras.get("data");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            imageBitmap.compress(Bitmap.CompressFormat.JPEG, 60, bytes);
            byte[] byteArray = bytes.toByteArray();
            final Bitmap compressedBitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

            if (numPhotos < 3) {

                switch (numPhotos) {
                    case 0:
                        imgPhoto1.setImageBitmap(compressedBitmap);
                        savebitmap(compressedBitmap);
                        numPhotos++;
                        break;
                    case 1:
                        imgPhoto2.setImageBitmap(compressedBitmap);
                        savebitmap(compressedBitmap);
                        numPhotos++;
                        break;
                    case 2:
                        imgPhoto3.setImageBitmap(compressedBitmap);
                        savebitmap(compressedBitmap);
                        numPhotos = 0;
                        Toast.makeText(PhotoActivity.this, "Max # of ohotos reached", Toast.LENGTH_LONG).show();
                        Intent i = new Intent(getApplicationContext(), MenuActivity.class);
                        startActivity(i);
                        finish();
                        break;

                }
            } else {
                numPhotos = 0;
                Toast.makeText(PhotoActivity.this, "Max # of ohotos reached", Toast.LENGTH_LONG).show();
                Intent i = new Intent(getApplicationContext(), MenuActivity.class);
                startActivity(i);
                finish();
            }
        }
        catch(Exception e){
            e.printStackTrace();;
        }
    }


savebitmap:

Java
public static File savebitmap(Bitmap bmp) throws IOException {

       String date = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()).format(new Date());

       File folder = new File(Environment.getExternalStorageDirectory()
               + "/RefreshPhotos");

       boolean var = false;
       if (!folder.exists())
           var = folder.mkdir();

       ByteArrayOutputStream bytes = new ByteArrayOutputStream();
       bmp.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
       File f = new File(folder
               + File.separator + "RefreshPhoto_" + date + ".png");
       f.createNewFile();
       FileOutputStream fo = new FileOutputStream(f);
       fo.write(bytes.toByteArray());
       fo.close();
       return f;
   }
 
Share this answer
 
Comments
Pratishkumar Kushwaha 10-Apr-19 1:27am    
Nice Code Bro
[no name] 10-Apr-19 11:47am    
1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900