Java 保存要解析的图像
我试图让用户上传他们的图像(个人资料图片)到解析作为他们的个人资料创建页面的一部分,他们将不得不填写各种信息。用户可以在图像视图中预览他们的图片 我试图通过以下步骤实现这一目标 1) 创建一个按钮,允许用户从他们的图库中检索图片,并将其上载到创建的ImageView中Java 保存要解析的图像,java,android,android-layout,android-activity,parse-platform,Java,Android,Android Layout,Android Activity,Parse Platform,我试图让用户上传他们的图像(个人资料图片)到解析作为他们的个人资料创建页面的一部分,他们将不得不填写各种信息。用户可以在图像视图中预览他们的图片 我试图通过以下步骤实现这一目标 1) 创建一个按钮,允许用户从他们的图库中检索图片,并将其上载到创建的ImageView中 Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect); buttonLoadImage.setOnClickListener(n
Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && 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();
ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
这一部分通常已经成功执行,用户能够从他们的图库上传图像,并在图像视图中看到图像的可视化显示
2) 将用户上传的图像存储在parse上
这是我正在努力的领域。为了进一步澄清,在代码之间添加了注释,并强调了我的问题
ParseUser currentUser = ParseUser.getCurrentUser();
/* This is the section where the images is converted, saved, and uploaded. I have not been able Locate the image from the ImageView, where the user uploads the picture to imageview from either their gallery and later on from facebook */
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
/*to be retrieved from image view */);
// 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("profilePicture.png", image);
// Upload the image into Parse Cloud
// Create a column named "Profile Picture" and set the string
currentUser.put("ImageName", "Profile Picture");
// Create a column named "ImageFile" and insert the image
currentUser.put("ProfilePicture", file);
// Create the class and the columns
currentUser.put("name", name);
currentUser.put("age", age);
currentUser.put("headline", headline);
currentUser.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
setProgressBarIndeterminateVisibility(false);
if (e == null) {
// Success!
Intent intent = new Intent(ProfileCreation.this, MoodActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
}
});
如果你能在任何方面帮助我,那将是非常有帮助的。
如果您需要进一步澄清,请告诉我。
提前谢谢。您忘了先保存文件
ParseFile file = new ParseFile("profilePicture.png", image);
file.saveinBackground()
您可以定义一个名为util.java的新类,并在该类中编写一个方法
public static byte[] getbytearray(Bitmap bm){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
现在你不需要一次又一次地重复这些基本路线。
更重要的是,您应该使用asynctask来避免内存不足错误,如-
public class storeimage extends AsyncTask<Bitmap,Void,Void>{
@Override
protected Void doInBackground(Bitmap... params) {
byte[] byteArray = util.getbytearray(params[0]);
ParseUser user = ParseUser.getCurrentUser();
ParseFile parseFile = new ParseFile(user.getUsername()+"dp.png",byteArray);
parseFile.saveInBackground();
user.put("dp", parseFile);
user.saveInBackground();
Log.d("mayank","asynctask successful");
// Bitmap dpbitmap2 = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
//ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
// dpbitmap.compress(Bitmap.CompressFormat.PNG, 0, stream2);
// byte[] byteArray2 = stream2.toByteArray();
// ParseFile parseFile2 = new ParseFile(user.getUsername()+"dp_circle.png",byteArray2);
// ParseObject dp = new ParseObject("dp");
// dp.put("pic",byteArray2);
// dp.pinInBackground();
return null;
}
}
感谢您的及时回复,并提醒您将图片保存在后台。在完成这些代码行时,我也遇到了很大的问题:“Bitmap Bitmap=BitmapFactory.decodeResource(getResources(),/*从图像视图中检索*);“在这里,我希望getResources不是从我的drawable中检索,而是从imageview中检索,从用户图像库中显示imageview的图片的位置(不久用户将能够从facebook上传图像到他们的imageview中)。
new storeimage().execute(BitmapFactory.decodeFile(picturePath));