使用parse.com将捕获的图像保存为文件

时间:2015-04-06 10:51:25

标签: android android-studio parse-platform capture image-capture

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    formObject = new ParseObject("FormObject");
    onCaptureImage();
    setContentView(R.layout.activity_form);
    // parse key and
    Parse.initialize(this, "O28eS5ZhQ1d1aZWrUl2UBWNkLEzTKUb09gDOlJsJ", "fVX7ganN5obFevOOl4fwPYfGOAcxmnABBI7b9J4d");
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    //Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
   // startActivityForResult(intent, 0);
}

public void onCaptureImage() {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
            .format(new Date());
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, "IMG_" + timeStamp + ".jpg");
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    // start the Camera Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            if (fileUri != null) {

                    addform();

            }

        } else if (resultCode == RESULT_CANCELED) {
            finish();
        } else {
            // Image capture failed, advise user
        }
    }
}

public void addform() {
    setContentView(R.layout.activity_form);
    formpic = (ImageView) findViewById(R.id.imageView);
    send = (Button) findViewById(R.id.sendEmail);
    // compress the image and save it in the DB
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    b = ShrinkBitmap(fileUri.getPath(), 300, 300);
    formpic.setImageBitmap(b);
    b.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] image = baos.toByteArray();
    file = new ParseFile("test.jpg", image);
    try {
        file.save();
        imageURL = file.getUrl();
        dlgProgress.setMessage("جاري رفع صورة البلاغ");
        dlgProgress.show();
    } catch (com.parse.ParseException e1) {
        e1.printStackTrace();
    }
    // Hide the progress dialog
    dlgProgress.dismiss();
    send.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            note = (EditText) findViewById(R.id.editText2);
            final String CityName = note.getText()
                    .toString();

            // check the empty fields
            if (note.getText().length() == 0) {
                note.setError("الرجاء تعبئة الحقل");
            } else {
                SharedPreferences pref = getApplicationContext()
                        .getSharedPreferences("MyPref", 0);
                String s = pref.getString("name", "");
                formObject.put("note", note);
                formObject.put("formPic", file);
                try {
                    formObject.save();
                } catch (com.parse.ParseException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }
            }
            formObject.saveInBackground(new SaveCallback() {
                @Override
                public void done(com.parse.ParseException e) {
                    if (e == null) {
                        // success
                        Toast.makeText(
                                formActivity.this,
                                "تم إرسال البريد الإلكتروني",
                                Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        Toast.makeText(formActivity.this,
                                "فشل في إضافة البلاغ", Toast.LENGTH_SHORT)
                                .show();
                    }
                }
            });
        }
    });


}

Bitmap ShrinkBitmap(String file, int width, int height) {

    BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
    bmpFactoryOptions.inJustDecodeBounds = true;
    Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

    int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight
            / (float) height);
    int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth
            / (float) width);

    if (heightRatio > 1 || widthRatio > 1) {
        if (heightRatio > widthRatio) {
            bmpFactoryOptions.inSampleSize = heightRatio;
        } else {
            bmpFactoryOptions.inSampleSize = widthRatio;
        }
    }

    bmpFactoryOptions.inJustDecodeBounds = false;
    bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
    return bitmap;
}

/**
 * Create a file Uri for saving an image to specific folder
 */
private static Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type) {
    if (Environment.getExternalStorageState() != null) {
        File mediaStorageDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "TechSupport");

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                .format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + ".jpg");
        } else {
            return null;
        }

        return mediaFile;
    }

    return null;
}

大家好,我对此代码有问题,此代码使用相机执行捕获的图像并将保存好的图像作为文件压缩后传输到字节数组,当用户点击发送按钮时使用解析保存所有解析对象.comm我不知道这个代码有哪些错误,请我帮忙解决这个问题

非常感谢你帮助我

0 个答案:

没有答案