Android 如何从图库中选择图像并上传到服务器(parse.com)?
嗨,我正在使用Eclipse和Android新手。 是否有一种简单的方法来选择图像并将其上传到parse.com服务器。Android 如何从图库中选择图像并上传到服务器(parse.com)?,android,parse-platform,Android,Parse Platform,嗨,我正在使用Eclipse和Android新手。 是否有一种简单的方法来选择图像并将其上传到parse.com服务器。 此外,还有一种简单的方法可以从服务器检索图像,并将此图像设置为版面上的图像视图。尝试使用这种方法从照相机和多媒体资料中拾取图像 // this is local variable. String selectedImagePath, ServerUploadPath = "" + "", str_response; public static final int MEDIA_
此外,还有一种简单的方法可以从服务器检索图像,并将此图像设置为版面上的图像视图。尝试使用这种方法从照相机和多媒体资料中拾取图像
// 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();
}
}
现在将数据存储在解析表中,如下所示。
并在存储后获取数据
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
}
}
});
parseobjectobj=新的ParseObject(“ClassName”);
对象put(“表列名称”,ParseimageFile1);
obj.saveInBackground(新的SaveCallback(){
@凌驾
公共作废完成(Parsee异常){
//TODO自动生成的方法存根
如果(e==null){
//成功
}否则{
//失败
}
}
});
//现在从解析表中获取图像
//在解析表中,图像数据类型为parseFile
ParseQuery=新的ParseQuery(
“类名”);
findInBackground(新的FindCallback(){
@凌驾
公共作废完成(列表,异常解析){
//TODO自动生成的方法存根
如果(e==null){
//成功
用于(ParseObject ParseObject:list){
ParseFile image=(ParseFile)parseObject
.get(“key_name”);
Log.e(key_标记,“get Image URL”+Image.getUrl());
}
}否则{
//失败
}
}
});
两者都可以相对容易地完成,您的应用程序中是否已经实现了图像拾取?您的问题已经在stackoverflow上得到了回答。使用搜索工具来找出答案。看看这个,所以回答:我的建议是,在这里发布之前,先试试你自己。。。。并获得@StackOverFlow(上一篇文章)的帮助。但如果你面临任何问题。。。那就到这里来,把你的问题公布出来。
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
}
}
});