2013-12-08 6 views
0

내 앱이 세로 모드로만 표시되도록 설정되었습니다. 문제는 내가 카메라와 갤러리의 의도를 사용해야한다는 것인데, 그 앱을 같은 방향으로 지정할 수 없기 때문에 내 이미지 데이터를 null로 만드는 방향 변경 사이에서 어떤 펑키 한 것들이 발생한다는 것입니다.오리엔테이션 변경 후 인 텐트의 데이터 처리

이 코드는 휴대 전화가 옆으로 기울어지지 않을 때 (세로 모드에서) 방향 변경 후 데이터를 처리하기 위해 어떻게 향상시킬 수 있습니까?

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); 
    } 
} 
] 
+0

'sharedPreference'에서 데이터를 저장할 수 있습니다. http://developer.android.com/guide/topics/data/data-storage.html#pref – Naddy

답변

관련 문제