如何在Android中将图片上传到Parse? [英] How to upload image to Parse in android?
问题描述
我已经能够使用相机拍摄照片或从图库中拍摄并使用此代码在ImageView中显示.我现在需要做的是使用该图片并将其上传到Parse.我一直在这里和那里进行谷歌搜索,但是还没有找到正确的方法.有人可以帮我吗?是否可以从ImageView上传图像?谢谢.
I have been able to take a picture using a camera or take from a gallery and show it in an ImageView using this code. What I need to do now is to use that picture and upload it to Parse. I have been googling here and there to do this, and I haven't found the right way to do it. Can someone please help me with this? Is it possible to upload the image from the ImageView? Thank you.
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
推荐答案
阅读答案:
我已经遵循了您之前的代码.我能够上传图片进行解析.但是我不知道如何将可绘制源切换为来自相机/画廊或imageview的图像. –斯坦利·桑托索(Stanley Santoso)
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
Abhishek Bansal
我了解您的问题不是解析图片吗?
I understand that your problem is not parsing your image ?
尝试回答您的问题:
我不知道如何从相机/画廊或imageview切换可绘制源为我的图像.
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-所以我建议在代码的开头声明一个Bitmap类型的变量
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();
...
这篇关于如何在Android中将图片上传到Parse?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!