基本上我想做的就是从图库中选择一张图片然后将相同的图片上传到我的Cloudinary帐户,我看过Cloudinary的文档,但我真的不明白它是如何工作因此不知道如何在我的代码中实现它。
这是我获取信息的链接,以防有人需要它... https://github.com/cloudinary/cloudinary_java/tree/master/cloudinary-android
这就是我到目前为止......
upload_image.class
public class edit_profile_activity extends AppCompatActivity
{
private static int RESULT_LOAD_IMG = 1;
String imgDecodableString;
public void loadImagefromGallery(View view)
{
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
try
{
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data)
{
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageView imgView = (ImageView) findViewById(R.id.circleImageView );
imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
} else {
Toast.makeText(this, "You haven't picked Image",Toast.LENGTH_LONG).show();
}
} catch (Exception e)
{
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG) .show();
}
}
}
任何帮助将不胜感激
答案 0 :(得分:0)
你可以使用Cloudinary的这个简单上传功能:
Cloudinary cloudinary = new Cloudinary(Constants.CLOUDINARY_URL);
try {
FileInputStream is = new FileInputStream(new File(filePath));
Uploader uploader = cloudinary.uploader();
Map map = uploader.upload(is, new HashMap());
return map;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
进一步说明如下:The order is like this: Take picture->save to file->upload to cloudinary
答案 1 :(得分:0)
我就是这样做的:
图库意图返回一个contentprovider,我使用它并将图像保存到我的本地(app / local)目录。
我们这样说:
String local_uri = new SaveImageAsyncTask(cropImageView,
uri).execute().get();
然后我用这个uri将图像上传到cloudinary:
private void checkInternetAndUploadImage(final String uri) {
if (Validator.isOnline(this)) {
new UploadAsyncTask().execute(Uri.parse(uri));
}
}
我的doInBackground有这段代码:
Map profilePicture = ObjectUtils.asMap("public_id", PreferencesManager.getInstance().getUserId() +
"/" + PreferencesManager.getInstance().getUserName() + "_PROFILE_PICTURE");
profilePicture.put("uploadPrefix", "http://api.cloudinary.com");
profilePicture.put("timestamp", System.currentTimeMillis() / 1000);
Map imageUploadResult = CloudinaryManager.getInstance(Activity_ImageViewer.this).uploader().
upload(new File(voids[0].getPath()),
profilePicture);
if (imageUploadResult.containsKey("url") /*&& backUploadResult.containsKey("url")*/) {
PreferencesManager.getInstance().setUSER_PROFILE_PICTURE_URL(String.valueOf(imageUploadResult.get("url")));
//PreferencesManager.getInstance().setUserBackAssetUrl(String.valueOf(backUploadResult.get("url")));
result = true;
}