Java 如何在SharedReferences中保存缩放位图?

Java 如何在SharedReferences中保存缩放位图?,java,android,bitmap,Java,Android,Bitmap,我试图在SharedReferences中保存位图,但是当我试图加载位图时,我给出了一个“java.lang.OutOfMemoryError” 我想我正在保存未缩放的版本 这是我的缩放代码: public void decodeFile(String filePath) { // Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds =

我试图在SharedReferences中保存位图,但是当我试图加载位图时,我给出了一个“java.lang.OutOfMemoryError”

我想我正在保存未缩放的版本

这是我的缩放代码:

public void decodeFile(String filePath) {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 2048;

    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bmp = BitmapFactory.decodeFile(filePath, o2);

}
怎么了

编辑:

这是我的全部onActivityResult代码:

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

    if (requestCode == GALLERY_PICTURE) {
        if (resultCode == RESULT_OK) {


                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                if (cursor != null) {
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();


               decodeFile(filePath);
               img_logo.setImageBitmap(bmp);
               settings = getSharedPreferences("pref", 0);
               SharedPreferences.Editor prefsEditor = settings.edit();
                    prefsEditor.putString("photo1", filePath);
                    prefsEditor.commit();
                }


            } else {
                Toast.makeText(getApplicationContext(), "Cancelled",
                        Toast.LENGTH_SHORT).show();
            }
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getApplicationContext(), "Cancelled",
                    Toast.LENGTH_SHORT).show();
        }
     else if (requestCode == CAMERA_REQUEST) {
        if (resultCode == RESULT_OK) {
            String[] projection = { MediaStore.Images.Media.DATA}; 
            Cursor cursor = getContentResolver().query(mCapturedImageURI2, projection, null, null, null); 
            int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
            cursor.moveToFirst(); 
            String capturedImageFilePath = cursor.getString(column_index_data);
            Log.d("photos*******"," in camera take int  "+capturedImageFilePath);

            decodeFile(capturedImageFilePath);

            if(data != null)
            {
                img_logo.setImageBitmap(bmp);
                settings = getSharedPreferences("pref", 0);
                SharedPreferences.Editor prefsEditor = settings.edit();
                    prefsEditor.putString("photo1", capturedImageFilePath);
                    prefsEditor.commit();
            }
        }
}
}
Edit2:

我的整个活动代码:

public class MainActivity extends Activity {
ImageView img_logo;
protected static final int CAMERA_REQUEST = 0;
protected static final int GALLERY_PICTURE = 1;
Uri mCapturedImageURI2;
SharedPreferences settings;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    settings = getSharedPreferences("pref", 0);

    img_logo = (ImageView) findViewById(R.id.imageView1);
    Log.d("SHAREDimagezzzz", "**********SHAREDzzzzzzz suckes******");
    img_logo.setImageURI(Uri.parse(settings.getString("photo1", "android.resource://com.tiktak.babyalbum/" + R.drawable.ic_launcher)));
    Log.d("SHAREDimage", "**********SHARED suckes******");
    img_logo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            startDialog();
        }
    });
}
protected void startDialog() {
    AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
    myAlertDialog.setTitle("Upload Pictures Option");
    myAlertDialog.setMessage("How do you want to set your picture?");

    myAlertDialog.setPositiveButton("Gallery",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT);
                    galleryintent.setType("image/*");
                    galleryintent.putExtra("return-data", true);
                    startActivityForResult(galleryintent, GALLERY_PICTURE);
                }
            });

    myAlertDialog.setNegativeButton("Camera",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    ContentValues values = new ContentValues();  
                    values.put(MediaStore.Images.Media.TITLE, "fileName");  
                    mCapturedImageURI2 = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  

                    Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI2);  
                    startActivityForResult(cameraIntent, CAMERA_REQUEST);

                }
            });
    myAlertDialog.show();
}
Bitmap bmp;
public String decodeFile(String filePath) {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 2048;

    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bmp = BitmapFactory.decodeFile(filePath, o2);
    return filePath;

}

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

    if (requestCode == GALLERY_PICTURE) {
        if (resultCode == RESULT_OK) {

                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                if (cursor != null) {
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();

               String bb = decodeFile(filePath);
               img_logo.setImageBitmap(bmp);
               Log.d("galleryimage", "**********gallery suckes******");
               settings = getSharedPreferences("pref", 0);
               SharedPreferences.Editor prefsEditor = settings.edit();
                    prefsEditor.putString("photo1", bb);
                    prefsEditor.commit();
                }

            } else {
                Toast.makeText(getApplicationContext(), "Cancelled",
                        Toast.LENGTH_SHORT).show();
            }
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getApplicationContext(), "Cancelled",
                    Toast.LENGTH_SHORT).show();
        }
     else if (requestCode == CAMERA_REQUEST) {
        if (resultCode == RESULT_OK) {
            String[] projection = { MediaStore.Images.Media.DATA}; 
            Cursor cursor = getContentResolver().query(mCapturedImageURI2, projection, null, null, null); 
            int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
            cursor.moveToFirst(); 
            String capturedImageFilePath = cursor.getString(column_index_data);
            Log.d("photos*******"," in camera take int  "+capturedImageFilePath);

            String bbd = decodeFile(capturedImageFilePath);

            if(data != null)
            {
                img_logo.setImageBitmap(bmp);
                Log.d("cameraimage", "**********camera suckes******");
                settings = getSharedPreferences("pref", 0);
                SharedPreferences.Editor prefsEditor = settings.edit();
                    prefsEditor.putString("photo1", bbd);
                    prefsEditor.commit();
            }
        }
}
}
}
公共类MainActivity扩展活动{
ImageView img_徽标;
受保护的静态最终int摄像机\u请求=0;
受保护的静态最终int画廊图片=1;
Uri mCapturedImageURI2;
共享引用设置;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
设置=GetSharedReferences(“pref”,0);
img_徽标=(ImageView)findViewById(R.id.imageView1);
Log.d(“SharedMagezzz”,“************SHAREDzzzzzzz吸盘*******”;
img_logo.setImageURI(Uri.parse(settings.getString)(“photo1”,“android。resource://com.tiktak.babyalbum/“+R.drawable.ic_发射器”);
Log.d(“SHAREDimage”,即“**********共享的吸盘*******”);
img_logo.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图arg0){
startDialog();
}
});
}
受保护的void startDialog(){
AlertDialog.Builder myAlertDialog=新建AlertDialog.Builder(此);
myAlertDialog.setTitle(“上传图片选项”);
myAlertDialog.setMessage(“您想如何设置图片?”);
myAlertDialog.setPositiveButton(“Gallery”,
新建DialogInterface.OnClickListener(){
公共void onClick(对话框接口arg0,int arg1){
Intent gallerycontent=新的意图(Intent.ACTION\u GET\u CONTENT);
GalleryContent.setType(“image/*”);
GalleryContent.putExtra(“返回数据”,true);
startActivityForResult(画廊内容、画廊图片);
}
});
myAlertDialog.setNegativeButton(“摄像头”,
新建DialogInterface.OnClickListener(){
公共void onClick(对话框接口arg0,int arg1){
ContentValues=新的ContentValues();
value.put(MediaStore.Images.Media.TITLE,“文件名”);
mCapturedImageURI2=getContentResolver().insert(MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI,值);
Intent-cameraIntent=newintent(“android.media.action.IMAGE_-CAPTURE”);
cameraIntent.putExtra(MediaStore.EXTRA_输出,mCapturedImageURI2);
startActivityForResult(摄像机帐篷、摄像机请求);
}
});
myAlertDialog.show();
}
位图bmp;
公共字符串解码文件(字符串文件路径){
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码文件(文件路径,o);
//我们要扩展到的新尺寸
所需最终整型尺寸=2048;
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){
如果(宽度\u tmp<要求的\u尺寸和高度\u tmp<要求的\u尺寸)
打破
宽度_tmp/=2;
高度_tmp/=2;
比例*=2;
}
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
bmp=BitmapFactory.decodeFile(文件路径,o2);
返回文件路径;
}
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
//TODO自动生成的方法存根
super.onActivityResult(请求代码、结果代码、数据);
if(requestCode==画廊图片){
if(resultCode==RESULT\u OK){
Uri selectedImage=data.getData();
字符串[]filePathColumn={MediaStore.Images.Media.DATA};
Cursor Cursor=getContentResolver().query(selectedImage,filePathColumn,null,null);
如果(光标!=null){
cursor.moveToFirst();
int columnIndex=cursor.getColumnIndex(filePathColumn[0]);
String filePath=cursor.getString(columnIndex);
cursor.close();
字符串bb=解码文件(文件路径);
img_logo.setImageBitmap(bmp);
Log.d(“galleryimage”,即“galleryimage”和“GalleryShacks”***********”);
设置=GetSharedReferences(“pref”,0);
SharedReferences.Editor prefsEditor=settings.edit();
prefsEditor.putString(“photo1”,bb);
提交();
}
}否则{
Toast.makeText(getApplicationContext(),“已取消”,
吐司。长度(短)。show();
}
}else if(resultCode==RESULT\u取消){
Toast.makeText(getApplicationContext(),“已取消”,
吐司。长度(短)。show();
}
else if(requestCode==CAMERA\u请求){
if(resultCode==RESULT\u OK){
字符串[]投影={MediaStore.Images.Media.DATA};
Cursor Cursor=getContentResolver().query(mCapturedImageURI2,投影,null,null);
int column\u index\u data=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.data);
cursor.moveToFirst();
String capturedImageFilePath=cursor.getString(列索引数据);
Log.d(“照片*******”、“相机内拍摄int”+capturedImageFilePath);
字符串bbd=解码文件(capturedImageFilePath);
如果(数据!=null)
{
img_logo.setImageBitmap(bmp);
Log.d(“cameraimage”,“**********摄像头吸盘*******”;
设置=GetSharedReferences(“pref”,0);
SharedReferences.Editor prefsEditor=settings.edit();
prefsEditor.putString(“photo1”,bbd);
提交();
public class MainActivity extends Activity {
ImageView img_logo;
protected static final int CAMERA_REQUEST = 0;
protected static final int GALLERY_PICTURE = 1;
Uri mCapturedImageURI2;
SharedPreferences settings;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    settings = getSharedPreferences("pref", 0);

    img_logo = (ImageView) findViewById(R.id.imageView1);
    Log.d("SHAREDimagezzzz", "**********SHAREDzzzzzzz suckes******");
    img_logo.setImageURI(Uri.parse(settings.getString("photo1", "android.resource://com.tiktak.babyalbum/" + R.drawable.ic_launcher)));
    Log.d("SHAREDimage", "**********SHARED suckes******");
    img_logo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            startDialog();
        }
    });
}
protected void startDialog() {
    AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
    myAlertDialog.setTitle("Upload Pictures Option");
    myAlertDialog.setMessage("How do you want to set your picture?");

    myAlertDialog.setPositiveButton("Gallery",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT);
                    galleryintent.setType("image/*");
                    galleryintent.putExtra("return-data", true);
                    startActivityForResult(galleryintent, GALLERY_PICTURE);
                }
            });

    myAlertDialog.setNegativeButton("Camera",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    ContentValues values = new ContentValues();  
                    values.put(MediaStore.Images.Media.TITLE, "fileName");  
                    mCapturedImageURI2 = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  

                    Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI2);  
                    startActivityForResult(cameraIntent, CAMERA_REQUEST);

                }
            });
    myAlertDialog.show();
}
Bitmap bmp;
public String decodeFile(String filePath) {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 2048;

    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bmp = BitmapFactory.decodeFile(filePath, o2);
    return filePath;

}

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

    if (requestCode == GALLERY_PICTURE) {
        if (resultCode == RESULT_OK) {

                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                if (cursor != null) {
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();

               String bb = decodeFile(filePath);
               img_logo.setImageBitmap(bmp);
               Log.d("galleryimage", "**********gallery suckes******");
               settings = getSharedPreferences("pref", 0);
               SharedPreferences.Editor prefsEditor = settings.edit();
                    prefsEditor.putString("photo1", bb);
                    prefsEditor.commit();
                }

            } else {
                Toast.makeText(getApplicationContext(), "Cancelled",
                        Toast.LENGTH_SHORT).show();
            }
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getApplicationContext(), "Cancelled",
                    Toast.LENGTH_SHORT).show();
        }
     else if (requestCode == CAMERA_REQUEST) {
        if (resultCode == RESULT_OK) {
            String[] projection = { MediaStore.Images.Media.DATA}; 
            Cursor cursor = getContentResolver().query(mCapturedImageURI2, projection, null, null, null); 
            int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
            cursor.moveToFirst(); 
            String capturedImageFilePath = cursor.getString(column_index_data);
            Log.d("photos*******"," in camera take int  "+capturedImageFilePath);

            String bbd = decodeFile(capturedImageFilePath);

            if(data != null)
            {
                img_logo.setImageBitmap(bmp);
                Log.d("cameraimage", "**********camera suckes******");
                settings = getSharedPreferences("pref", 0);
                SharedPreferences.Editor prefsEditor = settings.edit();
                    prefsEditor.putString("photo1", bbd);
                    prefsEditor.commit();
            }
        }
}
}
}
     img_logo.setImageBitmap(bmp); 
public static Bitmap getThumbnail(String path, int target_width, int target_height, int max_size) {

    try {
      BitmapFactory.Options options = new BitmapFactory.Options();
      options.inJustDecodeBounds = true;
      BitmapFactory.decodeFile(path, options);

      int scale = 1;
      while ((options.outWidth * options.outHeight) * (1 / Math.pow(scale, 2)) >
        max_size) {
        scale++;
      }
      Log.d("boom", "scale = " + scale + ", orig-width: " + options.outWidth + ", orig-height: " + options.outHeight);

      Bitmap pic = null;
      if (scale > 1) {
        scale--;
        options = new BitmapFactory.Options();
        options.inSampleSize = scale;
        pic = BitmapFactory.decodeFile(path, options);

        Log.d("boom", "1th scale operation dimenions - width: " + target_width + ", target_height: " + target_height);

        double y = Math.sqrt(max_size
          / (((double) target_width) / target_height));
        double x = (y / target_height) * target_width;

        Bitmap scaledBitmap = Bitmap.createScaledBitmap(pic, (int) x, (int) y, true);
        pic.recycle();
        pic = scaledBitmap;

        System.gc();
      } else {
        pic = BitmapFactory.decodeFile(path, options);
      }

      Log.d("boom", "bitmap size - width: " +pic.getWidth() + ", height: " + pic.getHeight());
      return pic;
    } catch (Exception e) {
      Log.e("boom", e.getMessage(),e);
      return null;
    }
  }
String bb = decodeFile(filePath);
...
prefsEditor.putString("photo1", bb);
img_logo.setImageURI(Uri.parse(settings.getString("photo1", "android.resource://com.tiktak.babyalbum/" + R.drawable.ic_launcher)));
public class MainActivity extends Activity {
ImageView img_logo;
protected static final int CAMERA_REQUEST = 0;
protected static final int GALLERY_PICTURE = 1;
Uri mCapturedImageURI2;
SharedPreferences settings;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    settings = getSharedPreferences("pref", 0);
    Button b1 = (Button) findViewById(R.id.button1);
    Button b2 = (Button) findViewById(R.id.bto2);
    b2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            startActivity(new Intent(MainActivity.this, A2.class));
        }
    });

    img_logo = (ImageView) findViewById(R.id.imageView1);
    Log.d("SHAREDimagezzzz", "**********SHAREDzzzzzzz suckes******");
    BitmapFactory.Options o3 = new BitmapFactory.Options();
    o3.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(settings.getString("photo1", "android.resource://com.tiktak.babyalbum/" + R.drawable.ic_launcher), o3);
    final int REQUIRED_SIZE = 2048;

    int width_tmp = o3.outWidth, height_tmp = o3.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }
    BitmapFactory.Options o4 = new BitmapFactory.Options();
    o4.inSampleSize = scale;
    Bitmap bit1 = BitmapFactory.decodeFile(settings.getString("photo1", "android.resource://com.tiktak.babyalbum/" + R.drawable.ic_launcher), o4);

    img_logo.setImageBitmap(bit1);
    // img_logo.setImageURI(Uri.parse(settings.getString("photo1", "android.resource://com.tiktak.babyalbum/" + R.drawable.ic_launcher)));

    Log.d("SHAREDimage", "**********SHARED suckes******");
    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            startDialog();
        }
    });

}
protected void startDialog() {
    AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
    myAlertDialog.setTitle("Upload Pictures Option");
    myAlertDialog.setMessage("How do you want to set your picture?");

    myAlertDialog.setPositiveButton("Gallery",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT);
                    galleryintent.setType("image/*");
                    galleryintent.putExtra("return-data", true);
                    startActivityForResult(galleryintent, GALLERY_PICTURE);
                }
            });

    myAlertDialog.setNegativeButton("Camera",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    ContentValues values = new ContentValues();  
                    values.put(MediaStore.Images.Media.TITLE, "fileName");  
                    mCapturedImageURI2 = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  

                    Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
                    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI2);  
                    startActivityForResult(cameraIntent, CAMERA_REQUEST);

                }
            });
    myAlertDialog.show();

}


Bitmap bmp;
public void decodeFile(String filePath) {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 2048;

    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bmp = BitmapFactory.decodeFile(filePath, o2);



}

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

    if (requestCode == GALLERY_PICTURE) {
        if (resultCode == RESULT_OK) {


                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                if (cursor != null) {
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();


               decodeFile(filePath);
               img_logo.setImageBitmap(bmp);
               Log.d("galleryimage", "**********gallery suckes******");
               settings = getSharedPreferences("pref", 0);
               Editor prefsEditor = settings.edit();
                    prefsEditor.putString("photo1", filePath);
                    prefsEditor.commit();
                }


            } else {
                Toast.makeText(getApplicationContext(), "Cancelled",
                        Toast.LENGTH_SHORT).show();
            }
        } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getApplicationContext(), "Cancelled",
                    Toast.LENGTH_SHORT).show();
        }
     else if (requestCode == CAMERA_REQUEST) {
        if (resultCode == RESULT_OK) {
            String[] projection = { MediaStore.Images.Media.DATA}; 
            Cursor cursor = getContentResolver().query(mCapturedImageURI2, projection, null, null, null); 
            int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
            cursor.moveToFirst(); 
            String capturedImageFilePath = cursor.getString(column_index_data);
            Log.d("photos*******"," in camera take int  "+capturedImageFilePath);

            decodeFile(capturedImageFilePath);

            if(data != null)
            {
                img_logo.setImageBitmap(bmp);
                settings = getSharedPreferences("pref", 0);
                Editor prefsEditor = settings.edit();
                    prefsEditor.putString("photo1", capturedImageFilePath);
                    prefsEditor.commit();

            }

        }

}

}


}