我想将高质量的图片上传到服务器并在我的应用中的Imageview中获取。我能够这样做。但问题是图像质量非常低。如何将想要的图像分辨率上传到服务器并在imageview中获取。帮我。谢谢
这是我的代码:
public class ChangeProfileActivity extends Activity {
ImageView updateBT, IV;
private SharedPreferences saveImagePref;
private SharedPreferences.Editor saveImagePrefEditor;
protected static final int CAMERA_REQUEST = 0;
protected static final int GALLERY_PICTURE = 1;
private Intent pictureActionIntent = null;
Bitmap bitmap;
private TouchImageView touch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_change_profile);
updateBT = (ImageView) findViewById(R.id.buttonUpdate);
dialog = new AlertDialogCheck(ChangeProfileActivity.this);
IV = (ImageView) findViewById(R.id.PersonalPictureIV);
saveImagePref = getSharedPreferences("saveImage", MODE_PRIVATE);
saveImagePrefEditor = saveImagePref.edit();
saveDOBPref = getSharedPreferences("saveImage", MODE_PRIVATE);
saveDOBPrefEditor = saveImagePref.edit();
registerForContextMenu(IV);
A = saveImagePref.getString("SAVE", "0");
bitmap = StringToBitMap(A);
if (bitmap != null) {
bitmap = Bitmap.createScaledBitmap(bitmap, 250, 250, false);
IV.setImageBitmap(bitmap);
}
if (bitmap == null) {
bitmap = BitmapFactory.decodeResource(this.getResources(),
R.drawable.user_profile_image);
IV.setImageBitmap(bitmap);
IV.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startDialog();
uploadImage();
}
});
}
IV.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startDialog();
}
});
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
}
private void startDialog() {
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
myAlertDialog.setTitle("Upload Pictures Option");
myAlertDialog.setMessage("How do you want to set your picture?");
myAlertDialog.setPositiveButton("Gallery",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
pictureActionIntent = new Intent(
Intent.ACTION_GET_CONTENT, null);
pictureActionIntent.setType("image/*");
pictureActionIntent.putExtra("return-data", true);
startActivityForResult(pictureActionIntent,
GALLERY_PICTURE);
}
});
myAlertDialog.setNegativeButton("Camera",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
pictureActionIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(pictureActionIntent,
CAMERA_REQUEST);
}
});
myAlertDialog.show();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_PICTURE) {
if (resultCode == RESULT_OK) {
if (data != null) {
// our BitmapDrawable for the thumbnail
BitmapDrawable bmpDrawable = null;
// try to retrieve the image using the data from the intent
Cursor cursor = getContentResolver().query(data.getData(),
null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(ImageColumns.DATA);
String fileSrc = cursor.getString(idx);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize=1;
bitmap = BitmapFactory.decodeFile(fileSrc);
bitmap = Bitmap.createScaledBitmap(bitmap, 250, 250,
true);
saveImagePrefEditor.clear();
saveImagePrefEditor.commit();
saveImagePrefEditor.putString("SAVE",
BitMapToString(bitmap));
saveImagePrefEditor.commit();
IV.setImageBitmap(bitmap);
// touch.setImageBitmap(bitmap);+
A = saveImagePref.getString("SAVE", "0");
uploadImage();
} else {
bmpDrawable = new BitmapDrawable(getResources(), data
.getData().getPath());
IV.setImageDrawable(bmpDrawable);
// touch.setImageBitmap(bitmap);
A = saveImagePref.getString("SAVE", "0");
uploadImage();
}
} else {
Toast.makeText(getApplicationContext(), "Cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (requestCode == CAMERA_REQUEST) {
if (resultCode == RESULT_OK) {
if (data.hasExtra("data")) {
bitmap = (Bitmap) data.getExtras().get("data");
bitmap = Bitmap.createScaledBitmap(bitmap, 250, 250, false);
saveImagePrefEditor.clear();
saveImagePrefEditor.commit();
saveImagePrefEditor.putString("SAVE",
BitMapToString(bitmap));
saveImagePrefEditor.commit();
// update the image view with the bitmap
IV.setImageBitmap(bitmap);
// touch.setImageBitmap(bitmap);
A = saveImagePref.getString("SAVE", "0");
uploadImage();
} else if (data.getExtras() == null) {
Toast.makeText(getApplicationContext(),
"No extras to retrieve!", Toast.LENGTH_SHORT)
.show();
BitmapDrawable thumbnail = new BitmapDrawable(
getResources(), data.getData().getPath());
// update the image view with the newly created drawable
IV.setImageDrawable(thumbnail);
// touch.setImageBitmap(bitmap);
A = saveImagePref.getString("SAVE", "0");
uploadImage();
}
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Cancelled",
Toast.LENGTH_SHORT).show();
} }
}
public String BitMapToString(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String temp = Base64.encodeToString(b, Base64.DEFAULT);
return temp;
}
public Bitmap StringToBitMap(String encodedString) {
try {
byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0,
encodeByte.length);
return bitmap;
} catch (Exception e) {
e.getMessage();
return null;
}}
}
答案 0 :(得分:1)
您可以使用Android最佳实践。
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static Bitmap decodeFile(int width , int height ,File f) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = calculateInSampleSize(o, width, height);
//System.out.println("Scale: " + scale);
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
//System.out.println("--- exception "+e.toString());
e.printStackTrace();
}
return null;
}