我正在尝试将用户选择的图片上传到Parse.com中的阵列上。 我无法做到这一点。问题是如何保存以及如何将这些照片取回? 这是我上传图片的代码(这是一个例子,所以我使用了drawable的图片:
Button btnadd = (Button) findViewById(R.id.btnadd);
btnadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap;
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.one);
myArrayOfParseFiles.add(add(bitmap));
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.two);
myArrayOfParseFiles.add(add(bitmap));
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.three);
myArrayOfParseFiles.add(add(bitmap));
ParseObject object = ParseObject.create("Pictures");
object.add("pic", myArrayOfParseFiles);
object.put("username", ParseUser.getCurrentUser().getUsername());
object.saveInBackground();
}
});
Button btnnext = (Button) findViewById(R.id.btnnext);
btnnext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentObj = new Intent(Welcome.this, Show.class);
startActivity(intentObj);
finish();
}
});
}
public ParseFile add(Bitmap pic){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
pic.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();
ParseFile file = new ParseFile(image);
file.saveInBackground();
return file;
}
这是获取数组和图片的代码。我得到了空,我无法得到照片。而且,我如何在循环中为解析的整个数组(在特定的原始数据中)执行此操作并将其放在位图列表中。
ParseQuery<ParseObject> query = ParseQuery.getQuery("Pictures");
query.whereEqualTo("username", ParseUser.getCurrentUser().getUsername());
query.getFirstInBackground(new GetCallback<ParseObject>() {
@Override
public void done(ParseObject object, ParseException e) {
List<ParseFile> list = object.getList("pic");
try {
URL url = new URL(list.get(0).getUrl());
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
picone.setImageBitmap(bmp);
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
答案 0 :(得分:1)
将位图转换为字符串以进行上传:
private String getStringFromBitmap(Bitmap bitmapPicture) {
final int COMPRESSION_QUALITY = 100;
String encodedImage;
ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
bitmapPicture.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY,
byteArrayBitmapStream);
byte[] b = byteArrayBitmapStream.toByteArray();
encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
return encodedImage;
}
并将您的字符串转换为位图:
private Bitmap getBitmapFromString(String jsonString) {
byte[] decodedString = Base64.decode(jsonString, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
return decodedByte;
}
你可以将你的字符串保存在文件中并将其保存在服务器端或...然后下载字符串并再次将其转换为位图...