android上传图片到亚马逊存储与图像路径

时间:2014-03-10 05:53:27

标签: android image amazon-web-services

我想使用它的文件路径上传图片。关于将图像上传到亚马逊,我所遵循的大部分教程都有选择图像选项(图片选择来自galary)。但我需要的是:

但我想跳过这一步。因为我知道图像路径和名称。假设我在设备的目录中有一个图像。当用户点击上传照片按钮时,它将开始上传。我想跳过选择图像选项。但是如何使用图像路径将图像上传到亚马逊存储而不是选择图像(Intent image/*)?

所以简而言之,我只是想直接使用文件路径而不是选择图像选项来上传亚马逊中的图像。

任何帮助将不胜感激。提前谢谢。

编辑:

这是我的活动:

public class SubmitActivity extends Activity {

Button submit;
ImageView thumbnailimage;
private AmazonS3Client s3Client = new AmazonS3Client(
        new BasicAWSCredentials(Constants.ACCESS_KEY_ID,
                Constants.SECRET_KEY));

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // getActionBar().setDisplayShowTitleEnabled(false);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    s3Client.setRegion(Region.getRegion(Regions.US_WEST_2));
    setContentView(R.layout.submit);

    submit = (Button) findViewById(R.id.buttonsubmit);
    submit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Uri selectedImage = Uri.parse(Environment
                    .getExternalStorageDirectory().toString()
                    + File.separator + "cubicasa.jpg");
            new S3PutObjectTask().execute(selectedImage);
        }
    });
    thumbnailimage = (ImageView) findViewById(R.id.thumbimg);

    byte[] imageData = null;

    try {

        final int THUMBNAIL_SIZE = 150;
        // InputStream is=getAssets().open("apple-android-battle.jpg");
        FileInputStream fis = new FileInputStream(Environment
                .getExternalStorageDirectory().toString()
                + File.separator
                + "cubicasa.jpg");
        Bitmap imageBitmap = BitmapFactory.decodeStream(fis);

        Float width = new Float(imageBitmap.getWidth());
        Float height = new Float(imageBitmap.getHeight());
        Float ratio = width / height;
        imageBitmap = Bitmap.createScaledBitmap(imageBitmap,
                (int) (THUMBNAIL_SIZE * ratio), THUMBNAIL_SIZE, false);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        imageData = baos.toByteArray();
        thumbnailimage.setImageBitmap(imageBitmap);
    } catch (Exception ex) {

    }

}

private class S3PutObjectTask extends AsyncTask<Uri, Void, S3TaskResult> {

    ProgressDialog dialog;

    protected void onPreExecute() {
        dialog = new ProgressDialog(SubmitActivity.this);
        dialog.setMessage(SubmitActivity.this.getString(R.string.uploading));
        dialog.setCancelable(false);
        dialog.show();
    }

    protected S3TaskResult doInBackground(Uri... uris) {

        if (uris == null || uris.length != 1) {
            return null;
        }

        // The file location of the image selected.
        Uri selectedImage = uris[0];

        ContentResolver resolver = getContentResolver();
        String fileSizeColumn[] = { OpenableColumns.SIZE };

        Cursor cursor = resolver.query(selectedImage, fileSizeColumn, null,
                null, null);

        cursor.moveToFirst();

        int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
        // If the size is unknown, the value stored is null. But since an
        // int can't be
        // null in java, the behavior is implementation-specific, which is
        // just a fancy
        // term for "unpredictable". So as a rule, check if it's null before
        // assigning
        // to an int. This will happen often: The storage API allows for
        // remote
        // files, whose size might not be locally known.
        String size = null;
        if (!cursor.isNull(sizeIndex)) {
            // Technically the column stores an int, but cursor.getString
            // will do the
            // conversion automatically.
            size = cursor.getString(sizeIndex);
        }

        cursor.close();

        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentType(resolver.getType(selectedImage));
        if (size != null) {
            metadata.setContentLength(Long.parseLong(size));
        }

        S3TaskResult result = new S3TaskResult();

        // Put the image data into S3.
        try {
            s3Client.createBucket(Constants.getPictureBucket());

            PutObjectRequest por = new PutObjectRequest(
                    Constants.getPictureBucket(), Constants.PICTURE_NAME,
                    resolver.openInputStream(selectedImage), metadata);
            s3Client.putObject(por);
        } catch (Exception exception) {

            result.setErrorMessage(exception.getMessage());
        }

        return result;
    }

    protected void onPostExecute(S3TaskResult result) {

        dialog.dismiss();

        if (result.getErrorMessage() != null) {

            displayErrorAlert(
                    SubmitActivity.this
                            .getString(R.string.upload_failure_title),
                    result.getErrorMessage());
        }
    }
}

protected void displayErrorAlert(String title, String message) {

    AlertDialog.Builder confirm = new AlertDialog.Builder(this);
    confirm.setTitle(title);
    confirm.setMessage(message);

    confirm.setNegativeButton(
            SubmitActivity.this.getString(R.string.ok),
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {

                    SubmitActivity.this.finish();
                }
            });

    confirm.show().show();
}

private class S3TaskResult {
    String errorMessage = null;
    Uri uri = null;

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public Uri getUri() {
        return uri;
    }

    public void setUri(Uri uri) {
        this.uri = uri;
    }
}

}

这是我的日志cat错误:

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个:

Uri uriImg =Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ "/Images/img1.jpg");  

    PutObjectRequest por = new PutObjectRequest( Constants.getPictureBucket(),      

    Constants.PICTURE_NAME, new java.io.File( uriImg) );  
    s3Client.putObject( por );  

我希望这有帮助: