Cloudinary - 缺少必需参数

时间:2016-03-14 14:01:23

标签: android-studio cloudinary

我遇到某种错误,经过Cloudinary Docs。,以及其他几个帖子和其他网站,加上读取堆栈跟踪我假设我的语法将图像上传到服务器不是应该如何虽然我从文档中的示例中清楚地理解,但我无法根据stacktrace中的错误应用正确的方法。

我的堆栈跟踪给了我这个错误: 引起:java.lang.RuntimeException:缺少必需参数 - 文件

我的代码如下:

public class Upload extends AppCompatActivity implements View.OnClickListener {


public static final int RESULT_LOAD_IMAGE = 1;
ImageView imgToUpload;
Button buttonUploadSubmit;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.upload);
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    imgToUpload = (ImageView) findViewById(R.id.imgToUpload);
    buttonUploadSubmit = (Button) findViewById(R.id.ButtonUploadSubmit);

    imgToUpload.setOnClickListener(this);
    buttonUploadSubmit.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.imgToUpload:
            Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
            break;

        case R.id.ButtonUploadSubmit:
            UploadTask uploadTask = new UploadTask();
            uploadTask.execute();

            break;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Uri selectedVideo = data.getData();
    imgToUpload.setImageURI(selectedVideo);
    Toast.makeText(getApplicationContext(), selectedVideo.toString(), Toast.LENGTH_LONG).show();
    Toast.makeText(getApplicationContext(), imgToUpload.toString(), Toast.LENGTH_LONG).show();
}



class UploadTask extends AsyncTask<String, Void, Void> {

    ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(Upload.this);
        progressDialog.setTitle("Upload in Progress ...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(100);
        progressDialog.setProgress(0);
        progressDialog.show();
    }

    @Override
    protected Void doInBackground(String... params) {

       Map config = new HashMap();
                       config.put("cloud_name", "xxxxxxx");
                       config.put("api_key", "xxxxxxxxxxxxxx");
                       config.put("api_secret", "xxxxxxxxxxx");
                       Cloudinary cloudinary = new Cloudinary(config);
                       try {
                           cloudinary.uploader().upload(imgToUpload, ObjectUtils.asMap("resource_type", "auto"));
                       } catch (IOException e) {
                           e.printStackTrace();
                       }


        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }


}

} 我猜imgToUpload,在cloudaniry上传方法中不被接受:

cloudinary.uploader().upload( imgToUpload,
                ObjectUtils.asMap("resource_type", "auto"));

因为它不是“文件”而且根据stacktrace这就是我所缺少的,所以如果有人能够确认这一点我会很感激,并请指导我使用这种方法的正确语法,或者创建一个方法使用ImageView中所选图片的文件,并在Cloudinary上传方法中包含该文件?

堆栈跟踪:

  03-15 14:32:14.260 17683-20341/net.we4x4.we4x4 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
 Process: net.we4x4.we4x4, PID: 17683
                                                             java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                 at android.os.AsyncTask$3.done(AsyncTask.java:300)
                                                                 at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                                                                 at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                                                                 at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                                                                 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                 at java.lang.Thread.run(Thread.java:841)
                                                              Caused by: java.lang.RuntimeException: Missing required parameter - file
                                                                 at com.cloudinary.android.UploaderStrategy.callApi(UploaderStrategy.java:106)
                                                                 at com.cloudinary.Uploader.callApi(Uploader.java:22)
                                                                 at com.cloudinary.Uploader.upload(Uploader.java:55)
                                                                 at net.we4x4.we4x4.Upload$UploadTask.doInBackground(Upload.java:100)
                                                                 at net.we4x4.we4x4.Upload$UploadTask.doInBackground(Upload.java:77)
                                                                 at android.os.AsyncTask$2.call(AsyncTask.java:288)
                                                                 at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                                                                 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                                 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                                 at java.lang.Thread.run(Thread.java:841) 

所以我能够上传apicture,提供以下路径: /storage/emulated/0/Pictures/Screenshots/Screenshot_2016-03-09-15-40-13.png

但是通过手动将其写入Upload方法,我手动从手机获取代码,只是查看图片的详细信息,因此我现在的问题是如何在ImageView中获取显示图片的这条路径?

此外,progressBar刚刚开始,在0并没有改变,我不得不检查网站/服务器,看到图片上传了......我不确定是什么导致进度条不能正常/ freez !?

我很感激任何指导。

2 个答案:

答案 0 :(得分:2)

您还可以将ImageView结果转换为Base64并将字符串发送到Cloudinary。

否则,您可以提供https://github.com/cloudinary/cloudinary_java/blob/master/cloudinary-android/src/main/java/com/cloudinary/android/UploaderStrategy.java#L65-L77

中定义的任何一种类型

答案 1 :(得分:0)

嗯,问题解决了,出于某种原因,从一个API到另一个API获取文件图像的真实路径是一种棘手的问题,不确定原因,或者是否有另一种简单的方法可以做到这一点我还没有发现,但根据几个帖子,如: android get real path by Uri.getPath()

考虑到API,您可以通过转换帖子中给出的方法从Uri获取实际图像路径。

由于Cloudinary要求上传文件,因此提供设备上文件的实际路径,或其文档中提到的其他方法。必须通过获取文件的实际路径来解决这个障碍 http://cloudinary.com/documentation/upload_images#api_example_1

以下是在Android Studio中将文件上传到Cloudinary的代码;

    @Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.imgToUpload:
            Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);

            break;
        case R.id.ButtonUploadSubmit:
            UploadTask uploadTask = new UploadTask();
            uploadTask.execute(String.valueOf(vidToUpload));

            break;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {
        Uri selectedImgPath = data.getData();
        RealImgPath = Uri.parse(getPath(selectedImgPath));
        Toast.makeText(getApplicationContext(), " " + RealImgPath, Toast.LENGTH_LONG).show();

    }

}

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


class UploadTask extends AsyncTask<String, Void, Void> {

    ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(Upload.this);
        progressDialog.setTitle("Upload in Progress ...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(100);
        progressDialog.setProgress(0);
        progressDialog.show();
    }

    @Override
    protected Void doInBackground(String... params) {

        Map config = new HashMap();
        config.put("cloud_name", "xxxxxx");
        config.put("api_key", "xxxxxxxxxxxxxxx");
        config.put("api_secret", "xxxxxxxxxxxxxxxxxxx");
        Cloudinary cloudinary = new Cloudinary(config);
        try {
            cloudinary.uploader().upload(""+RealImgPath, ObjectUtils.asMap("resource_type", "auto"));
        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        progressDialog.dismiss();
        super.onPostExecute(aVoid);
    }
}

我希望这可能对某些人有用,并且为了实现这一目标,我们必须经历几天的发现探险,因为Cloudinary Docs。有点缺乏一些特定的信息,Stakcoverflow在Cloudinary特定帖子上缺少一些帖子,考虑到我在学习的过程中也延长了这个简单的问题。

需要注意的是,上面的代码仍然不完美,因为进度对话框仍然无法正常运行(弹出和freez - 没有进展),并且意图库中所选的图片将不会显示在ImageView上应该是,现在需要解决这个问题。