定义文件上传的接口:
ApiService.java/**
* @author Pratik Butani on 23/4/16.
*/
public interface ApiService {
/*
Retrofit get annotation with our URL
And our method that will return us the List of Contacts
*/
@Multipart
@POST("upload.php")
Call uploadImage(@Part MultipartBody.Part file);
}
Android Code:/**
* Upload Image Client Code
*/
private void uploadImage() {
/**
* Progressbar to Display if you need
*/
final ProgressDialog progressDialog;
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage(getString(R.string.string_title_upload_progressbar_));
progressDialog.show();
//Create Upload Server Client
ApiService service = RetroClient.getApiService();
//File creating from selected URL
File file = new File(imagePath);
// create RequestBody instance from file
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body =
MultipartBody.Part.createFormData("uploaded_file", file.getName(), requestFile);
Call resultCall = service.uploadImage(body);
resultCall.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
progressDialog.dismiss();
// Response Success or Fail
if (response.isSuccessful()) {
if (response.body().getResult().equals("success"))
Snackbar.make(parentView, R.string.string_upload_success, Snackbar.LENGTH_LONG).show();
else
Snackbar.make(parentView, R.string.string_upload_fail, Snackbar.LENGTH_LONG).show();
} else {
Snackbar.make(parentView, R.string.string_upload_fail, Snackbar.LENGTH_LONG).show();
}
/**
* Update Views
*/
imagePath = "";
textView.setVisibility(View.VISIBLE);
imageView.setVisibility(View.INVISIBLE);
}
@Override
public void onFailure(Call call, Throwable t) {
progressDialog.dismiss();
}
});
}
服务端PHP代码:<?php
$file_path = "";
$var = $_POST['result'];
$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
$result = array("result" => "success", "value" => $var);
} else{
$result = array("result" => "error");
}
echo json_encode($result);
?>
完整代码:
RetroClient.java/**
* @author Pratik Butani
*/
public class RetroClient {
/**
* Upload URL of your folder with php file name...
* You will find this file in php_upload folder in this project
* You can copy that folder and paste in your htdocs folder...
*/
private static final String ROOT_URL = "http://192.168.0.105/php_upload/";
public RetroClient() {
}
/**
* Get Retro Client
*
* @return JSON Object
*/
private static Retrofit getRetroClient() {
return new Retrofit.Builder()
.baseUrl(ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static ApiService getApiService() {
return getRetroClient().create(ApiService.class);
}
}
Activity Code:
MainActivity.javapublic class MainActivity extends AppCompatActivity {
/**
* Permission List
*/
private static final String[] PERMISSIONS_READ_STORAGE = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE};
/**
* Context Variables
*/
Context mContext;
/**
* Views
*/
View parentView;
ImageView imageView;
TextView textView;
/**
* Image path to send
*/
String imagePath;
/**
*
*/
PermissionsChecker checker;
/**
*
*/
Toolbar toolbar;
/**
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
/**
* Parent View
*/
parentView = findViewById(R.id.parentView);
/**
* Permission Checker Initialized
*/
checker = new PermissionsChecker(this);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
textView = (TextView) findViewById(R.id.textView);
imageView = (ImageView) findViewById(R.id.imageView);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
assert fab != null;
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!TextUtils.isEmpty(imagePath)) {
/**
* Uploading AsyncTask
*/
if (InternetConnection.checkConnection(mContext)) {
/******************Retrofit***************/
uploadImage();
} else {
Snackbar.make(parentView, R.string.string_internet_connection_warning, Snackbar.LENGTH_INDEFINITE).show();
}
} else {
Snackbar.make(parentView, R.string.string_message_to_attach_file, Snackbar.LENGTH_INDEFINITE).show();
}
}
});
}
/**
* Upload Image Client Code
*/
private void uploadImage() {
/**
* Progressbar to Display if you need
*/
final ProgressDialog progressDialog;
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage(getString(R.string.string_title_upload_progressbar_));
progressDialog.show();
//Create Upload Server Client
ApiService service = RetroClient.getApiService();
//File creating from selected URL
File file = new File(imagePath);
// create RequestBody instance from file
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body =
MultipartBody.Part.createFormData("uploaded_file", file.getName(), requestFile);
Call resultCall = service.uploadImage(body);
resultCall.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
progressDialog.dismiss();
// Response Success or Fail
if (response.isSuccessful()) {
if (response.body().getResult().equals("success"))
Snackbar.make(parentView, R.string.string_upload_success, Snackbar.LENGTH_LONG).show();
else
Snackbar.make(parentView, R.string.string_upload_fail, Snackbar.LENGTH_LONG).show();
} else {
Snackbar.make(parentView, R.string.string_upload_fail, Snackbar.LENGTH_LONG).show();
}
/**
* Update Views
*/
imagePath = "";
textView.setVisibility(View.VISIBLE);
imageView.setVisibility(View.INVISIBLE);
}
@Override
public void onFailure(Call call, Throwable t) {
progressDialog.dismiss();
}
});
}
/**
* Showing Image Picker
*/
public void showImagePopup(View view) {
if (checker.lacksPermissions(PERMISSIONS_READ_STORAGE)) {
startPermissionsActivity(PERMISSIONS_READ_STORAGE);
} else {
// File System.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_PICK);
// Chooser of file system options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, getString(R.string.string_choose_image));
startActivityForResult(chooserIntent, 1010);
}
}
/***
* OnResult of Image Picked
*
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == 1010) {
if (data == null) {
Snackbar.make(parentView, R.string.string_unable_to_pick_image, Snackbar.LENGTH_INDEFINITE).show();
return;
}
Uri selectedImageUri = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImageUri, filePathColumn, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imagePath = cursor.getString(columnIndex);
Picasso.with(mContext).load(new File(imagePath))
.into(imageView);
Snackbar.make(parentView, R.string.string_reselect, Snackbar.LENGTH_LONG).show();
cursor.close();
textView.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE);
} else {
textView.setVisibility(View.VISIBLE);
imageView.setVisibility(View.GONE);
Snackbar.make(parentView, R.string.string_unable_to_load_image, Snackbar.LENGTH_LONG).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void startPermissionsActivity(String[] permission) {
PermissionsActivity.startActivityForResult(this, 0, permission);
}
}
权限检查 (Marshmallow user):
PermissionsActivity.javapublic class PermissionsActivity extends AppCompatActivity {
public static final int PERMISSIONS_GRANTED = 0;
public static final int PERMISSIONS_DENIED = 1;
private static final int PERMISSION_REQUEST_CODE = 0;
private static final String EXTRA_PERMISSIONS = "com.androidbuts.uploadimage.EXTRA_PERMISSIONS";
private static final String PACKAGE_URL_SCHEME = "package:";
private PermissionsChecker checker;
private boolean requiresCheck;
public static void startActivityForResult(Activity activity, int requestCode, String... permissions) {
Intent intent = new Intent(activity, PermissionsActivity.class);
intent.putExtra(EXTRA_PERMISSIONS, permissions);
ActivityCompat.startActivityForResult(activity, intent, requestCode, null);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent() == null || !getIntent().hasExtra(EXTRA_PERMISSIONS)) {
throw new RuntimeException("This Activity needs to be launched using the static startActivityForResult() method.");
}
//setContentView(R.layout.activity_permissions);
checker = new PermissionsChecker(this);
requiresCheck = true;
}
@Override
protected void onResume() {
super.onResume();
if (requiresCheck) {
String[] permissions = getPermissions();
if (checker.lacksPermissions(permissions)) {
requestPermissions(permissions);
} else {
allPermissionsGranted();
}
} else {
requiresCheck = true;
}
}
private String[] getPermissions() {
return getIntent().getStringArrayExtra(EXTRA_PERMISSIONS);
}
private void requestPermissions(String... permissions) {
ActivityCompat.requestPermissions(this, permissions, PERMISSION_REQUEST_CODE);
}
private void allPermissionsGranted() {
setResult(PERMISSIONS_GRANTED);
finish();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE && hasAllPermissionsGranted(grantResults)) {
requiresCheck = true;
allPermissionsGranted();
} else {
requiresCheck = false;
showMissingPermissionDialog();
}
}
private boolean hasAllPermissionsGranted(@NonNull int[] grantResults) {
for (int grantResult : grantResults) {
if (grantResult == PackageManager.PERMISSION_DENIED) {
return false;
}
}
return true;
}
private void showMissingPermissionDialog() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(PermissionsActivity.this);
dialogBuilder.setTitle(R.string.help);
dialogBuilder.setMessage(R.string.string_help_text);
dialogBuilder.setNegativeButton(R.string.quit, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setResult(PERMISSIONS_DENIED);
finish();
}
});
dialogBuilder.setPositiveButton(R.string.settings, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startAppSettings();
}
});
dialogBuilder.show();
}
private void startAppSettings() {
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse(PACKAGE_URL_SCHEME + getPackageName()));
startActivity(intent);
}
}
Permission Checker:
PermissionsChecker.javapublic class PermissionsChecker {
private final Context context;
public PermissionsChecker(Context context) {
this.context = context;
}
public boolean lacksPermissions(String... permissions) {
for (String permission : permissions) {
if (lacksPermission(permission)) {
return true;
}
}
return false;
}
private boolean lacksPermission(String permission) {
return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_DENIED;
}
}