0

My problem:

My app works fine to take pictures and receive them from the phones gallery when the phone is in portrait mode. By default my app is set to only be portrait and not allow orientation changes. I think theproblem arises because the phones default apps (camera and gallery) support portrait mode and somehow that is messing things up. I've tried allot of stuff but I think I may be overlooking something please take a look at my code.

PostPhotosActivity.java

public class PostPhotosActivity extends Activity {

public static final String TAG = "PostPhotosActivity";


String title, price, description, maincat, subcat, pname, pemail, pphone, pmeet, imageUri;

public static final String TEMP_PHOTO_FILE_NAME = "temp_photo.jpg";

public static final int REQUEST_CODE_GALLERY      = 0x1;
public static final int REQUEST_CODE_TAKE_PICTURE = 0x2;
public static final int REQUEST_CODE_CROP_IMAGE   = 0x3;
private ImageView mImageView;
private File      mFileTemp;
ParseFile file;
double latitude, longitude;
Button button;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    //To change body of overridden methods use File | Settings | File Templates.
    setContentView(R.layout.activity_post_photos);



     Bundle extras= getIntent().getExtras();
     if(extras!=null)
     {
        title = extras.getString("TITLE"); // get the value based on the key
        price = extras.getString("PRICE"); // get the value based on the key 
        description = extras.getString("DESCRIPTION"); // get the value based on the key
        maincat = extras.getString("MAINCAT"); // get the value based on the key 
        subcat = extras.getString("SUBCAT"); // get the value based on the key
        pname = extras.getString("PNAME"); // get the value based on the key 
        pemail = extras.getString("PEMAIL"); // get the value based on the key
        pphone = extras.getString("PPHONE"); // get the value based on the key 
        pmeet = extras.getString("PMEET"); // get the value based on the key
    }  



button = (Button) findViewById(R.id.post_data);

button.setVisibility(View.INVISIBLE);

button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Perform action on click

        GpsLocationTracker mGpsLocationTracker = new GpsLocationTracker(PostPhotosActivity.this);

         /**
              * Set GPS Location fetched address
              */
             if (mGpsLocationTracker.canGetLocation()) 
             {
                 latitude = mGpsLocationTracker.getLatitude();
                 longitude = mGpsLocationTracker.getLongitude();
                 Log.i(TAG, String.format("latitude: %s", latitude));
                 Log.i(TAG, String.format("longitude: %s", longitude));

             } 
             else 
             {
                 mGpsLocationTracker.showSettingsAlert();
             }

        ParseGeoPoint point = new ParseGeoPoint(latitude, longitude);
        ParseObject setPost = new ParseObject("testData");

        // Create an author relationship with the current user
        setPost.put("author", ParseUser.getCurrentUser());

        // Get location

        setPost.put("location", point);
        setPost.put("Title", title);
        setPost.put("Price", price);
        setPost.put("Description", description);
        setPost.put("MainCat", maincat);
        setPost.put("SubCat", subcat);
        setPost.put("PName", pname);
        setPost.put("PEmail", pemail);
        setPost.put("PPhone", pphone);
        setPost.put("PMeet", pmeet);
        setPost.put("Photo", file);

        setPost.saveInBackground();

        Intent intent = new Intent(PostPhotosActivity.this, Flow.class);
       startActivity(intent);
    }
});


    final String[] items = new String[] { "Take from camera",
            "Select from gallery" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.select_dialog_item, items);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Select Image");
    builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) { // pick from
                                                                // camera
            if (item == 0) {

                takePicture();

            } else { // pick from file

                openGallery();

            }
        }
    });

    final AlertDialog dialog = builder.create();

    mImageView = (ImageView) findViewById(R.id.iv_photo);
    mImageView.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            dialog.show();
        }
    });



        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
                mFileTemp = new File(Environment.getExternalStorageDirectory(), TEMP_PHOTO_FILE_NAME);
        }
        else {
                mFileTemp = new File(getFilesDir(), TEMP_PHOTO_FILE_NAME);
        }

}

private void takePicture() {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    try {
            Uri mImageCaptureUri = null;
            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state)) {
                    mImageCaptureUri = Uri.fromFile(mFileTemp);
            }
            else {
                    /*
                     * The solution is taken from here: http://stackoverflow.com/questions/10042695/how-to-get-camera-result-as-a-uri-in-data-folder
                     */
                    mImageCaptureUri = InternalStorageContentProvider.CONTENT_URI;
            }        
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
    } catch (ActivityNotFoundException e) {

        Log.d(TAG, "cannot take picture", e);
    }
}

private void openGallery() {

    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, REQUEST_CODE_GALLERY);
}

private void startCropImage() {

    Intent intent = new Intent(this, CropImage.class);
    intent.putExtra(CropImage.IMAGE_PATH, mFileTemp.getPath());
    intent.putExtra(CropImage.SCALE, true);

    intent.putExtra(CropImage.ASPECT_X, 1);
    intent.putExtra(CropImage.ASPECT_Y, 1);

    startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);
}

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

    if (resultCode != RESULT_OK) {

        return;
    }

    Bitmap bitmap;

    switch (requestCode) {

        case REQUEST_CODE_GALLERY:

            try {

                InputStream inputStream = getContentResolver().openInputStream(data.getData());
                FileOutputStream fileOutputStream = new FileOutputStream(mFileTemp);
                copyStream(inputStream, fileOutputStream);
                fileOutputStream.close();
                inputStream.close();

                startCropImage();

            } catch (Exception e) {

                Log.e(TAG, "Error while creating temp file", e);
            }

            break;
        case REQUEST_CODE_TAKE_PICTURE:

            startCropImage();
            break;
        case REQUEST_CODE_CROP_IMAGE:

            String path = data.getStringExtra(CropImage.IMAGE_PATH);
            if (path == null) {

                return;
            }

            //byte[] idata = path.getBytes();
            Bitmap picture = BitmapFactory.decodeFile(path);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            picture.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            // get byte array here
            byte[] idata= stream.toByteArray();

            file = new ParseFile("photo.jpg", idata);
            file.saveInBackground();

            bitmap = BitmapFactory.decodeFile(mFileTemp.getPath());

            mImageView.setImageBitmap(bitmap);
            button.setVisibility(View.VISIBLE);
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}


public static void copyStream(InputStream input, OutputStream output)
        throws IOException {

    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = input.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    }
}

}

Log cat doesn't show any errors but it does show this...

12-08 13:57:26.715: D/dalvikvm(7138): GC_FOR_ALLOC freed 7148K, 18% free 34349K/41528K, paused 50ms, total 50ms
12-08 13:57:26.864: D/dalvikvm(7138): GC_FOR_ALLOC freed 366K, 16% free 35157K/41528K, paused 27ms, total 27ms
12-08 13:57:26.879: I/dalvikvm-heap(7138): Grow heap (frag case) to 38.939MB for 4804880-byte allocation
12-08 13:57:26.918: D/dalvikvm(7138): GC_FOR_ALLOC freed 0K, 14% free 39849K/46224K, paused 39ms, total 39ms
12-08 13:57:41.504: W/IInputConnectionWrapper(7138): showStatusIcon on inactive InputConnection
12-08 13:57:47.614: I/Choreographer(7138): Skipped 37 frames!  The application may be doing too much work on its main thread.
12-08 13:57:48.075: D/dalvikvm(7138): GC_FOR_ALLOC freed 1596K, 15% free 39360K/46224K, paused 31ms, total 31ms
12-08 13:57:48.083: I/dalvikvm-heap(7138): Grow heap (frag case) to 43.266MB for 5038864-byte allocation
12-08 13:57:48.122: D/dalvikvm(7138): GC_FOR_ALLOC freed 1436K, 17% free 42844K/51148K, paused 34ms, total 34ms
12-08 13:57:48.512: I/Choreographer(7138): Skipped 50 frames!  The application may be doing too much work on its main thread.
12-08 13:57:49.520: I/Choreographer(7138): Skipped 52 frames!  The application may be doing too much work on its main thread.
12-08 13:57:50.481: I/Choreographer(7138): Skipped 47 frames!  The application may be doing too much work on its main thread.
Derek
  • 632
  • 2
  • 11
  • 29
  • please post a stack trace for the crash – Emmanuel Dec 08 '13 at 22:20
  • As I stated there is no "Crash" so there isn't any log-cat output. What the crash looks like is my app flashes to my crop photo activity then flashes to the android homescreen and then returns to the my apps main activity. – Derek Dec 08 '13 at 22:23
  • I thought there was some sort of log-cat output since you said crash on the title of the question. – Emmanuel Dec 08 '13 at 22:24
  • Good call I'l edit that. – Derek Dec 08 '13 at 22:27

0 Answers0