我想使用多部分将通过相机选择的图像发布到服务器。我在这里得到例外
java.io.FileNotFoundException: /external/images/media/26: open failed: ENOENT (No such file or directory)
代码如下:
//On clicking Gallery
layout_gallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loadImageFromGallery();
}
});
public void loadImageFromGallery() {
popupWindow.dismiss();
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
String root = Environment.getExternalStorageDirectory().toString();
Log.e("root", root);
String directory_path = root + "/cam_intent/";
File myDir = new File(directory_path);
myDir.mkdirs();
File file = new File(myDir, "MyPhoto.jpg");
fileUri = Uri.fromFile(file);
Log.e("Uri of File", String.valueOf(fileUri));
galleryIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
launchUploadActivity(true);
} else if (requestCode == RESULT_LOAD_IMG) {
fileUri = data.getData();
//File file = new File(getRealPathFromURI(NewGroupActivity.this,uri));
//fileUri = Uri.fromFile(file);
Log.e("FileUriImageGallery", String.valueOf(fileUri));
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// Get the cursor
Cursor cursor = getContentResolver().query(fileUri,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageView imgView = (ImageView) findViewById(R.id.groupPic);
// Set the Image in ImageView after decoding the String
imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
}
} else {
Toast.makeText(this, "You haven't picked Image",Toast.LENGTH_LONG).show();
}
}
图片显示在ImageView
中,但不会发送到服务器,因为它会被FileNotFoundException
抛出。
private String uploadFile() {
Log.e("upload File", "called");
String responseString = null;
HttpClient httpclient = new DefaultHttpClient();
Log.e("URL", url_create_new_group);
HttpPost httppost = new HttpPost(url_create_new_group);
try {
AndroidMultiPartEntity entity = new AndroidMultiPartEntity(new AndroidMultiPartEntity.ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
// Adding file data to http body
if (val == 0) {
if (fileUri != null) {
Log.e("val = ", String.valueOf(val));
File sourceFile = new File(fileUri.getPath());
if (sourceFile != null) {
Log.e("SoureFile", String.valueOf(sourceFile));
photosize = 0;
entity.addPart("image", new FileBody(sourceFile));
} else {
photosize = 1;
Toast.makeText(NewGroupActivity.this, "Please Upload Your Image", Toast.LENGTH_LONG).show();
}
}
} else {
File sourceFile = new File(picturePath);
if (sourceFile != null) {
photosize = 0;
entity.addPart("image", new FileBody(sourceFile));
} else {
photosize = 1;
Toast.makeText(NewGroupActivity.this, "Please Upload Your Image", Toast.LENGTH_LONG).show();
}
}
totalSize = entity.getContentLength();
Log.e("Total Size", String.valueOf(totalSize));
httppost.setEntity(entity);
// Making server call
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
// Server response
responseString = EntityUtils.toString(r_entity);
} else {
responseString = "Error occurred! Http Status Code: " + statusCode;
}
} catch (ClientProtocolException e) {
responseString = e.toString();
} catch (IOException e) {
responseString = e.toString();
}
return responseString;
}
请检查代码。
更新了工作代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
launchUploadActivity(true);
} else if (requestCode == RESULT_LOAD_IMG) {
Uri uri = data.getData();
//File file = new File(getRealPathFromURI(NewGroupActivity.this,uri));
//fileUri = Uri.fromFile(file);
Log.e("FileUriImageGallery", String.valueOf(uri));
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// Get the cursor
Cursor cursor = getContentResolver().query(uri,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageView imgView = (ImageView) findViewById(R.id.groupPic);
// Set the Image in ImageView after decoding the String
imgView.setImageBitmap(BitmapFactory
.decodeFile(imgDecodableString));
}
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
}