作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在尝试使用改造将图像上传到服务器时收到以下错误,API 接收 header 授权 token ,_method =“put”和 image =“imageName.jpg”作为参数,其他所有内容都是可选的,任何帮助将不胜感激。
java.io.FileNotFoundException: /storage/emulated/0/Download/space-wallpaper-21.jpg: open failed: EACCES (Permission denied)
userImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("*/*");
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imageDecodableString = cursor.getString(columnIndex);
cursor.close();
StringBuilder stringBuilder = new StringBuilder(AUTHORIZATION);
sharedPreferences = this.getSharedPreferences(getResources().getString(R.string.token), 0);
stringBuilder.append(sharedPreferences.getString(getResources().getString(R.string.token), ""));
methodMap.put("_method", "PUT");
File file = new File(imageDecodableString);
RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), requestBody);
//RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "image");
Call<UserInfo> uploadImage = new RestClient(CONSTS.BASE_URL_ADDRESS_ACCOUNT_CREATION).getUserSignUpClient()
.postImage(stringBuilder.toString(), methodMap, body);
uploadImage.enqueue(new Callback<UserInfo>() {
@Override
public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
if (response.isSuccessful()) {
}
}
@Override
public void onFailure(Call<UserInfo> call, Throwable t) {
Toast.makeText(DetailActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
界面:
public interface UserSignUpClient {
@POST("account")
Call<AuthenticationAccountCreationResponse> createAccount(@Body UserSignUp userSignUp);
@Multipart
@POST("account")
Call<UserInfo> postImage(@Header("Authorization") String headerValue, @PartMap Map<String, String> map, @Part MultipartBody.Part image);
}
改造构建器类:
public class RestClient {
private UserSignInClient userSignInClient;
private UserSignUpClient userSignUpClient;
private UserInfoClient userInfoClient;
private UserImageUploadClient userImageUploadClient;
public RestClient(String baseUrlLink){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrlLink)
.addConverterFactory(GsonConverterFactory.create()).build();
if (baseUrlLink.equals(CONSTS.BASE_URL_ADDRESS_TOKEN)){
userSignInClient = retrofit.create(UserSignInClient.class);
} else if (baseUrlLink.equals(CONSTS.BASE_URL_ADDRESS_ACCOUNT_CREATION)) {
userSignUpClient = retrofit.create(UserSignUpClient.class);
} else if (baseUrlLink.equals(CONSTS.BASE_URL_ADDRESS_USER_INFO)){
userInfoClient = retrofit.create(UserInfoClient.class);
}
}
public UserSignInClient getUserSignInClient() {
return userSignInClient;
}
public UserSignUpClient getUserSignUpClient() {
return userSignUpClient;
}
public UserInfoClient getUserInfoClient() {
return userInfoClient;
}
}
最佳答案
适用于 Android 6.0 Marshmallow 以下版本:
在 list 中添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
对于 Marshmallow 及以上设备:
public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback{
private static final int REQUEST_WRITE_PERMISSION = 786;
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_WRITE_PERMISSION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openFilePicker();//do your job
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermission();
}
private void requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION);
} else {
openFilePicker();//do your job
}
}
}
有关更多信息,请参阅下面的链接
Android 6.0 Marshmallow. Cannot write to SD Card
Read and Write permission for storage and gallery usage for marshmallow
注意:不要忘记在 list 文件中添加权限
关于java - Android 在使用 Retrofit 多部分上传图像时遇到问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43713787/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!