android - 无法解决结果的 Activity

标签 android android-activity android-imagebutton

我正在创建一个动态按钮来从 android 中捕获照片。动态按钮位于与主要 Activity 不同的类中。我的 startActivityForResult 出现了 Can't resolve 错误,这里是 my code

我将不胜感激任何帮助。谢谢。

最佳答案

尝试这种方式,希望这能帮助您解决问题。

请记住自定义类不能覆盖 onActivityResult() 只有 Activity 扩展类覆盖 onActivityResult() 所以你必须在你的 Activity 中覆盖 onActivityResult() 并像下面这样回调你的自定义类

public class MainActivity extends Activity {

    private JsonGuiImageView jsonGuiImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        jsonGuiImageView = new JsonGuiImageView(this);
        setContentView(jsonGuiImageView);
    }

    @Override
    protected void  onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode == jsonGuiImageView.CAMERA_REQUEST && resultCode == Activity.RESULT_OK){
            jsonGuiImageView.setPhoto();
        }
    }
}

public class JsonGuiImageView extends LinearLayout {
    private ImageView imageView;
    private ImageButton button;
    private Intent cameraIntent;
    private Bitmap photo;
    private Context context;
    public static int CAMERA_REQUEST = 1777;
    private String imagePath;

    public JsonGuiImageView(Context context){
        super(context);
        this.context = context;
        this.setOrientation(VERTICAL);
        this.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        this.setGravity(Gravity.CENTER);
        button = new ImageButton(this.context);
        button.setLayoutParams(new ViewGroup.LayoutParams(60, 60));
        button.setImageResource(R.drawable.ic_launcher);
        button.setMaxHeight(60);
        button.setMinimumHeight(60);
        button.setMaxWidth(60);
        button.setMinimumWidth(60);
        button.setOnClickListener(AddImage);
        this.addView(button);
    }

    public JsonGuiImageView(Context context, AttributeSet attributeSet){
        super(context, attributeSet);
    }

    OnClickListener AddImage = new OnClickListener() {
        @Override
        public void onClick(View view) {
            File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
            imagePath = file.getAbsolutePath();
            cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(file));
            if (cameraIntent.resolveActivity(((Activity)context).getPackageManager()) != null) {
                ((Activity)context).startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        }
    };

    public void setPhoto(){
        photo = decodeSampledBitmapFromFile(imagePath, 480, 640);
        imageView = new ImageView(getContext());
        imageView.setMaxHeight(60);
        imageView.setMinimumHeight(60);
        imageView.setMaxWidth(60);
        imageView.setMinimumWidth(60);
        imageView.setImageBitmap(photo);
        this.addView(imageView);
    }

    public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight)
    {
        Bitmap decode, rotatedBitmap = null;

        //First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        // Calculate inSampleSize, Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        int inSampleSize = 8;

        if (height > reqHeight)
        {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        }
        int expectedWidth = width / inSampleSize;

        if (expectedWidth > reqWidth)
        {
            //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }

        options.inSampleSize = inSampleSize;

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inTempStorage = new byte[16 * 1024];

        try{
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

            int rotation = 0;
            switch (orientation){
                case ExifInterface.ORIENTATION_ROTATE_90 :
                    rotation = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180 :
                    rotation = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270 :
                    rotation = 270;
                    break;
                default: break;
            }

            Matrix matrix = new Matrix();
            matrix.postRotate(rotation);

            decode = BitmapFactory.decodeFile(path, options);
            rotatedBitmap = Bitmap.createBitmap(decode, 0, 0, decode.getWidth(), decode.getHeight(), matrix, true);
        } catch (IOException e){
            e.printStackTrace();
        }

        return rotatedBitmap;
    }
}

关于android - 无法解决结果的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26337069/

相关文章:

android - 能否仅在开发/调试期间使 Activity 类可启动?

安卓工作室项目 : bottom of UI is cut off

android - 为什么android Activity 操作栏标题是黑色的?

android - Android 应用程序是否有等同于 http session 的内容?

android - 无法在 Android Studio 2.2 中创建发布 apk

java - 想学Android开发,不懂Java

android - 按下时更改 ImageButton 背景颜色

android - 单击时,部分波纹效果不会在图像按钮上消失

java - 从 Web 服务解析 Sax

python - 安卓发布者 : "Track names in request path and request body must match."