我想创建一个Android应用程序,捕获并上传图像到服务器,我的代码工作正常,即我可以捕获图像并使用http post方法将其提交到服务器。
但问题是......在我的服务器中,它只获得低质量的图像,实际图像几乎是500-900Kb。但上传的尺寸仅为5-10kb
应用程序中的-preview也显示低质量图像。我不想压缩或调整大小。我需要服务器上的实际尺寸图像
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnCamera:
Intent cameraintent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraintent, 101);
break;
case R.id.btnSubmit:
if (cd.isConnectingToInternet()) {
if (!upflag) {
Toast.makeText(ImageActivity.this, "Image Not Captured..!", Toast.LENGTH_LONG).show();
} else {
saveFile(bitmapRotate, file);
}
} else {
Toast.makeText(ImageActivity.this, "No Internet Connection !", Toast.LENGTH_LONG).show();
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
switch (requestCode) {
case 101:
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
selectedImage = data.getData(); // the uri of the image taken
if (String.valueOf((Bitmap) data.getExtras().get("data")).equals("null")) {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
} else {
bitmap = (Bitmap) data.getExtras().get("data");
}
if (Float.valueOf(getImageOrientation()) >= 0) {
bitmapRotate = rotateImage(bitmap, Float.valueOf(getImageOrientation()));
} else {
bitmapRotate = bitmap;
bitmap.recycle();
}
ivImage.setVisibility(View.VISIBLE);
ivImage.setImageBitmap(bitmapRotate);
// Saving image to mobile internal memory for sometime
String root = getApplicationContext().getFilesDir().toString();
File myDir = new File(root + "/androidlift");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
//Give the file name that u want
fname = "sg" + n + ".jpg";
imagepath = root + "/androidlift/" + fname;
file = new File(myDir, fname);
upflag = true;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
}
public static Bitmap rotateImage(Bitmap source, float angle) {
Bitmap retVal;
Matrix matrix = new Matrix();
matrix.postRotate(angle);
retVal = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
return retVal;
}
// In some mobiles image will get rotate so to correting that this code will help us
private int getImageOrientation() {
final String[] imageColumns = {MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION};
final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,imageColumns, null, null, imageOrderBy);
if (cursor.moveToFirst()) {
int orientation = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION));
System.out.println("orientation===" + orientation);
cursor.close();
return orientation;
} else {
return 0;
}
}
// Saving file to the mobile internal memory
private void saveFile(Bitmap sourceUri, File destination) {
if (destination.exists()) destination.delete();
try {
FileOutputStream out = new FileOutputStream(destination);
sourceUri.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
if (cd.isConnectingToInternet()) {
new DoFileUpload().execute();
} else {
Toast.makeText(ImageActivity.this, "No Internet Connection..", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
class DoFileUpload extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(ImageActivity.this);
pDialog.setMessage("wait uploading Image..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
try {
// Set your file path here
FileInputStream fstrm = new FileInputStream(imagepath);
// Set your server page url (and the file title/description)
HttpFileUpload hfu = new HttpFileUpload("http://www.example.com/test/save.php", "ftitle", "fdescription", fname);
upflag = hfu.Send_Now(fstrm);
} catch (FileNotFoundException e) {
// Error: File not found
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String file_url) {
if (pDialog.isShowing()) {
pDialog.dismiss();
}
if (upflag) {
Toast.makeText(getApplicationContext(), "Uploading Complete", Toast.LENGTH_LONG).show();
此外,我的php脚本中没有图像压缩或调整大小
答案 0 :(得分:1)
密钥&#34;数据&#34;下的附加内容中的位图仅包含图片的缩略图。
阅读这篇文章:https://developer.android.com/training/camera/photobasics.html#TaskPath