2013-01-21 39 views
0
public class BeginTakePhotoScene extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_begin_take_photo_scene); 
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

    takephotobtn = new ImageButton(this); 
    takephotobtn.setId(888); 
    takephotobtn.setBackgroundResource(R.drawable.take_photo_btn); 
    takephotobtnparam = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 
    takephotobtnparam.addRule(RelativeLayout.CENTER_HORIZONTAL); 
    takephotobtnparam.topMargin = margin.heightPixels * 9/10 - height; 
    takephotobtn.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      mUri = Uri.fromFile(new File(Environment 
        .getExternalStorageDirectory(), String.valueOf(System 
        .currentTimeMillis()) + ".jpg")); 
      camera.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri); 
      camera.putExtra("return-data", false); 
      startActivityForResult(camera, PICTURE_RESULT); 
     } 
    }); 

    choosephotobtn = new ImageButton(this); 
    choosephotobtn.setId(889); 
    choosephotobtn.setBackgroundResource(R.drawable.choose_photo_btn); 
    choosephotobtnparam = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 
    choosephotobtnparam.addRule(RelativeLayout.CENTER_HORIZONTAL); 
    choosephotobtnparam.addRule(RelativeLayout.BELOW, 888); 
    choosephotobtn.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      startActivityForResult(
        Intent.createChooser(intent, "Select Picture"), 
        SELECT_PICTURE); 
     } 
    }); 
} 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (resultCode == RESULT_OK) { 
     if (requestCode == SELECT_PICTURE) { 
      selectedImageUri = data.getData(); 
      uncroppedimage = getFilePath(selectedImageUri); 

      Intent cropIntent = new Intent("com.android.camera.action.CROP"); 

      cropIntent.setDataAndType(selectedImageUri, "image/*"); 

      cropIntent.putExtra("crop", "true"); 

      cropIntent.putExtra("aspectX", 1); 
      cropIntent.putExtra("aspectY", 1); 

      cropIntent.putExtra("outputX", 128); 
      cropIntent.putExtra("outputY", 128); 

      File tempFile; 
      tempFile = new File(Environment.getExternalStorageDirectory(), 
        String.valueOf(System.currentTimeMillis()) + ".jpg"); 
      mSavedUri = Uri.fromFile(tempFile); 
      cropIntent.putExtra("output", mSavedUri); 

      cropIntent.putExtra("return-data", true); 

      startActivityForResult(cropIntent, PIC_CROP); 

     } else if (requestCode == PICTURE_RESULT) { 
      Log.e("mUri", mUri.getPath()); 
      uncroppedimage = mUri.getPath(); 

      Intent cropIntent = new Intent("com.android.camera.action.CROP"); 

      cropIntent.setDataAndType(mUri, "image/*"); 

      cropIntent.putExtra("crop", "true"); 

      cropIntent.putExtra("aspectX", 1); 
      cropIntent.putExtra("aspectY", 1); 

      cropIntent.putExtra("outputX", 128); 
      cropIntent.putExtra("outputY", 128); 

      File tempFile; 
      tempFile = new File(Environment.getExternalStorageDirectory(), 
        String.valueOf(System.currentTimeMillis()) + ".jpg"); 
      mSavedUri = Uri.fromFile(tempFile); 
      cropIntent.putExtra("output", mSavedUri); 

      cropIntent.putExtra("return-data", true); 

      startActivityForResult(cropIntent, PIC_CROP); 
     } else { 
      Log.e("mSavedUri", mSavedUri.getPath()); 
      profileimage.setImageURI(mSavedUri); 
      croppedimage = mSavedUri.getPath(); 

      profileimage.setVisibility(View.VISIBLE); 
      confirmbtn.setVisibility(View.VISIBLE); 

      takephotobtnparam.topMargin = margin.heightPixels * 9/10 
        - height * 2; 
     } 
    } 
} 

private String getFilePath(Uri uri) { 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = this.managedQuery(uri, projection, null, null, null); 
    if (cursor != null) { 
     int colum_index = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(colum_index); 
    } else 
     return null; 
} 
} 

我登錄了uri.getpath,它返回file:///mnt/sdcard/1358731920220.jpg。此鏈接似乎有額外的/。但是,我不確定爲什麼會這樣?java.io.FileNotFoundException:/mnt/sdcard/1358731920220.jpg(沒有這樣的文件或目錄)

+1

它是一個文件:// uri。路徑是/ mnt/sdcard ...當你把它們放在一起時,你會得到file:/// mnt/sdcard –

+0

@GregS如何解決它? –

+1

請根據標題詢問更具體的問題。 – herbertD

回答

0

添加此權限清單文件,如果你還沒有加入,

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

已經添加了,如果不是不會得到路徑 –

+0

看看[這個鏈接](http://stackoverflow.com/questions/8845534/getting-file-not-found-exception)。 – abhi

2

cropIntent.putExtra("return-data", true);

變化

cropIntent.putExtra("return-data", false);

將裁剪後的圖片保存到SD卡。

相關問題