该应用最初会执行以下操作
该应用程序的目的是自动上传数据,而我能够使用Volley成功使用BroadcaseReciever进行文本输入,但是由于我不知道如何从Android上传图像的过程而停滞不前。
我已经进行了一些合理的研究,但是从Sqlite中存储的imagepath上传图像到服务器时没有解决方案。 以下是存储在sqlite中的图像路径示例。 (我是Android的初学者)
/storage/emulated/0/Pictures/1547728376728.jpg
下面是我正在使用的代码。 PS:我相信getimagepath方法的目的是1.找到图像2.将其转换为字节3.传递给Volley。我被卡在1.上,并且代码可能是错误的。
package com.example.narendra.e5.activities.Connectivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Base64;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.narendra.e5.activities.database.DatabaseHelper;
import com.example.narendra.e5.activities.others.AppSingleton;
import com.example.narendra.e5.activities.others.MySingleton;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class NetworkMonitor extends BroadcastReceiver {
Context context;
public Bitmap bitmap;
@Override
public void onReceive(final Context context, Intent intent) {
if (checkNetworkConnection(context)){
Toast.makeText(context, "Hello hello", Toast.LENGTH_LONG).show();
final DatabaseHelper db=new DatabaseHelper(context);
SQLiteDatabase database=db.getWritableDatabase();
Cursor cursor=db.getdataIncoming(database);
while (cursor.moveToNext()){
final String incoming=cursor.getString(cursor.getColumnIndex(DatabaseHelper.INCOMINGTIME));
Toast.makeText(context, incoming, Toast.LENGTH_LONG).show();
final String INVENID=cursor.getString(cursor.getColumnIndex(DatabaseHelper.INVENIDAPP));
final String imageurl=cursor.getString(cursor.getColumnIndex(DatabaseHelper.SLIP_IMAGE));
// get image path from database and convert it into bitmap
// Uri uri = Uri.parse(imageurl);
// bitmap=MediaStore.Images.Media.getBitmap();
StringRequest stringRequest=new StringRequest(Request.Method.POST, DatabaseHelper.SERVER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject=new JSONObject(response);
String getResponse=jsonObject.getString("response");
if (getResponse.equals("OK")){
Toast.makeText(context, "Response ok", Toast.LENGTH_LONG).show();
//db.deleteOfflineSaveOutgoingDetails(INVENID);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, "Response error", Toast.LENGTH_LONG).show();
}
}
){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params=new HashMap<>();
params.put("name",incoming);
// params.put("image",getimagepath(imageurl));
return params;
}
}
;
MySingleton.getInstance(context).addToRequestQue(stringRequest);
}
}
}
public boolean checkNetworkConnection(Context context){
ConnectivityManager connectivityManager= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
return (networkInfo!=null && networkInfo.isConnected());
}
public String getimagepath(Intent imageurl){
// File imagepath=new File(String.valueOf(imageurl));
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
Uri imagePath = imageurl.getData();
bitmap=BitmapFactory.decodeFile(String.valueOf(imagePath));
bitmap.compress(Bitmap.CompressFormat.JPEG,60,byteArrayOutputStream);
byte[] imgByte=byteArrayOutputStream.toByteArray();
return Base64.encodeToString(imgByte,Base64.DEFAULT);
}
}
答案 0 :(得分:0)
对于POST
,您可以像这样使用JsonObjectRequest
。无需覆盖getParams
方法。
HashMap<String,String> maps=new HashMap<>();
maps.put("name",incoming);
maps.put("image",getimagepath(imageurl));
JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, YOUR_URL, new JSONObject(maps), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
jsonObjectRequest.setTag("TAG");
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(2,0f));
VolleySingleton.getInstance(context).addToRequestQueue(jsonObjectRequest);
答案 1 :(得分:0)
我通过更正方法public String getimagepath(Intent imageurl)
解决了该问题
对此:
public String getimagepath(String imageurl){
// File imagepath=new File(String.valueOf(imageurl));
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
//Uri imagePath = imageurl.getData();
if (imageurl !=null){
// Uri imagePath = Uri.fromFile(new File(imageurl));
bitmap=BitmapFactory.decodeFile(imageurl);
bitmap.compress(Bitmap.CompressFormat.JPEG,60,byteArrayOutputStream);
byte[] imgByte=byteArrayOutputStream.toByteArray();
return Base64.encodeToString(imgByte,Base64.DEFAULT);
}
else {
return null;
}
}