如何使用android中的parse api在解析服务器上传图像

puj*_*tav 21 android image parse-platform android-drawable

我想在android中的解析云服务器上传一个图像.但我无法这样做.

我试过以下代码:

    Drawable drawable = getResources().getDrawable(R.drawable.profilepic) ;
    Bitmap bitmap = (Bitmap)(Bitmap)drawable()
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] data = stream.toByteArray();                

    ParseFile imageFile = new ParseFile("image.png", data);
    imageFile.saveInBackground();
Run Code Online (Sandbox Code Playgroud)

请让我知道我该怎么做.


我已经添加了一笔赏金来找到这个常见问题最佳权威代码

小智 18

经过几个小时的挣扎之后,代码段对我来说很有用.

1.活动类的数据成员

Bitmap bmp;
Intent i;
Uri BmpFileName = null;
Run Code Online (Sandbox Code Playgroud)

2.启动相机.目标是启动摄像机活动,BmpFileName将参考存储到文件

String storageState = Environment.getExternalStorageState();
if (storageState.equals(Environment.MEDIA_MOUNTED)) {

String path = Environment.getExternalStorageDirectory().getName() + File.separatorChar + "Android/data/" + this.getPackageName() + "/files/" + "Doc1" + ".jpg";

File photoFile = new File(path);
try {
if (photoFile.exists() == false) { 
photoFile.getParentFile().mkdirs();
photoFile.createNewFile();
}
} 
catch (IOException e) 
{
Log.e("DocumentActivity", "Could not create file.", e);
}
Log.i("DocumentActivity", path);
BmpFileName = Uri.fromFile(photoFile);
i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, BmpFileName);
startActivityForResult(i, 0);
Run Code Online (Sandbox Code Playgroud)

3.通过覆盖onActivityResult从Camera输出中读取内容.目标是获得bmp变量评估.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
try {
bmp = MediaStore.Images.Media.getBitmap( this.getContentResolver(), BmpFileName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {

// TODO Auto-generated catch block
e.printStackTrace();
}
// Myocode to display image on UI - You can ignore
if (bmp != null)
IV.setImageBitmap(bmp);
}
}
Run Code Online (Sandbox Code Playgroud)

4.在保存事件上

// MUST ENSURE THAT YOU INITIALIZE PARSE
Parse.initialize(mContext, "Key1", "Key2");

ParseObject pObj = null;
ParseFile pFile = null ;
pObj = new ParseObject ("Document");
pObj.put("Notes", "Some Value");

// Ensure bmp has value
if (bmp == null || BmpFileName == null) {
Log.d ("Error" , "Problem with image"
return;
}

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 100, stream);
pFile = new ParseFile("DocImage.jpg", stream.toByteArray());
try 
{
pFile.save();
pObj.put("FileName", pFile);
pObj.save();
_mParse.DisplayMessage("Image Saved");
} 
catch (ParseException e) 
{
// TODO Auto-generated catch block
_mParse.DisplayMessage("Error in saving image");
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

//在我的案例中完成活动.你可以选择其他东西完成();

所以这是与其他人的关键区别

  • 我打电话给初始化解析.你可能会笑一下,但大家花了一个小时的调试代码却没有意识到解析没有被初始化
  • 使用Save而不是SaveInBackground.我知道它可以保留活动,但这对我来说是理想的行为,更重要的是它有效

如果它不起作用,请告诉我


Mak*_*ple 8

将ParseObject保存在后台

// ParseObject
  ParseObject pObject = new ParseObject("ExampleObject");
  pObject.put("myNumber", number);
  pObject.put("myString", name);
  pObject.saveInBackground(); // asynchronous, no callback
Run Code Online (Sandbox Code Playgroud)

使用回调保存在后台

pObject.saveInBackground(new SaveCallback () {
   @Override
   public void done(ParseException ex) {
    if (ex == null) {
        isSaved = true;
    } else {
        // Failed
        isSaved = false;
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

save ...()方法的变体包括以下内容:

    saveAllinBackground() saves a ParseObject with or without a callback.
    saveAll(List<ParseObject> objects) saves a list of ParseObjects.
    saveAllinBackground(List<ParseObject> objects) saves a list of ParseObjects in the 
    background.
    saveEventually() lets you save a data object to the server at some point in the future; use 
    this method if the Parse cloud is not currently accessible.
Run Code Online (Sandbox Code Playgroud)

一旦ParseObject成功保存在云上,就会为其分配一个唯一的Object-ID.此Object-ID非常重要,因为它唯一标识该ParseObject实例.例如,您可以使用Object-ID来确定对象是否已成功保存在云上,用于检索和刷新给定的Parse对象实例,以及用于删除特定的ParseObject.

我希望你能解决你的问题..

  • 是的,将图像视为文件.但是parse.com上的Parse指南说**为具有文件扩展名的文件命名是很重要的.这让Parse可以找出文件类型并相应地处理它.因此,如果您要存储PNG图像,请确保您的文件名以.png结尾.** (2认同)