android - 错误:(191, 78)错误:在android中找不到符号变量ARGB_8888

标签 android image bitmap

Edit_Staff Activity 中,有一个ImageViewImageView 将显示从 MySQL 获取的图像。

private void showStaff(String json) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
            JSONObject c = result.getJSONObject(0);
            String image=c.getString(Config.TAG_IMAGE);
            byte[] data= Base64.decode(image,0);
            Bitmap b=BitmapFactory.decodeByteArray(data,0,data.length);
            image1.setImageBitmap(b);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } 

imageView下方,有一个更改图片按钮,供用户更改图片。

 public void activeGallery()
    {
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, RESULT_LOAD_IMAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case RESULT_LOAD_IMAGE:
                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();
                    Bitmap a = (BitmapFactory.decodeFile(picturePath));
                    photo = scaleBitmap(a, 200, 150);
                    image1.setImageBitmap(photo);
                }

  public Bitmap scaleBitmap(Bitmap bitmap,int newWidth,int newHeight) {
        Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);

        float ratioX = newWidth / (float) bitmap.getWidth();
        float ratioY = newHeight / (float) bitmap.getHeight();
        float middleX = newWidth / 2.0f;
        float middleY = newHeight / 2.0f;

        Matrix scaleMatrix = new Matrix();
        scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

        Canvas canvas = new Canvas(scaledBitmap);
        canvas.setMatrix(scaleMatrix);
        canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));

        return scaledBitmap;
    }

为了从 MySQL 获取图像,我使用这个库

import com.example.project.myapplication.Handler.Config;

但是scaleBitmap里面的Config.ARGB_8888无法解决。我改为 import android.graphics.Bitmap.Config;,但是showStaff里面的Config.TAG_IMAGE无法解析。

我该如何解决这个问题?谢谢

最佳答案

您收到错误是因为您的 Config 类没有字段 ARGB_8888

您可以将类名更改为 Config 以外的名称,以便将它们与导入一起使用,或者您可以使用 android.graphics.Bitmap.Config 的完整命名空间。 .

类似于:

import android.graphics.Bitmap.Config;
// The rest of your code
// ...
// And make a call somewhat like this:
Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, android.graphics.Bitmap.Config.ARGB_8888);

但我认为将 Config 类重命名为 Configs 之类的名称以避免命名空间冲突会更快。

了解有关类和命名空间的更多信息 here ,另外,检查this question .

关于android - 错误:(191, 78)错误:在android中找不到符号变量ARGB_8888,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34646505/

相关文章:

java - 在 Android 中使用来自 Google Places API 的 JSON 响应

html - 使用最大高度、宽度自动限制 div 中的 img

jquery - 我需要根据窗口宽度更改 img

android - 使用 Volley 或 Picasso 从 URL 加载并显示 WebM 和 Gif 到 ImageView

android - 缩放后保存全尺寸图像的位图

android - 使用 ACTION_IMAGE_CAPTURE 拍照并 setImageBitmap 来显示它

java - keytool不会提示输入android密码

android - 当我们点击android中的按钮时如何显示Spinner-List?

android - 获取android联系人详细信息非常慢

Android - 如何加载大图像(将其转换为较小的尺寸)并将其作为 Base64 字节数组发送(避免 OutOfMemory 错误)