要获取图像,您可以执行以下操作:
// TODO check sdcard is available
File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File photoPath = new File(directory, "temp.jpg");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
final Bitmap bitmap = BitmapFactory.decodeFile(photoPath.getAbsolutePath(), options);
要发送到您的服务器,您可以使用 okHttp 客户端或您使用哪种协议与您的服务器通信。
更新:在清单中你应该指出这个权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
如果您在 android 6 及更高版本上运行代码,您应该处理运行时权限 (WRITE_EXTERNAL_STORAGE):
API 23中的Android权限正常权限和危险权限列表?
// oncreate method or whereever you want to call your code
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
STATUS_CODE);
} else {
// TODO run your code
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case STATUS_CODE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// TODO run your code
} else {
// TODO show warning
}