Android java需要一些时间,直到应用程序从相机返回

时间:2015-11-20 14:20:32

标签: android android-intent android-activity parse-platform

我有onActivityResult功能(捕获图片后)并且功能在相机意图中运行。也就是说我没有看到ProgressDialog因为它在相机意图上运行并且加载了很多时间。 我怎么能首先从相机意图返回应用程序,然后运行onActivityResult函数?

Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            File f = new File(Environment.getExternalStorageDirectory(), "POST_IMAGE.jpg");
                            chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                            imageToUploadUri = Uri.fromFile(f);
                            startActivityForResult(chooserIntent, 1220);

功能:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1220 && resultCode == RESULT_OK)
    {


        final ProgressDialog mDialog = new ProgressDialog(PageAndExercise.this);
        mDialog.setMessage(getString(R.string.loading));
        mDialog.setCancelable(false);
        mDialog.show();

        Uri selectedImage = imageToUploadUri;
        getContentResolver().notifyChange(selectedImage, null);
        Bitmap reducedSizeBitmap = getBitmap(imageToUploadUri.getPath());
        if(reducedSizeBitmap != null) {



            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            reducedSizeBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            //  bookimage.setImageBitmap(photo);
            byte[] image = stream.toByteArray();
            ParseFile file = new ParseFile("photo.jpeg", image);
            file.saveInBackground();


            ParseObject toaccept = new ParseObject("Answers");

            toaccept.put("Picture", file);


            try {
               toaccept.save();
} catch (ParseException e1) {
                    e1.printStackTrace();
                }

        } else {
            Toast.makeText(getApplicationContext(),getString(R.string.error),Toast.LENGTH_LONG).show();
        }

    }
}

1 个答案:

答案 0 :(得分:0)

mDialog.show()之后的所有内容移动到后台线程中。不要在主应用程序线程上进行磁盘I / O,数据解析等。

然后,将mDialog.show()替换为DialogFragment以管理您的ProgressDialog,以便正确处理配置更改(例如,屏幕旋转)。