80,362
社区成员
发帖
与我相关
我的任务
分享
public class ShareActivity extends Activity implements Serializable {
public static final int SELECT_PIC_BY_TACK_PHOTO = 1;// Camera
public static final int SELECT_PIC_BY_PICK_PHOTO = 2;// Gallery
// path-KEY
public static final String SAVED_IMAGE_DIR_PATH = "photo_path";
private static final String TAG = "ShareActivity";
// path
private String picPath;
private Intent lastIntent;
private Uri photoUri;
private RelativeLayout layout1;
private RelativeLayout layout2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_share);
layout1 = (RelativeLayout) this.findViewById(R.id.upload_photo);
layout2 = (RelativeLayout) this.findViewById(R.id.upload_gallery);
lastIntent = getIntent();
layout1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
takePhoto();
}
});
layout2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
pickPhoto();
}
});
}
private void takePhoto() {
String SDState = Environment.getExternalStorageState();
if (SDState.equals(Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();
photoUri = this.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);
} else {
Toast.makeText(this, R.string.takePhoto_msg, Toast.LENGTH_LONG)
.show();
}
}
private void pickPhoto() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
doPhoto(requestCode, data);
}
super.onActivityResult(requestCode, resultCode, data);
}
private void doPhoto(int requestCode, Intent data) {
if (requestCode == SELECT_PIC_BY_PICK_PHOTO) {
if (data == null) {
Toast.makeText(this, "选择图片文件出错", Toast.LENGTH_LONG).show();
return;
}
photoUri = data.getData();
if (photoUri == null) {
Toast.makeText(this, "选择图片文件出错", Toast.LENGTH_LONG).show();
return;
}
}
String[] pojo = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(photoUri, pojo, null, null, null);
if (cursor != null) {
int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
cursor.moveToFirst();
picPath = cursor.getString(columnIndex);
cursor.close();
}
Log.i(TAG, "imagePath = " + picPath);
if (picPath != null) {
Intent startEx = new Intent(ShareActivity.this, PhotoPre.class);
Bundle bundle = new Bundle();
bundle.putString(SAVED_IMAGE_DIR_PATH, picPath);
startEx.putExtras(bundle);
setResult(Activity.RESULT_FIRST_USER, startEx);
startActivity(startEx);
} else {
Toast.makeText(this, "选择文件不正确!", Toast.LENGTH_LONG).show();
}
}
}
public class PhotoPre extends Activity {
public static String picPath = null;
private static final String TAG = "PhotoPreActivity";
public static final String KEY_PHOTO_PATH = "photo_path";
public static final String IMAGE_UNSPECIFIED = "image/*";
private ProgressDialog progressDialog;
private Button back;
private Button upload;
private Button cancel;
private ImageView view;
private Bitmap bm;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imageshow);
back = (Button) this.findViewById(R.id.phtot_back);
upload = (Button) this.findViewById(R.id.phtot_upload);
cancel = (Button) this.findViewById(R.id.phtot_cancel);
view = (ImageView) this.findViewById(R.id.photo_selected);
progressDialog = new ProgressDialog(this);
back.setOnClickListener(new ThisOnClickListener(0));
upload.setOnClickListener(new ThisOnClickListener(1));
cancel.setOnClickListener(new ThisOnClickListener(2));
Bundle bundle = getIntent().getExtras();
String path = bundle.getString(KEY_PHOTO_PATH);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bm = BitmapFactory.decodeFile(path);
view.setImageBitmap(bm);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (requestCode == 1) {
view.setImageBitmap(null);
picPath = data.getStringExtra(ShareActivity.SAVED_IMAGE_DIR_PATH);
Log.i(TAG, "最终选择的图片=" + picPath);
Bitmap bm = BitmapFactory.decodeFile(picPath);
view.setImageBitmap(bm);
} else if (requestCode == 2) {
try {
Bitmap bm = null;
ContentResolver resolver = getContentResolver();
Uri uri = data.getData();
bm = MediaStore.Images.Media.getBitmap(resolver, uri);
String[] pro = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, pro, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String picPath1 = cursor.getString(column_index);
Bitmap bitmap = BitmapFactory.decodeFile(picPath1);
view.setImageBitmap(bitmap);
cursor.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, e.toString());
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
public class ThisOnClickListener implements OnClickListener {
private int index = 0;
private ThisOnClickListener(int i) {
index = i;
}
@Override
public void onClick(View v) {
switch (index) {
case 0:
Intent intent = new Intent(PhotoPre.this, MainActivity.class);
startActivity(intent);
break;
case 1:
try {
postFile();
} catch (Exception e) {
e.printStackTrace();
}
break;
case 2:
Intent intent1 = new Intent(PhotoPre.this, ShareActivity.class);
startActivity(intent1);
break;
}
}
}
private void postFile() throws Exception {
progressDialog.setMessage("正在上传文件...");
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(
CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost("http://localhost:9002/upload.php");
File file = new File("c:/TRASH/zaba_1.jpg");
FileEntity reqEntity = new FileEntity(file, "binary/octet-stream");
httppost.setEntity(reqEntity);
reqEntity.setContentType("binary/octet-stream");
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
}
}