您好我使用Eclipse和Android新手。 是否有一种简单的方法来选择图像并将其上传到parse.com服务器。 另外,有一种简单的方法可以从服务器检索图像并将此图像设置为布局上的ImageView。
答案 0 :(得分:1)
尝试以这种方式从相机和相机获取图像画廊。
// this is local variable.
String selectedImagePath, ServerUploadPath = "" + "", str_response;
public static final int MEDIA_TYPE_IMAGE = 1;
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
private static final int SELECT_PICTURE = 1;
private static final String IMAGE_DIRECTORY_NAME = "Hello Camera";
static File mediaFile;
private Uri fileUri; // file url to store image/video
这种摄像机和摄像机的方法画廊图片拿起。
public void cameraAndGalaryPicture() {
final String[] opString = { "Take Photo", "Choose From Gallery",
"Cancel" };
AlertDialog.Builder dbuilder = new AlertDialog.Builder(this);
dbuilder.setTitle("Add Photo!");
dbuilder.setItems(opString, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (opString[which].equals("Take Photo")) {
fromCamera();
} else if (opString[which].equals("Choose From Gallery")) {
fromFile();
} else {
// Picasso.with(getApplicationContext())
// .load(R.drawable.default_image).resize(360, 200)
// .into(iv_Profile_pic);
rotatedBMP = BitmapFactory.decodeResource(getResources(),
R.drawable.default_image);
Global.mImage = rotatedBMP;
Log.e("Else if", "msg::19th Feb- " + rotatedBMP);
Log.e("Else if", "msg::19th Feb- " + Global.mImage);
// dialog.dismiss();
}
}
});
dbuilder.show();
}
public void fromCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
private static File getOutputMediaFile(int type) {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
IMAGE_DIRECTORY_NAME);
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else {
return null;
}
Log.e("path", "media file:-" + mediaFile);
return mediaFile;
}
public void fromFile() {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select File"),
SELECT_PICTURE);
}
public String getPath(Uri uri, Activity activity) {
String[] projection = { MediaColumns.DATA };
Cursor cursor = activity
.managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
这种压缩图像的方法
private void previewCapturedImage() {
try {
int targetW = 380;
int targetH = 800;
Log.d("Get w", "width" + targetW);
Log.d("Get H", "height" + targetH);
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selectedImagePath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor << 1;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath,
bmOptions);
Matrix mtx = new Matrix();
try {
File imageFile = new File(selectedImagePath);
ExifInterface exif = new ExifInterface(
imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
Log.e("Orintation", " :-" + orientation);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
mtx.postRotate(270);
rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), mtx, true);
if (rotatedBMP != bitmap)
bitmap.recycle();
iv_Profile_pic.setImageBitmap(rotatedBMP);
Global.edtImage = rotatedBMP;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
mtx.postRotate(180);
rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), mtx, true);
if (rotatedBMP != bitmap)
bitmap.recycle();
iv_Profile_pic.setImageBitmap(rotatedBMP);
Global.edtImage = rotatedBMP;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
mtx.postRotate(90);
rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), mtx, true);
if (rotatedBMP != bitmap)
bitmap.recycle();
iv_Profile_pic.setImageBitmap(rotatedBMP);
Global.edtImage = rotatedBMP;
break;
case ExifInterface.ORIENTATION_NORMAL:
mtx.postRotate(0);
rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), mtx, true);
if (rotatedBMP != bitmap)
bitmap.recycle();
iv_Profile_pic.setImageBitmap(rotatedBMP);
Global.edtImage = rotatedBMP;
break;
default:
mtx.postRotate(0);
rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), mtx, true);
if (rotatedBMP != bitmap)
bitmap.recycle();
iv_Profile_pic.setImageBitmap(rotatedBMP);
// img_profilepic.setImageBitmap(BitmapFactory
// .decodeFile(mCurrentPhotoPath));
Global.edtImage = rotatedBMP;
}
Log.i("RotateImage", "Exif orientation: " + orientation);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] data = stream.toByteArray();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
// hear store data in parse table.
ParseFile ParseimageFile1 = new ParseFile("IMG_" + timeStamp
+ ".jpg", data);
ParseimageFile1.saveInBackground();
// ParseimageFile1 variable declare at Globle
} catch (Exception e) {
e.printStackTrace();
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
这是活动结果。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
Log.d("select pah", "path" + selectedImagePath);
previewCapturedImage();
}
}
// if the result is capturing Image
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// successfully captured the image
// display it in image view
selectedImagePath = mediaFile.toString();
previewCapturedImage();
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
}
}
现在将数据存储在解析表中。 并在商店获取数据后
ParseObject obj = new ParseObject("ClassName");
obj.put("table colum name", ParseimageFile1);
obj.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
// TODO Auto-generated method stub
if (e == null) {
// success
} else {
// fail
}
}
});
// now get image from parse table
// in parse table image data type is parseFile
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"Class Name");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> list, ParseException e) {
// TODO Auto-generated method stub
if (e == null) {
// success
for (ParseObject parseObject : list) {
ParseFile image = (ParseFile) parseObject
.get("key_name");
Log.e(key_TAG, "get Image URL " + image.getUrl());
}
} else {
// fail
}
}
});