您好工作在聊天應用程序,現在我想集成共享音頻圖像PDF視頻從我的應用程序。我正在使用此代碼,但無法正常工作。有沒有人有任何想法如何發送這些文件使用凌空或改造。聽到是我試過的。如何上傳文件,如視頻音頻PDF格式文本文件圖像從Android到PHP服務器
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
而這是我onActivityResult方法。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
ContentResolver cr = this.getContentResolver();
mime = cr.getType(uri);
Toast.makeText(ChatActivity.this,mime,Toast.LENGTH_LONG).show();
Log.d(TAG, "File Uri: " + uri.toString()+" "+mime);
// Get the path
if(mime.equalsIgnoreCase("image/jpeg")){
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
uploadImage();
} catch (IOException e) {
e.printStackTrace();
}
}else{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis;
try {
fis = new FileInputStream(new File(uri.getPath()));
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
} catch (Exception e) {
e.printStackTrace();
}
filebytes = baos.toByteArray();
uploadImage();
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
這是我的上傳方法。
private void uploadImage(){
StringRequest stringRequest = new StringRequest(Request.Method.POST,
UPLOAD_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
//Disimissing the progress dialog
loading.dismiss();
//Showing toast message of the response
try {
JSONObject jsonObject = new JSONObject(s);
String message = jsonObject.getString("result");
sendfileMessage(message);
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("image path",s);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//Dismissing the progress dialog
loading.dismiss();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
//Converting Bitmap to String
Map<String,String> params = new Hashtable<String, String>();
//Adding parameters
params.put("mkey", "R09fQ2hhdC0z");
if(mime.equalsIgnoreCase("image/jpeg")){
String image = getStringImage(bitmap);
params.put("ch_img", image);
}else{
String image = new String(filebytes);
params.put("ch_img", image);
}
//returning parameters
Log.d("imagedemo",params.toString());
return params;
}
};
//Creating a Request Queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
[上傳的可能的複製doc,pdf,xls等,從android應用程序到php服務器](https://stackoverflow.com/questions/33820723/upload-doc-pdf-xls-etc -from-android-application-to-php-server) – SripadRaj
@SripadRas是否適用於視頻和音頻 –
我想它應該可以工作。 – SripadRaj