2017-05-09 89 views
0

我能夠捕捉圖像,也能夠裁剪圖像。但是當我上傳圖像時,我無法保存裁剪後的圖像。而不是完整的圖像正在上傳,我要上傳僅裁剪圖像...裁剪後的圖像,並將其保存到mysqldatabse

public class imageuploadtest extends AppCompatActivity implements View.OnClickListener { 
private ImageView Imageview; 
EditText Name; 
private Button Upload, Capture,camera; 
private final int IMG_REQUEST = 1; 
public Bitmap bitmap; 
ImageView imageView; 
Button buttonCamera, buttonGallery, submit; 
File file; 
Uri uri; 
EditText date; 
Intent CamIntent, GalIntent, CropIntent; 
public static final int RequestPermissionCode = 1; 
DisplayMetrics displayMetrics; 
int width, height; 
String server_url = "http://10.0.0.01/adcube/imageupload.php"; 

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.imageupload); 
    Imageview = (ImageView) findViewById(R.id.imageView); 
    Upload = (Button) findViewById(R.id.upload); 
    Capture = (Button) findViewById(R.id.capture); 
    Name = (EditText) findViewById(R.id.name); 
    camera= (Button) findViewById(R.id.camera); 
    Upload.setOnClickListener(this); 
    Capture.setOnClickListener(this); 
    camera.setOnClickListener(this); 
} 

@Override 
public void onClick(View view) { 
    switch (view.getId()) { 
     case R.id.upload: 
      selectimage(); 
      break; 
     case R.id.capture: 

      uploadImage(); 
      break; 
     case R.id.camera: 
      ClickImageFromCamera(); 
    } 
} 

public void ClickImageFromCamera() { 

    CamIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

    file = new File(Environment.getExternalStorageDirectory(), 
      "file" + String.valueOf(System.currentTimeMillis()) + ".jpg"); 
    uri = Uri.fromFile(file); 

    CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri); 

    CamIntent.putExtra("return-data", true); 

    startActivityForResult(CamIntent, 0); 

} 
private void selectimage() { 
    Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(intent, IMG_REQUEST); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == 0 && resultCode == RESULT_OK) { 
     ImageCropFunction(); 

    } else if (requestCode == 2) { 

     if (data != null) { 

      uri = data.getData(); 

      ImageCropFunction(); 
     } 
    } else if (requestCode == 1) { 

     if (data != null) { 

ImageCropFunction(); 
       try { 
       bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri); 
       Bundle bundle = data.getExtras(); 

       Bitmap bitmap =bundle.getParcelable("data"); 
       Imageview.setImageBitmap(bitmap); 
       Imageview.setVisibility(View.VISIBLE); 
       Name.setVisibility(View.VISIBLE); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 



private void uploadImage() { 
    StringRequest stringrequest = new StringRequest(Request.Method.POST, server_url, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      try { 
       JSONObject jsonObject = new JSONObject(response); 
       String Response = jsonObject.getString("cropIntent"); 
       Toast.makeText(imageuploadtest.this, Response, Toast.LENGTH_SHORT).show(); 
       Imageview.setImageResource(0); 
       Imageview.setVisibility(View.GONE); 
       Name.setText(""); 
       Name.setVisibility(View.GONE); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      Toast.makeText(imageuploadtest.this, "successful", Toast.LENGTH_SHORT).show(); 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Toast.makeText(imageuploadtest.this, error.toString(), Toast.LENGTH_SHORT).show(); 
     } 
    }) { 
     @Override 
     protected Map<String, String> getParams() throws AuthFailureError { 
      Map<String, String> parms = new HashMap<>(); 
      parms.put("name", Name.getText().toString().trim()); 
      parms.put("image", imageToString(bitmap)); 

      return parms; 
     } 
    }; 
    Mysingleton.getInstance(imageuploadtest.this).addToRequest(stringrequest); 
} 
public void ImageCropFunction() { 

    // Image Crop Code 
    try { 
     CropIntent = new Intent("com.android.camera.action.CROP"); 

     CropIntent.setDataAndType(uri, "image/*"); 

     CropIntent.putExtra("crop", "true"); 
     CropIntent.putExtra("outputX", 180); 
     CropIntent.putExtra("outputY", 180); 
     CropIntent.putExtra("aspectX", 3); 
     CropIntent.putExtra("aspectY", 4); 

     CropIntent.putExtra("scaleUpIfNeeded", true); 
     CropIntent.putExtra("return-data", true); 

     startActivityForResult(CropIntent, 1); 

    } catch (ActivityNotFoundException e) { 

    } 
} 


//cconvert image to string for database 

public static String imageToString(Bitmap BitmapData) { 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    BitmapData.compress(Bitmap.CompressFormat.JPEG, 100, bos); 
    byte[] byte_arr = bos.toByteArray(); 
    String encoded = Base64.encodeToString(byte_arr, Base64.DEFAULT); //appendLog(file); 
    return encoded; 
    } 
} 

回答

0

onActivityResult應該是這樣的

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
super.onActivityResult(requestCode, resultCode, data); 
if (requestCode == 0 && resultCode == RESULT_OK) { 
    ImageCropFunction(); 

} else if (requestCode == 2) { 

    if (data != null) { 

     uri = data.getData(); 

     ImageCropFunction(); 
    } 
} else if (requestCode == 1) { 

    if (data != null) { 
ImageCropFunction(); 
try { 

      bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri); 
      Bundle bundle = data.getExtras(); 

      bitmap =bundle.getParcelable("data"); 
      Imageview.setImageBitmap(bitmap); 
      Imageview.setVisibility(View.VISIBLE); 
      Name.setVisibility(View.VISIBLE); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

我只是刪除一個額外的位圖

Bitmap bitmap =bundle.getParcelable("data"); 

應該是這樣而已,

bitmap =bundle.getParcelable("data"); 
+0

感謝它的工作原理... –

相關問題