0
我想打一個Android應用程序捕獲和上傳圖像到服務器,我的代碼工作正常,即我可以捕捉的圖像,並使用HTTP POST方法提交給服務器。Android的採集和上傳只獲得低質量的圖像
但問題是......在我的服務器,它僅獲得圖像與低質量的,實際的圖像幾乎是500-900Kb。但是上傳的尺寸只有5-10kb
內的應用程序 - 預覽也顯示出低質量的圖像。我不想壓縮或調整大小。我需要的實際大小的圖片我的服務器上
@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腳本調整
您是第####號碼誰報告此問題。您的問題每週至少報告兩次或三次。所以有一點Google會給你所有這些帖子。你的開始意圖是錯誤的,因爲你應該給它一個額外的輸出參數,告訴它應該存儲圖片的位置。之後,您可以使用提供的文件路徑上傳文件而不是位圖。 – greenapps
是的,我知道了。 –