0

嗨,Android 开发新手。当方向改变时需要帮助维护 imageView 中的图像。此时,当我拍照或上传照片时,它会很好地上传,直到我切换方向。我尝试从其他帖子中搜索不太了解。我想这与保存实例有关。有人可以帮我解决这个问题。

    img1.setOnClickListener(new OnClickListener() {

    public void onClick(View v){
    CharSequence[] names = { "From Gallery", "From Camera" };
    new AlertDialog.Builder(context)
            .setTitle("Select an option for updating your Profile Picture")
          .setItems(names, new DialogInterface.OnClickListener() {

            @Override
                public void onClick(DialogInterface dialog, int pos) {
                    // TODO Auto-generated method stub
                    if (pos == 0) {

                        Intent i = new Intent(
                                Intent.ACTION_PICK,
                                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);    


                        startActivityForResult(i, GET_GAL_IMG);


                    } else {


                       Intent i = new Intent(
                                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);                    startActivityForResult(i, GET_CAM_IMG);

                    }}}


            )
            .setNegativeButton(android.R.string.cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                        }
                    }).create().show();
    }
    });}



   @Override
public void onActivityResult(int requestCode, int resultCode,  Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    switch (requestCode) {

    case 2://Camera
        Log.d("take","pic");
        if (resultCode == -1) {
            String encodedImageString = null;      
            Uri selectimage=intent.getData();               
            Log.d("take","picture");
            ImageView img1=(ImageView)findViewById(R.id.img1);


          //Changing URI to Bitmap
            Bitmap bmp = null;
            try {
                bmp =   BitmapFactory.decodeStream(getContentResolver().openInputStream(selectimage));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            //Reducing Memory
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            if (bmp.compress(Bitmap.CompressFormat.JPEG, 50, baos)) {
                byte[] image = baos.toByteArray();
                encodedImageString = Base64.encodeToString(image,
                        Base64.DEFAULT);
            } else {
                System.out.println("Compression returned false");
                Log.d("Compress", "Compression returned false");
            }

           //setting Imageview as the bitmap so could send it to the canvas 
            img1.setImageBitmap(bmp);


            }



        break;
    case 1://Selecting from Gallery
        Log.d("view","pic");
        if (resultCode == -1) {

            String encodedImageString = null;
             Uri selectimage = intent.getData();
             String selectedImagepath = getPath(selectimage);
             ImageView img1=(ImageView)findViewById(R.id.img1);

       //Changing URI to Bitmap
        Bitmap bmp = null;
        try {
            bmp =  BitmapFactory.decodeStream(getContentResolver().openInputStream(selectimage));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

         //Reducing Memory
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        if (bmp.compress(Bitmap.CompressFormat.JPEG, 50, baos)) {
            byte[] image = baos.toByteArray();
            encodedImageString = Base64.encodeToString(image,
                    Base64.DEFAULT);
        } else {
            System.out.println("Compression returned false");
            Log.d("Compress", "Compression returned false");
        }


                Log.d("view","picture");
                //setting Imageview as the bitmap so could send it to the canvas
                img1.setImageBitmap(bmp);
        }
        break;
    }
}
4

2 回答 2

0

要获得快速解决方案,请在文件中将以下内容添加到您的相关活动标签中。AndroidManifest.xml

android:configChanges="orientation|screenSize"

当方向改变时,它将重用(不会破坏和重新创建活动)屏幕内容。

于 2013-08-23T14:25:38.003 回答
0

@Waqas 解决方案是一种快速解决方案,由于不同的原因,这是一个错误的决定。

简而言之:你正在失去你的形象,因为当设备旋转时,当前的 Activity 实例丢失并创建了一个新实例。每次要销毁活动时都会调用方法 onSaveInstanceState(Bundle outState) (您可以覆盖)。您可以将不同类型的类型放入 Bundle 参数。当您的 Activity 的另一个实例被创建时(例如,在旋转屏幕之后),您可以使用相同的包(您将之前的状态放入其中)来获取之前保存的状态。也可以在 onRestoreInstanceState 中检索相同的状态。

在您的情况下,会出现另一个问题:捆绑并不意味着存储图形。要解决这个问题,您可以简单地将图形数据放入内存,然后从那里读取。在这种情况下,您可以将文件名和路径放入包中。您可以参考http://developer.android.com/reference/android/app/Activity.html以获取一些更具体的信息。

于 2013-08-23T14:42:38.883 回答