在Edit_Staff Activity 中,有一个ImageView
。 ImageView
将显示从 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/