5

我已经能够使用相机拍摄照片或从相册中取出并使用此代码在ImageView中显示它。我现在需要做的是使用该图片并将其上传到Parse。我一直在这里和那里搜索,我还没有找到正确的方法来做到这一点。有人可以帮助我吗?有没有可能从ImageView上传图片?谢谢。如何将图像上传到解析在android中?

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 

回答

2

阅读你的答案:

我已经跟着你有之前的代码。我能够上传图像进行解析。但我不知道如何将可绘制源切换为来自相机/图库或imageview的图像。 - 斯坦利桑托索

到:

阿布舍克邦萨尔

我明白你的问题没有解析你的形象?

为了回答你的问题:

我不知道如何切换绘制源是从相机/画廊或imageview的我的形象。

1 - R.drawable.androidbegin似乎是你的问题,但事实是,你已经有你的位图在代码解析:

从画廊 - >

mImageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

从相机 - >

Bitmap photo = (Bitmap) data.getExtras().get("data"); 

2 - 这样我就肃ggest在你的代码的开头

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(); 
      ... 
+0

非常感谢。这工作。由于格式看起来非常不同,我没有意识到这些是我的图像的来源。我能够将上传代码放在底部的onActivityForResult中。有没有可能在onActivityForResult之外做到这一点?我想我需要在onCreate中做这件事,这样我就可以在一次点击中解析其他信息(用户名和东西),以便它们进入相同的对象/类。 这个代码的意思是,如果我从一个画廊中挑选了一个图片,然后改变主意拍照,我会上传2张图片。我如何做,所以我只上传1张图片? –

+0

嗨,是的,你可以在自己的功能中使用上传代码,那么你可以从任何你喜欢的地方引用它。例如:private myUploading(){//将其转换为字节ByteArrayOutputStream stream = new ByteArrayOutputStream(); //压缩图像以降低质量1 - 100 yourbitmap.compress(Bitmap.CompressFormat.PNG,100,stream); byte [] image = stream.toByteArray(); ...}并在每次休息前的onActivityResult中;你打电话给上传:myUploading();瞧。 – mourad

+0

也是我的答案?我非常想要绿色标志。 – mourad

2

在互联网上有很好的教程可用。从本质上讲以下是你需要做什么

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(); 

来源:this tutorial

也看到了这个问题How to upload an image in parse server using parse api in android

+0

我已经跟着你有之前的代码。我能够上传图像进行解析。但我不知道如何将可绘制源切换为来自相机/图库或imageview的图像。 –

0

点击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"); 
          } 
         }); 


    } 
相关问题