AWS 无法计算 MD5 哈希值Android

标签: Java Android
发布时间: 2017/3/19 4:30:18
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我上传的图像文件到 S3 通过 AWS java SDK,但我正在下面上传时出错,过我 credentials 和我斗,他们都很好

Unable to calculate MD5 hash: /data/user/0/competent.groove.feetport/files/data/user/0/competent.groove.feetport/app_imageDir/IMG_20161122_073058.jpg: open failed: ENOENT (No such file or directory) 

我在捕捉的图像从 camera 和文件保存在手机里,这里是我的代码

 @Override
        public void onPictureTaken(CameraView cameraView, final byte[] data) {
            Log.d(TAG, "onPictureTaken " + data.length);
            //Toast.makeText(cameraView.getContext(), R.string.picture_taken, Toast.LENGTH_SHORT) .show();
            getBackgroundHandler().post(new Runnable() {
                @Override
                public void run() {
                    // This demo app saves the taken picture to a constant file.
                    // $ adb pull /sdcard/Android/data/com.google.android.cameraview.demo/files/Pictures/picture.jpg
                    ///storage/7A52-13E8/Android/data/competent.groove.feetport/files/Pictures/picture.jpg
                    //make a new picture file
                    File pictureFile = getOutputMediaFile();

                    if (pictureFile == null) {
                        Toast toast = Toast.makeText(getActivity(), "Picture Not saved", Toast.LENGTH_LONG);
                        toast.show();
                        return;
                    }
                    try {
                        //write the file
                        FileOutputStream fos = new FileOutputStream(pictureFile);
                        fos.write(data);
                        fos.close();
                        Toast toast = Toast.makeText(getActivity(), "Picture saved: " + pictureFile.getName(), Toast.LENGTH_LONG);
                        toast.show();

                        Log.e("-pictureFile--length-----",""+pictureFile.length());
                        Log.e("---pictureFile-----"+pictureFile.getName(),""+pictureFile.getAbsolutePath());
                        Log.e("-fileName--",""+fileName);

                        editor = sharedPref.edit();
                        editor.putString(Constants.PIC_PATH,""+fileName);
                        editor.commit();


                    } catch (FileNotFoundException e) {
                    } catch (IOException e) {
                    }



                }
            });
        }

    };




String fileName="";
    //make picture and save to a folder
    private  File getOutputMediaFile() {
        //make a new file directory inside the "sdcard" folder
       /* File mediaStorageDir = new File("/sdcard/", "JCG Camera");

        //if this "JCGCamera folder does not exist
        if (!mediaStorageDir.exists()) {
            //if you cannot make this folder return
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }
        }*/
        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();

        ContextWrapper cw = new ContextWrapper(getActivity());
        File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
        Log.e("---directory-----", "" + directory);

        //take the current timeStamp
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        ///data/user/0/competent.groove.biometric/files/IMG_20160803_181740.jpg
        //and make a media file:

        fileName = directory + File.separator + "IMG_" + timeStamp + ".jpg";
        mediaFile = new File(directory + File.separator + "IMG_" + timeStamp + ".jpg");

        Log.e("---extStorageDirectory-----", "" + extStorageDirectory);
        //Log.e("---mediaFile-----",""+mediaFile.length());
        //Log.e("---mediaFile-----"+mediaFile.getName(),""+mediaFile.getAbsolutePath());

        return mediaFile;

    }

用于上传文件我 aws 代码是 UploadAws 扩展 AsyncTask {的公共类

private  int total, percentage = 0;
Context context;
String s3_server_path = "";
ObscuredSharedPreferences shrdpref;
ObscuredSharedPreferences.Editor editor;

String res = ""; 


public UploadAws(Context context){
    Log.e(""+getClass(), "UploadAws constructor called");
    this.context = context;
    shrdpref = new ObscuredSharedPreferences(context, context.getSharedPreferences(Constants.PREFERENCES, Context.MODE_PRIVATE));

}

@Override
protected Object doInBackground(Object... params) {
    try {
        Log.d("" + getClass(), "UploadAws doInBackground called");
        File pictures = null;
        AmazonS3Client s3Client = null;
        TransferUtility tx = null;
        percentage = 0;

    try {                           
        s3_server_path = shrdpref.getString(Constants.PIC_PATH,"");

        File dir = context.getFilesDir();
        pictures = new File(dir, s3_server_path);
        s3Client = new AmazonS3Client(new BasicAWSCredentials(shrdpref.getString(Constants.AWS_Access_Key,""),shrdpref.getString(Constants.AWS_Secret_Key,"")));
        tx = new TransferUtility(s3Client,context);

    } catch (NullPointerException e) {
        e.printStackTrace();                
    }                 

        try {

            Log.e("------File length--------=", "" + pictures.length());
            Log.e("------File getName--------=", "" + pictures.getName());
            Log.e("------File getAbsolutePath--------=", "" + pictures.getAbsolutePath());
            s3Client.setEndpoint("s3.amazonaws.com");

            String Feetport_bucket = shrdpref.getString(Constants.fs_bucket, "");

            if (s3Client.doesBucketExist(Feetport_bucket)) {
                Log.e("Warn", "service called bucket exist");
            } else {
                s3Client.createBucket(Feetport_bucket);
                Log.e("FOS signature", "Bucket created");
            }

            int imageIndex = pictures.toString().lastIndexOf("/");
            String pictureString = pictures.toString().substring(imageIndex + 1, pictures.toString().length());

            String selfie_url = "FeetPort/" + shrdpref.getString(Constants.company_name, "") + "/attend/" + Utils.getCurrentDate() + "/" + pictureString;
            Log.d("UploadAws selfie_url called: ", "" + selfie_url);

            //https://feetport.s3.amazonaws.com/8632/17/20161122/mayank1_I_1624_065610_COL22.jpeg

            final TransferObserver observer = tx.upload(Feetport_bucket, pictures.getName(), pictures);

            observer.setTransferListener(new TransferListener() {
                @Override
                public void onStateChanged(int arg0, TransferState state) {
                    Log.e("onStateChanged", "on state changed " + state);
                }

                @Override
                public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
                    Log.e("onProgressChanged", "total bytes " + observer.getBytesTotal());
                    Log.e("onProgressChanged", "total bytes transfered " + observer.getBytesTransferred());
                    percentage = (int) (bytesCurrent / bytesTotal * 100);
                    Log.e("onProgressChanged", "total percentage " + percentage);
                }

                @Override
                public void onError(int arg0, Exception arg1) {
                    Log.e("onError=" + arg0, "on Error  " + arg1.toString());
                    percentage = 101;
                }
            });


            do {


            } while (percentage < 100);

            Log.e("percentage", "percentage  " + percentage);
            if (percentage == 100) {
                /**
                 * CGPL-17 9-5-2016 dynamic bucket and url.
                 */
                String S3_SERVER = "https://".concat(Feetport_bucket).concat(".s3.amazonaws.com/");
                String imageUrl = S3_SERVER + selfie_url;

                //Session.setselfie_url(shrdpref, imageUrl);

                editor = shrdpref.edit();
                editor.putString(Constants.PIC_PATH, "" + imageUrl);
                editor.commit();

                Log.e("Selfie url ", "imageUrl --- " + imageUrl);
        /*JsonParameters object = new JsonParameters(context);
        object.markedAttendance(callback);*/
            } else {
        /*JsonParameters object = new JsonParameters(context);
        object.markedAttendance(callback);*/
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    } catch (Exception exception) {
        exception.printStackTrace();

    }
    return null;
}

}

解决方法 1:

这并不涉及到 S3。

ENOENT (No such file or directory)是一个本地错误︰ 找不到文件。

非常密切看看 path 中的错误消息。 已经包括了 path 在这里,和添加一些换行符的可读性。 我否则没有编辑,和重要的是,没有不地不止一次粘贴任何在这里︰

/data/user/0
/competent.groove.feetport
/files/data/user/0
/competent.groove.feetport
/app_imageDir/IMG_20161122_073058.jpg

我不说,这错误的但是它当然看起来不对劲。 看起来,你想要上传的文件从 path 你不正确构造,与一些重复......的目录结构中并没有此目录,没有这样的文件。

赞助商