简体   繁体   中英

Move Images from Gallery to Server in android

我在android中有一个用于捕获图像的应用程序,然后将它们保存到Emualtor gallery Fine。但是当我从手机捕获它们时,我必须将图库中的所有照片移动到服务器上,它们应该自动上传到服务器并从图库中删除,即要移动图像到服务器。请告诉我如何从图库中选取所有图像,然后将它们移至服务器。

Invoking the Camera

String fileName = String.valueOf(System.currentTimeMillis()) + ".jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(cameraIntent, 1234);

Processing the Image

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 1234) {
String filepath = getPathfromUri(mCapturedImageURI);
//Now you have the file path. upload it to server.
//uploadtoserver(filepath);
//Now delete it after uploading 
new File(filepath).delete();
}
}

Method to convert Uri to actual path

public String getPathfromUri(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
startManagingCursor(cursor);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path= cursor.getString(column_index);
cursor.close();
return path;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM