1
package com.my.app ;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Comparator;

import com.my.app .R;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

public class ImageInfo extends Activity {

    private static final int CAMERA_PIC_REQUEST = 1111;
    private ImageView mImage;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mImage = (ImageView) findViewById(R.id.camera_image);
        //1
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, CAMERA_PIC_REQUEST);
    }



    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode != RESULT_CANCELED)   {
            if(requestCode == CAMERA_PIC_REQUEST){

                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                //  mImage.setImageBitmap(thumbnail);
                //3
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                //4
                File file = new File("data/data/com.my.app /photo.jpg");
                File myDir=new File("data/data/com.my.app /");
                try {
                    String encodedPhotoImage;

                    file.createNewFile();
                    FileOutputStream fo = new FileOutputStream(file);
                    //5
                    fo.write(bytes.toByteArray());
                    fo.close();
                    deleteLatest() ;


                    byte[] photoImgBytes=readPhotoFile();
                    ByteArrayOutputStream bao1 = new ByteArrayOutputStream();

                    Bitmap bitmapPhoto = BitmapFactory.decodeByteArray(photoImgBytes, 0, photoImgBytes.length);
                    bitmapPhoto.compress(Bitmap.CompressFormat.JPEG, 100,bao1);
                    byte[] by = bao1.toByteArray();

                    String by1 = Base64.encodeToString(by, 0);
                    encodedPhotoImage =  URLEncoder.encode(by1);
                    file.delete();

                    if (!myDir.exists()) {
                        myDir.mkdirs();
                    }
                    File encryptedFile=new File("data/data/com.my.app /photo.txt");
                    encryptedFile.createNewFile();
                    FileWriter writer = new FileWriter(encryptedFile);
                    writer.append(encodedPhotoImage);
                    writer.close();
                    //Base64.encodeToString(ba1, Base64.DEFAULT);
                    Intent intent=new Intent(ImageInfo.this,Info.class);
                    startActivity(intent);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }
    }

    private byte[] readPhotoFile(){         
        String url="data/data/com.my.app /photo.jpg";
        Log.e("","Image is"+url);
        Bitmap bm = BitmapFactory.decodeFile(url);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray(); 
        return b;
    }

    @Override
    public void onBackPressed() {
        // do nothing.
    }


    private void deleteLatest() {
        File f = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera" );
        File [] files = f.listFiles();
        Arrays.sort( files, new Comparator<Object>()
                {
            public int compare(Object o1, Object o2) {
                if (((File)o1).lastModified() > ((File)o2).lastModified()) {
                    return -1;
                } else if (((File)o1).lastModified() < ((File)o2).lastModified()) {
                    return 1;
                } else {
                    return 0;
                }
            }

                });
        files[0].delete();
    }
}

This is my java file. I have already disabled the back button using

public void onBackPressed() {
        // do nothing.
    }

But when back button is pressed in the app, the entire screen becomes black. Please find screen shot enter image description here. The app hangs and the only way it can be accessed by terminating it through task manager and then restarting it.

onkar
  • 109
  • 1
  • 2
  • 11

3 Answers3

3

try this code for disable back button

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if(keyCode==KeyEvent.KEYCODE_BACK)
        {
            return true;
        }
        else
        {
            return super.onKeyDown(keyCode, event);
        }
    }

Capture Image from Camera and Display in Activity

Community
  • 1
  • 1
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
1

I can't see a problem with your back button disabling. But there is another problem that may cause a part of this issue you are doing File IO in the UI Thread of your App.

Android has one UI Thread that does all the painting of your UI. If you block this thread through writing large files to the disk or reading bitmaps from the disk the phone UI will freeze and not react on user input until the heavy work is done.

I recommend to read this article on responsiveness to learn more about not blocking the UI Thread.

Janusz
  • 187,060
  • 113
  • 301
  • 369
1

You can intercept the BackButton inside your own App, ONLY! As I see, you are starting some kind of CameraApp, that should pick a picture for you. You have NO control over the CameraActivity's BackButton. Your screen Has an ImageView, but you never set a Bitmap to it. The disabling of the BackButton works, as you could NOT go out of your screen and have to "kill" it via a TaskManager. The BackButton has no functionality inside your Activity.

Rafael T
  • 15,401
  • 15
  • 83
  • 144