android 如何上传图片到 Parse?
How to upload image to Parse in android?
我已经能够使用相机拍摄照片或从图库中拍摄并使用此代码在 ImageView 中显示。我现在需要做的是使用那张图片并将其上传到 Parse。我一直在这里和那里进行谷歌搜索,但我还没有找到正确的方法。有人可以帮我吗?是否可以从 ImageView 上传图像?谢谢。
protected Button mFromCamera;
protected Button mFromGallery;
protected ImageView mImageView;
private static final int CAMERA_REQUEST = 1888;
private static final int SELECT_PHOTO = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize ImageView
mImageView = (ImageView) findViewById(R.id.ImgPrev);
//Initialize Camera
mFromCamera = (Button) findViewById(R.id.FromCamera);
//use camera
mFromCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
} //use camera end
});
//initialize button
mFromGallery = (Button) findViewById(R.id.FromGallery);
//pick a photo
mFromGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
});//pick a photo end
}
//previewing Image
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
//from the gallery
case SELECT_PHOTO:
if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && null!= data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
mImageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
break;
//from the camera
case CAMERA_REQUEST:
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
mImageView.setImageBitmap(photo);
}
break;
}
}//Preview Image End
互联网上有很好的教程。基本上以下是您需要做的
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.androidbegin);
// Convert it to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Compress image to lower quality scale 1 - 100
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();
// Create the ParseFile
ParseFile file = new ParseFile("androidbegin.png", image);
// Upload the image into Parse Cloud
file.saveInBackground();
// Create a New Class called "ImageUpload" in Parse
ParseObject imgupload = new ParseObject("ImageUpload");
// Create a column named "ImageName" and set the string
imgupload.put("ImageName", "AndroidBegin Logo");
// Create a column named "ImageFile" and insert the image
imgupload.put("ImageFile", file);
// Create the class and the columns
imgupload.saveInBackground();
也看到这个问题How to upload an image in parse server using parse api in android
单击 here 获取 AsyncHttpClient 库并上传您的图片。
上传你的图片是脂肪。
public void uploadImage(Bitmap img_bit)
{
AsyncHttpClient imgupload = new AsyncHttpClient();
RequestParams params = new RequestParams();
if (img_bit != null) {
byte[] imagebyte;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
img_bit.compress(Bitmap.CompressFormat.PNG, 100, bao);
imagebyte = bao.toByteArray();
params.put("image", new ByteArrayInputStream(imagebyte), "test"+ System.currentTimeMillis() + ".png");
}
imgupload.post("url",params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int arg0, Header[] arg1,
byte[] arg2) {
System.out.println("Image Upload successfully");
}
@Override
public void onFailure(int arg0, Header[] arg1,
byte[] arg2, Throwable arg3) {
System.out.println("faile the data");
}
});
}
正在阅读您的回答:
I already followed the code that you have before. I was able to upload the image to parse. but I dont know how to switch the drawable source to be my image from camera/gallery or imageview. – stanley santoso
至:
Abhishek Bansal
我了解到您的问题是没有解析您的图片?
尝试回答您的问题:
I dont know how to switch the drawable source to be my image from camera/gallery or imageview.
1 - R.drawable.androidbegin 似乎是您的问题,但事实是您已经在代码中解析了位图:
来自画廊 ->
mImageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
来自相机 ->
Bitmap photo = (Bitmap) data.getExtras().get("data");
2 - 所以我建议在代码开头声明一个位图类型的变量
private Bitmap yourbitmap;
3 - 然后在您的代码中为画廊和相机分配位图并使用它来解析它。
...
yourbitmap = BitmapFactory.decodeFile(picturePath);
...
yourbitmap = (Bitmap) data.getExtras().get("data");
...
4 - 最后你可以像这样使用你的位图了:
// Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
// R.drawable.androidbegin);
// Convert it to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Compress image to lower quality scale 1 - 100
yourbitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();
...
我已经能够使用相机拍摄照片或从图库中拍摄并使用此代码在 ImageView 中显示。我现在需要做的是使用那张图片并将其上传到 Parse。我一直在这里和那里进行谷歌搜索,但我还没有找到正确的方法。有人可以帮我吗?是否可以从 ImageView 上传图像?谢谢。
protected Button mFromCamera;
protected Button mFromGallery;
protected ImageView mImageView;
private static final int CAMERA_REQUEST = 1888;
private static final int SELECT_PHOTO = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize ImageView
mImageView = (ImageView) findViewById(R.id.ImgPrev);
//Initialize Camera
mFromCamera = (Button) findViewById(R.id.FromCamera);
//use camera
mFromCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
} //use camera end
});
//initialize button
mFromGallery = (Button) findViewById(R.id.FromGallery);
//pick a photo
mFromGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
});//pick a photo end
}
//previewing Image
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
//from the gallery
case SELECT_PHOTO:
if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && null!= data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
mImageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
break;
//from the camera
case CAMERA_REQUEST:
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
mImageView.setImageBitmap(photo);
}
break;
}
}//Preview Image End
互联网上有很好的教程。基本上以下是您需要做的
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.androidbegin);
// Convert it to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Compress image to lower quality scale 1 - 100
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();
// Create the ParseFile
ParseFile file = new ParseFile("androidbegin.png", image);
// Upload the image into Parse Cloud
file.saveInBackground();
// Create a New Class called "ImageUpload" in Parse
ParseObject imgupload = new ParseObject("ImageUpload");
// Create a column named "ImageName" and set the string
imgupload.put("ImageName", "AndroidBegin Logo");
// Create a column named "ImageFile" and insert the image
imgupload.put("ImageFile", file);
// Create the class and the columns
imgupload.saveInBackground();
也看到这个问题How to upload an image in parse server using parse api in android
单击 here 获取 AsyncHttpClient 库并上传您的图片。 上传你的图片是脂肪。
public void uploadImage(Bitmap img_bit)
{
AsyncHttpClient imgupload = new AsyncHttpClient();
RequestParams params = new RequestParams();
if (img_bit != null) {
byte[] imagebyte;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
img_bit.compress(Bitmap.CompressFormat.PNG, 100, bao);
imagebyte = bao.toByteArray();
params.put("image", new ByteArrayInputStream(imagebyte), "test"+ System.currentTimeMillis() + ".png");
}
imgupload.post("url",params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int arg0, Header[] arg1,
byte[] arg2) {
System.out.println("Image Upload successfully");
}
@Override
public void onFailure(int arg0, Header[] arg1,
byte[] arg2, Throwable arg3) {
System.out.println("faile the data");
}
});
}
正在阅读您的回答:
I already followed the code that you have before. I was able to upload the image to parse. but I dont know how to switch the drawable source to be my image from camera/gallery or imageview. – stanley santoso
至:
Abhishek Bansal
我了解到您的问题是没有解析您的图片?
尝试回答您的问题:
I dont know how to switch the drawable source to be my image from camera/gallery or imageview.
1 - R.drawable.androidbegin 似乎是您的问题,但事实是您已经在代码中解析了位图:
来自画廊 ->
mImageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
来自相机 ->
Bitmap photo = (Bitmap) data.getExtras().get("data");
2 - 所以我建议在代码开头声明一个位图类型的变量
private Bitmap yourbitmap;
3 - 然后在您的代码中为画廊和相机分配位图并使用它来解析它。
...
yourbitmap = BitmapFactory.decodeFile(picturePath);
...
yourbitmap = (Bitmap) data.getExtras().get("data");
...
4 - 最后你可以像这样使用你的位图了:
// Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
// R.drawable.androidbegin);
// Convert it to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Compress image to lower quality scale 1 - 100
yourbitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();
...