需要从相机或图库上传图像到服务器,使用改造(自我分配任务进行学习)



试图学习Android,所以我想使用改造将图像从相机/画廊上传到服务器,从互联网上获取活动代码并尝试转换为片段。它创建了一个应用程序,但是当我在选择图像后从图库中进行选择时,它会出现错误。

社区建议后,我更新了我的代码,上传文件时只发生一个错误

Response{protocol=http/1.1, code=404, message=Not Found, url=.........}

我的整页代码如下

package com.example.tc.ui.home;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import com.afollestad.materialdialogs.MaterialDialog;
import com.bumptech.glide.Glide;
import com.example.tc.BuildConfig;
import com.example.tc.R;
import com.example.tc.networking.ApiConfig;
import com.example.tc.networking.AppConfig;
import com.example.tc.networking.ServerResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Logger;
import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;

public class HomeFragment extends Fragment implements View.OnClickListener {
ImageView imageView;
Button pickImage, upload;
private static final int REQUEST_TAKE_PHOTO = 0;
private static final int REQUEST_PICK_PHOTO = 2;
private Uri mMediaUri;
private static final int CAMERA_PIC_REQUEST = 1111;
//    private static final String TAG = ImageActivity.class.getSimpleName();
private static final String TAG = HomeFragment.class.getSimpleName();
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;
private Uri fileUri;
private String mediaPath;
private Button btnCapturePicture;
private String mImageFileLocation = "";
public static final String IMAGE_DIRECTORY_NAME = "Android File Upload";
ProgressDialog pDialog;
private String postPath;
private HomeViewModel homeViewModel;
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
//Suggested by community @Siddharth
imageView = root.findViewById(R.id.preview);
pickImage = root.findViewById(R.id.pickImage);
upload = root.findViewById(R.id.upload);
pickImage.setOnClickListener(this);
upload.setOnClickListener(this);
initDialog();

return root;
}
@Override
public void onClick(final View v) {
switch (v.getId()) {
case R.id.pickImage:
new MaterialDialog.Builder(getActivity())
.title(R.string.uploadImages)
.items(R.array.uploadImages)
.itemsIds(R.array.itemIds)
.itemsCallback(new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
switch (which) {
case 0:
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, REQUEST_PICK_PHOTO);
break;
case 1:
captureImage();
break;
case 2:
imageView.setImageResource(R.drawable.ic_launcher_background);
break;
}
}
})
.show();
break;
case R.id.upload:
uploadFile();
break;
}
}
private boolean isExternalStorageAvailable() {
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
else {
return false;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_TAKE_PHOTO || requestCode == REQUEST_PICK_PHOTO) {
if (data != null) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
assert cursor != null;
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
mediaPath = cursor.getString(columnIndex);
//           Toast.makeText(getActivity(), "Hemant File Uploaded Successfully...", Toast.LENGTH_LONG).show();
// Set the Image in ImageView for Previewing the Media
imageView.setImageBitmap(BitmapFactory.decodeFile(mediaPath));
cursor.close();

postPath = mediaPath;
}

}else if (requestCode == CAMERA_PIC_REQUEST){
if (Build.VERSION.SDK_INT > 21) {
Glide.with(getActivity())
.load(mImageFileLocation)
.into(imageView);
postPath = mImageFileLocation;
}else{
Glide.with(getActivity()).load(fileUri).into(imageView);
postPath = fileUri.getPath();
}
}
}
else if (resultCode != RESULT_CANCELED) {
Toast.makeText(getActivity(), "Sorry, there was an error!", Toast.LENGTH_LONG).show();
}
}

/**
* Checking device has camera hardware or not
* */
private boolean isDeviceSupportCamera() {
if (getActivity().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
protected void initDialog() {
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage(getString(R.string.msg_loading));
pDialog.setCancelable(true);
}

protected void showpDialog() {
if (!pDialog.isShowing()) pDialog.show();
}
protected void hidepDialog() {
if (pDialog.isShowing()) pDialog.dismiss();
}

/**
* Launching camera app to capture image
*/
private void captureImage() {
if (Build.VERSION.SDK_INT > 21) { //use this if Lollipop_Mr1 (API 22) or above
Intent callCameraApplicationIntent = new Intent();
callCameraApplicationIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
// We give some instruction to the intent to save the image
File photoFile = null;
try {
// If the createImageFile will be successful, the photo file will have the address of the file
photoFile = createImageFile();
// Here we call the function that will try to catch the exception made by the throw function
} catch (IOException e) {
Logger.getAnonymousLogger().info("Exception error in generating the file");
e.printStackTrace();
}
// Here we add an extra file to the intent to put the address on to. For this purpose we use the FileProvider, declared in the AndroidManifest.
Uri outputUri = FileProvider.getUriForFile(
getActivity(),
BuildConfig.APPLICATION_ID + ".provider",
photoFile);
callCameraApplicationIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
// The following is a new line with a trying attempt
callCameraApplicationIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
Logger.getAnonymousLogger().info("Calling the camera App by intent");
// The following strings calls the camera app and wait for his file in return.
startActivityForResult(callCameraApplicationIntent, CAMERA_PIC_REQUEST);
} else {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}

}
File createImageFile() throws IOException {
Logger.getAnonymousLogger().info("Generating the image - method started");
// Here we create a "non-collision file name", alternatively said, "an unique filename" using the "timeStamp" functionality
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmSS").format(new Date());
String imageFileName = "IMAGE_" + timeStamp;
// Here we specify the environment location and the exact path where we want to save the so-created file
File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + "/photo_saving_app");
Logger.getAnonymousLogger().info("Storage directory set");
// Then we create the storage directory if does not exists
if (!storageDirectory.exists()) storageDirectory.mkdir();
// Here we create the file using a prefix, a suffix and a directory
File image = new File(storageDirectory, imageFileName + ".jpg");
// File image = File.createTempFile(imageFileName, ".jpg", storageDirectory);
// Here the location is saved into the string mImageFileLocation
Logger.getAnonymousLogger().info("File name and path set");
mImageFileLocation = image.getAbsolutePath();
// fileUri = Uri.parse(mImageFileLocation);
// The file is returned to the previous intent across the camera application
return image;
}

/**
* Here we store the file url as it will be null after returning from camera
* app
*/
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save file url in bundle as it will be null on screen orientation
// changes
outState.putParcelable("file_uri", fileUri);
}
@Override
public void onViewStateRestored (Bundle savedInstanceState) {
super.onViewStateRestored( savedInstanceState );
// fileUri = savedInstanceState.getParcelable("file_uri");
}
/**
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// get the file url
fileUri = savedInstanceState.getParcelable("file_uri");
}
**/
/**
* Receiving activity result method will be called after closing the camera
* */
/**
* ------------ Helper Methods ----------------------
* */
/**
* Creating file uri to store image/video
*/
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
/**
* returning image / video
*/
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(TAG, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + ".jpg");
}  else {
return null;
}
return mediaFile;
}
// Uploading Image/Video
private void uploadFile() {
if (postPath == null || postPath.equals("")) {
Toast.makeText(getActivity(), "please select an image ", Toast.LENGTH_LONG).show();
return;
} else {
showpDialog();
// Map is used to multipart the file using okhttp3.RequestBody
Map<String, RequestBody> map = new HashMap<>();
File file = new File(postPath);
// Parsing any Media type file
RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
map.put("file"; filename="" + file.getName() + """, requestBody);
ApiConfig getResponse = AppConfig.getRetrofit().create(ApiConfig.class);
Call<ServerResponse> call = getResponse.upload("token", map);
call.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
if (response.isSuccessful()){
if (response.body() != null){
hidepDialog();
ServerResponse serverResponse = response.body();
//                            Toast.makeText(getApplicationContext(), serverResponse.getMessage(), Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(), " uploading image", Toast.LENGTH_SHORT).show();
}
}else {
hidepDialog();
Toast.makeText(getActivity(), "problem uploading image", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
hidepDialog();
Log.v("Response gotten is", t.getMessage());
}
});
}
}
}

布局如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
android:id="@+id/preview"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="10dp"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher_background" />
<Button
android:id="@+id/pickImage"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="-200dp"
android:layout_marginTop="500dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="100dp"
android:text="Pick Image" />
<Button
android:id="@+id/upload"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="-200dp"
android:layout_marginTop="300dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="200dp"
android:text="Upload" />
</LinearLayout>

暗示如下

implementation 'com.android.support:support-v4:28.1.0'
implementation 'com.github.bumptech.glide:glide:3.8.0'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.2.0'

testImplementation 'junit:junit:4.12'
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})

ApiConfig 是

public interface ApiConfig {
@Multipart
@POST("images/upload_image.php")
Call<ServerResponse> upload(
@Header("Authorization") String authorization,
@PartMap Map<String, RequestBody> map
);
}

服务器响应

public class ServerResponse {
// variable name should be same as in the json response from php
@SerializedName("success")
boolean success;
@SerializedName("message")
String message;
public String getMessage() {
return message;
}
public boolean getSuccess() {
return success;
}
}

应用配置


public class AppConfig {
//    public static String BASE_URL = "http://unitypuzzlegame.com/";
public static String BASE_URL = "http://192.168.43.215/Retrofit/ImageUploadApi/";

public static Retrofit getRetrofit() {
return new Retrofit.Builder()
.baseUrl(AppConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
}

清单权限(社区@Mike M建议后(

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>

我在res->xml->provider_path.xml中添加了另一个文件

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

这是我的代码,用于在 android 中使用 Retrofit 2 上传图像:

Bitmap bitmap = null;
FileOutputStream fOut;
//imagePath is the Uri of my image
File f = new File(URI.create(imagePath.toString()));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
try {
bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, 
options);
Bitmap out = Bitmap.createScaledBitmap(bitmap, 300, 300, false);
fOut = new FileOutputStream(f);
out.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
bitmap.recycle();
out.recycle();
} catch (IOException e) {
e.printStackTrace();
return;
}

RequestBody fileReqBody = RequestBody.create(null, f);
MultipartBody.Part part = MultipartBody.Part.createFormData("image", 
f.getName(), fileReqBody);

Map<String, String> headers = new HashMap<>();
//token is the string that i use for Authorization
headers.put("Authorization", token);
Call<BaseModel> call = Api.getInstance().getAPI().uploadImage(headers, 
part);

这是改造接口:

@Multipart
@POST("user/image")
Call<BaseModel> uploadImage(@HeaderMap Map<String, String> token, @Part 
MultipartBody.Part image);

我使用了太多方法,但直到这个解决方案才奏效,希望有所帮助。

在评论中更新我

我的代码中有三个错误。 1. 我已经在类级别声明了 ImageView imageView,为什么在 onCreateView(( 中再次声明和初始化它。

  1. 我没有在清单文件中提到。

  2. Api 链接在服务器端有一个拼写错误,所以它正在响应 404

我可以在社区评论的帮助下修复前两个,我自己修复第三个。

对于第一个问题,我在 onCreateView(( 中删除了 ImageView 声明

//Suggested by community @Siddharth
imageView = root.findViewById(R.id.preview);
pickImage = root.findViewById(R.id.pickImage);
upload = root.findViewById(R.id.upload);

对于我在清单文件中添加的第二个

@MikeM建议。

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>

和瓦拉:)

最新更新