android使用图像路径将图像上传到亚马逊存储

标签 android image amazon-web-services

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

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

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

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

已编辑:

这是我的 Activity :

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;
    }
}

}

这是我的日志猫错误:

enter image description here

enter image description here

最佳答案

试试这个:

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 );  

希望对您有所帮助:

关于android使用图像路径将图像上传到亚马逊存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22292957/

相关文章:

amazon-web-services - 使用 aws cli 将带有元数据的项目上传到 Amazon S3 存储桶

包含 'grayed out' 项的 Android 列表

java - 不要使用 TabLayout 将 Activity 保留在缓存中

android - Android 的通用图像加载器可以处理来自 sqlite 数据库的图像吗?

javascript - 延迟 JQuery;线路完成时进行协调?

linux - AWS 实例元数据(Linux 控制台)

django - AWS 上的 nginx 和 Gunicorn 出现 504 超时

java - 创建 AlertDialog 时代码中断。我想我有上下文错误......?

jquery - 为什么此 HTML 会导致页面变为空白

image - 从 .swf 查看器中提取图像?