OutOfMemoryError android : converting bitmap to string (imposiable?) - android

i'm so fustrated from this little thing that for some reason just doesn't work out...i don't really know what else to do. the pictures im trying to add are been selected by my gallery..(the last thing i've checked was 1024X768 and 32 color bit)
ImageButton img;
private static final int SELECTED_PICTURE = 1;
String picPathFile;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_channel);
img = (ImageButton) findViewById(R.id.imageButton);
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECTED_PICTURE);
}
});
// defines the button functionality
Button b = (Button) findViewById(R.id.backToMenu);
b.setOnClickListener(new View.OnClickListener() {
#Override
// defines to move to menu activity when button is pressed
public void onClick(View v) {
Intent showImageIntent = new Intent(AddChannelActivity.this, ShowPreviewChannel.class);
Bitmap selected_image = BitmapFactory.decodeFile(picPathFile);
showImageIntent.putExtra("pic_file", picPathFile);
showImageIntent.putExtra("c_name", name);
showImageIntent.putExtra("c_id", "777");
startActivity(showImageIntent);
//newImage.recycle(); // where do i put it??
//newImage = null;
//selected_image.recycle();
//selected_image = null;
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case SELECTED_PICTURE: {
if (requestCode == SELECTED_PICTURE) {
Uri uri = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
picPathFile = picturePath;
have_picture = true;
}
break;
}
default: break;
}
}
Intent intent = getIntent();
// the rellvant part from the show preview class
Bundle bundle = intent.getExtras();
if(bundle!= null){
String picturePath = intent.getStringExtra("pic_file");
Bitmap bm = BitmapFactory.decodeFile(picturePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray(); // HERE~!!!!! out of memory
}
ive tried everything....please help me :/
i saw the baos size is 7193767

Related

How can selected gallery image show in first activity and another activity?

public class Main2Activity extends AppCompatActivity {
private static int PICK_IMAGE_REQUEST = 1;
ImageView imgView;
static final String TAG = "Main2Activity";
public String[] filePathColon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void loadImagefromGallery(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && null != data) {
Uri uri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
int nh = (int) (bitmap.getHeight() * (1024.0 / bitmap.getWidth()));
Bitmap scaled = Bitmap.createScaledBitmap(bitmap, 1024, nh, true);
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
final String filePath = cursor.getString(columnIndex);
imgView = (ImageView) findViewById(imageView);
imgView.setImageBitmap(scaled);
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Main3Activity.class);
intent.putExtra("imageUri", filePath);
startActivity(intent);
}
});
} else {
Toast.makeText(this, "No.", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Oops! Sorry", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
public class Main3Activity extends AppCompatActivity {
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
imageView = (ImageView) findViewById(R.id.imageView2);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap ba = this.getIntent().getParcelableExtra("imageUri");
imageView.setImageBitmap(ba);
}
}
What is the problem?
Step #1: Delete these lines:
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
final String filePath = cursor.getString(columnIndex);
Step #2: Replace intent.putExtra("imageUri", filePath); with intent.setData(uri).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Step #3: Replace:
Bitmap ba = this.getIntent().getParcelableExtra("imageUri");
imageView.setImageBitmap(ba);
with:
Uri imageUri = getIntent().getData();
imageView.setImageURI(imageUri);
Eventually, replace of your bitmap work (the Bitmap that you are not using in the first activity, and setImageURI() in the second activity) with an image-loading library, such as Picasso, so that you can move all this data processing off the main application thread.

upload photo to parse ( android)

I'm a new parse.com user and I want to upload image to it that is chosen by the user.
the user choose the image , then the name of it and then press the upload button
but when I click the button upload the program is crash :(
this is my try
Button btn;
ImageView Pic;
ParseObject shop;
final int PHOTO_SELECTED = 1;
ParseFile file;
ParseFile photoFile;
final Context context = this;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.openshop);
btn = (Button) findViewById(R.id.button1);
Pic = (ImageView) findViewById(R.id.imageView1);
final int PHOTO_SELECTED = 1;
Pic.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i,PHOTO_SELECTED);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PHOTO_SELECTED && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
final String picturePath = cursor.getString(columnIndex);
cursor.close();
final Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
Pic.setImageBitmap(bitmap);
btn.setOnClickListener(new View.OnClickListener() {
#SuppressLint("NewApi")
public void onClick(View arg0) {
EditText name = (EditText) findViewById(R.id.sname);
shop = new ParseObject("imagetest");
// Bitmap bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), Pic);
// Convert it to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Compress image to lower quality scale 1 - 100
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();
// Create the ParseFile
ParseFile file = new ParseFile(name.getText().toString()+".png", image);
// Upload the image into Parse Cloud
file.saveInBackground();
//Create
shop.put("name", name.getText().toString());
shop.put("UserOpen",ParseUser.getCurrentUser());
shop.put("Image", file);
shop.saveInBackground();
// Show a simple toast message
Toast.makeText(getApplicationContext(), "Created",
Toast.LENGTH_SHORT).show();
}
});
}
}
}
thankyou

How to specify whether intent is from camera or gallery in another Activity

I'm pretty new with android and I'm having problems specifying whether Intent is coming from camera or gallery in second activity. Here's my code:
MainActivity:
public class MainActivity extends ActionBarActivity implements OnClickListener {
private static final int CAMERA_REQUEST = 1888;
private static final int PICK_FROM_CAMERA = 1;
private static final int PICK_FROM_GALLERY = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Button cameraButton = (Button) this.findViewById(R.id.cameraButton);
Button galleryButton = (Button) this.findViewById(R.id.galleryButton);
//Here we choose to take a new picture
cameraButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
}
});
// Here we choose a photo from the gallery
galleryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, PICK_FROM_GALLERY);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case PICK_FROM_CAMERA:
if (resultCode == Activity.RESULT_OK)
{
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
Intent captureIntent = new Intent(MainActivity.this, ImageViewerActivity.class);
captureIntent.putExtra("data", imageBitmap);
startActivity(captureIntent);
}
break;
case PICK_FROM_GALLERY:
if (resultCode == Activity.RESULT_OK)
{
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent intent = new Intent(this, ImageViewerActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
}
break;
}
}
public String getPath(Uri uri) {
// just some safety built in
if( uri == null ) {
// TODO perform some logging or show user feedback
return null;
}
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// this is our fallback here
return uri.getPath();
}
ImageViewerActivity:
public class ImageViewerActivity extends Activity {
public static ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.photo_view);
imageView = (ImageView)findViewById(R.id.imageView);
getData();
}
private void getData() {
// TODO Auto-generated method stub
if(//image is from camera)
{
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("data");
imageView.setImageBitmap(bitmap);
}
if(//image is from gallery)
{
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
imageView.setImageBitmap(bmp);
}
}
Any help is needed! Thanks a lot!
In your extras, you can pass another field with "INTENT_FROM" and put a String with a identifier = CAMERA, GALLERY or whatever.
To attach it, before sending your intent, create a bundle and add it to the intent that will be sent, like this:
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);
You can find tips to manage bundles here.
Passing a Bundle on startActivity()?
I hope this would help you!

How to convert an Image to Bits array in android

I am new to android development, Now currently am working on an App for Image Steganography. So in my app , I need to convert an Image that i selected from gallery to Bits array(Each pixel will have a 8 bit value, thats what i mean), How can i do it ? Can anybody help me ?
public class ImageActivity extends Activity {
private Button btnSelectImage;
private Button btnEncode;
String Pathfile=new String();
public String selectedImagePath;
private ImageView myImage;
Bitmap result;
public static final int ICON_SELECT_GALLERY = 1;
private static final Object IMAGE_TAKER_REQUEST = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
btnSelectImage = (Button) findViewById(R.id.button1);
btnEncode = (Button) findViewById(R.id.button2);
btnSelectImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
selectImage();
}
});
myImage = (ImageView) findViewById(R.id.imageView1);
}
public void selectImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, ICON_SELECT_GALLERY);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == 1)
{
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
Log.v("IMAGE PATH====>>>> ",selectedImagePath);
TextView imgPath=(TextView)findViewById(R.id.textView2);
imgPath.setText(selectedImagePath);
Pathfile=new String(selectedImagePath);
result = BitmapFactory.decodeFile(Pathfile);
myImage.setImageBitmap(result);
}
}
}
public String getPath(Uri uri)
{
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
Pass Bitmap and method will return byte[]
public static byte[] getBytesFromBitmap(Bitmap bitmap){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 70, stream);
return stream.toByteArray();
}

Need to have image from Gallery to show up in IMageView at ALL TIME. Whenever the app starts

I am somewhat frustrated trying to get this to work. Can someone please help me with this. I am new to this and all i want is to get an image from Gallery/Image_capture to show on an ImageView at ALL TIME. I have been back and forth with so many ppl and have got diff answer.
This is the Activity that has a ImageView called editpic. Once user clicks on it,it opens a Dialog asking to choose from Gallery or Camera.
public int GET_CAM_IMG=2;
public int GET_GAL_IMG=1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
profile();
private void profile() {
editpic = (ImageView)findViewById(R.id.editpic);
button1.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();
}});
ONCE USER CLICKS IT GOES THROUGH CASE AND SWITCH
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case 2://Camera
if (resultCode == -1) {
String encodedImageString = null;
Uri selectedImage = intent.getData();
String selectedImagepath = getPath(selectedImage);
editpic.setImageURI(selectedImage);
Bitmap bmp_image = BitmapFactory.decodeFile(filePath);
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (bmp_image.compress(Bitmap.CompressFormat.JPEG, 50, baos)) {
byte[] image = baos.toByteArray();
encodedImageString = Base64.encodeToString(image,
Base64.DEFAULT);
} else {
System.out.println("Compreesion returned false");
Log.d("Compress", "Compreesion returned false");
}
break;
case 3://Selecting from Gallery
if (resultCode == -1) {
String encodedImageString = null;
Bitmap bmp_image = null;
Bundle extras = intent.getExtras();
bmp_image = (Bitmap) extras.get("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (bmp_image.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");
}
}
break;
}
}
TRIED CREATING A METHOD SO I CAN GET THE IMAGE PATH AND THEN USE THAT BUT COULDN'T GET IT TO WORK
SharedPreferences userprofile = getSharedPreferences(filename,0);
SharedPreferences.Editor editor = userprofile.edit();
editor.putString("Imagepath",selectedImagepath);
editor.commit();
AFTER TAKING THE PIC, images comes on Imageview but disappears. I want it to be there as this is a profile pic. It is suppose to be something the contact pic in phonebook.

Categories

Resources