gpt4 book ai didi

java - 保存图像到解析

转载 作者:行者123 更新时间:2023-12-01 12:39:00 25 4
gpt4 key购买 nike

我试图让用户将他们的图像(个人资料图片)上传到解析中,作为他们的个人资料创建页面的一部分,他们必须在其中填写各种信息。用户将能够在 ImageView 中预览他们的图片。

我尝试通过采取以下步骤来实现这一目标。

1) 创建一个按钮,允许用户从图库中检索图片,并将其上传到创建的 ImageView 中。

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

}

}
}

这部分通常已经成功执行,用户能够从他们的图库上传图像并在 ImageView 中直观地看到它。

2)在解析时存储用户上传的图像这是我正在努力的领域。在代码之间添加了注释以进一步澄清,并突出显示我的问题。

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

关于java - 保存图像到解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25295154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com