【问题标题】:Trouble setting Bitmap to ParseImageView?无法将位图设置为 ParseImageView?
【发布时间】:2023-04-01 12:05:02
【问题描述】:

我在将位图设置为我的一个布局中的 ParseImageView 时遇到问题。我也无法将位图作为 ParseFile 保存到 ParseDB,我相信这是因为 OnActivityResult() 上没有正确返回数据;

有人知道我在用户选择图片并裁剪/保存后是否正确检索位图数据吗?

public class UserActivity extends Fragment implements OnClickListener {

Context context;
public ParseImageView ivProfilePic;
private TextView tvUserName, tvBio, tvTagLine;

private static final String TAG = "OwlSample";
private static final int RESULT_OK = 0;
private static final int RESULT_LOAD_IMAGE = 888;

final int PHOTO_WIDTH = 500;
final int PHOTO_HEIGHT = 500;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    if (container == null) {
        // We have different layouts, and in one of them this
        // fragment's containing frame doesn't exist. The fragment
        // may still be created from its saved state, but there is
        // no reason to try to create its view hierarchy because it
        // won't be displayed. Note this is not needed -- we could
        // just run the code below, where we would create and return
        // the view hierarchy; it would just never be used.
        return null;
    }

    // Inflate the layout for this fragment
    LinearLayout mLayout = (LinearLayout) inflater.inflate(
            R.layout.activity_user_fragment, container, false);

    Button logoutButton = (Button) mLayout
            .findViewById(R.id.log_out_button);
    logoutButton.setOnClickListener(this);

    Button editProfileButton = (Button) mLayout
            .findViewById(R.id.btn_editProfile);
    editProfileButton.setOnClickListener(this);

    ImageButton btnFindFriends = (ImageButton) mLayout
            .findViewById(R.id.btn_friends);
    btnFindFriends.setOnClickListener(this);

    tvUserName = (TextView) mLayout.findViewById(R.id.text_name);
    tvBio = (TextView) mLayout.findViewById(R.id.text_bio);
    tvTagLine = (TextView) mLayout.findViewById(R.id.text_tagline);
    ivProfilePic = (ParseImageView) mLayout
            .findViewById(R.id.imageViewUserProfile);

    ivProfilePic.setOnClickListener(this);

    updateViewsWithProfileInfo();
    return mLayout;
}

@Override
public void onResume() {
    super.onResume();

    ParseUser currentUser = ParseUser.getCurrentUser();
    if (currentUser != null) {
        // Check if the user is currently logged
        // and show any cached content
        updateViewsWithProfileInfo();
    } else {
        // If the user is not logged in, go to the
        // activity showing the login view.
        showLoginActivity();
    }
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.log_out_button:
        Log.e(TAG, "Tapped sign out");
        onLogoutButtonClicked();
        break;

    case R.id.btn_editProfile:
        Log.e(TAG, "Tapped editProfile button");

        Intent intent = new Intent(getActivity(), EditProfileActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(intent);
        break;

    case R.id.btn_friends:
        Log.e(TAG, "Tapped editProfile button");

        Intent intent1 = new Intent(getActivity(),
                FriendSearchActivity.class);
        intent1.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(intent1);
        break;

    case R.id.imageViewUserProfile:

        Intent intent2 = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

        intent2.setType("image/*");
        intent2.putExtra("crop", "true");
        intent2.putExtra("scale", true);
        intent2.putExtra("outputX", PHOTO_WIDTH);
        intent2.putExtra("outputY", PHOTO_HEIGHT);
        intent2.putExtra("aspectX", 1);
        intent2.putExtra("aspectY", 1);
        intent2.putExtra("return-data", true);
        startActivityForResult(intent2, RESULT_LOAD_IMAGE);

    }

}

private void showLoginActivity() {
    Intent intent = new Intent(getActivity(), MainActivity.class);
    startActivity(intent);
}

private void onLogoutButtonClicked() {
    // Log the user out
    ParseUser.logOut();

    // Go to the login view
    showLoginActivity();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != RESULT_OK) {
        return;
    }

    if (requestCode == RESULT_LOAD_IMAGE) {
        final Bundle extras = data.getExtras();

        if (extras != null) {

            final ParseUser currentUser = ParseUser.getCurrentUser();

            Bitmap bitmap = extras.getParcelable("data");
            ivProfilePic.setImageBitmap(bitmap);

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] data1 = stream.toByteArray();

            final ParseFile profilePicture = new ParseFile(
                    "profilePicture", data1);
            profilePicture.saveInBackground(new SaveCallback() {
                public void done(ParseException e) {
                    if (e != null) {

                    } else {

                        currentUser.put("photo", profilePicture);
                        currentUser.saveInBackground();
                        updateViewsWithProfileInfo();
                    }
                }
            });

        }
    }
}

private void updateViewsWithProfileInfo() {
    ParseUser currentUser = ParseUser.getCurrentUser();

    if (currentUser.getString("displayName") != null) {
        tvUserName.setText(currentUser.getString("displayName"));
    } else {
        tvUserName.setText("");
    }

    if (currentUser.getString("bio") != null) {
        tvBio.setText(currentUser.getString("bio"));
    } else {
        tvBio.setText("");
    }

    if (currentUser.getString("tagline") != null) {
        tvTagLine.setText(currentUser.getString("tagline"));
    } else {
        tvTagLine.setText("");
    }

    ParseFile imageFile = currentUser.getParseFile("photo");
    if (imageFile != null) {
        ivProfilePic.setParseFile(imageFile);
        ivProfilePic.loadInBackground();
    }

}

}

【问题讨论】:

    标签: android image bitmap imageview parse-platform


    【解决方案1】:

    保存位图以解析为文件上传...

    将文件字节或位图的字节放入 byteArray 并将数组包装在 POST 的正文中以进行解析。

    然后,使用解析文件上传响应中包含的名称和 URL,将指向该文件的指针/关系作为另一个类中的元素。当您想将位图加载到视图时,您可以使用与 URL 的关系来提供给一些处理照片加载的框架。

    示例:

      final Uri uri = Uri.parse(arg);
      final BitmapFactory.Options options = new BitmapFactory.Options();
      Runnable myrun =  new Runnable() {
          public void run() {
                options.inJustDecodeBounds = true;
                try {
                    BitmapFactory.decodeStream(
                            getContentResolver().openInputStream(uri),null,options);
    

    【讨论】:

    • 你相信我正确地检索位图吗?我使用位图创建了一个 Byte[],并按照文档执行了创建 ParseFile 所需的操作。 -- 位图 bitmap = extras.getParcelable("data"); -- 这条线看起来正确吗?
    • 为什么需要parcelable?只需传递指向您上传的照片的 URL 字符串。然后使用解码器获取位图或使用框架加载照片,因此您只需将 url 和 imageView 传递给框架
    • 我将研究如何获取所创建图像的字符串 URL。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    相关资源
    最近更新 更多