upload photo to parse ( android) - 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

Related

OutOfMemoryError android : converting bitmap to string (imposiable?)

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

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();
}

Android - ImageView won't show pic taken with camera

Please bear with me... I've been looking around for DAYS for a working, bare-bones piece of code that starts the camera activity, takes a picture, and places it on a simple ImageView >.< The code posted below fires up the activity and takes the pic alright, but the image does not show on the ImageView! Just what is missing? :'(
public class MainActivity extends Activity
{
private static final int PICK_IMAGE = 0;
private static final int PICK_IMAGE_FROM_GALLERY = 1;
private Button mBtnCamera, mBtnGallery, mBtnCancel;
private ImageView mImageView;
private Uri mURI;
private String mPhotoPath;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.imgDisplayImage);
mBtnCamera = (Button) findViewById(R.id.btnPhotoCamera);
mBtnGallery = (Button) findViewById(R.id.btnPhotoGallery);
mBtnCancel = (Button) findViewById(R.id.btnCancel);
mBtnCamera.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent camera = new Intent();
camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
camera.putExtra("crop", "true");
File f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
mURI = Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myFile.jpg"));
camera.putExtra(MediaStore.EXTRA_OUTPUT, mURI);
startActivityForResult(camera, PICK_IMAGE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == PICK_IMAGE)
{
// Result includes a Bitmap thumbnail?
if (data != null)
{
if (data.hasExtra("data"))
{
//Bitmap thumbnail = data.getParcelableExtra("data");
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
mImageView.setImageBitmap(thumbnail);
}
}
// If there is no thumbnail data, the image will have been stored in target output URI.
else
{
Cursor cursor = getContentResolver().query(
Media.EXTERNAL_CONTENT_URI, new String[]
{
Media.DATA,
Media.DATE_ADDED,
MediaStore.Images.ImageColumns.ORIENTATION
},
Media.DATE_ADDED,
null,
"date_added ASC"
);
if (cursor != null && cursor.moveToFirst())
{
do
{
mURI = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));
mPhotoPath = mURI.toString();
}
while (cursor.moveToNext());
cursor.close();
}
// Resize full image to fit out in image view.
int width = mImageView.getWidth();
int height = mImageView.getHeight();
BitmapFactory.Options factoryOptions = new BitmapFactory.Options();
factoryOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(/*mURI.getPath()*/ mPhotoPath, factoryOptions);
int imageWidth = factoryOptions.outWidth;
int imageHeight = factoryOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(
imageWidth/width,
imageHeight/height
);
// Decode the image file into a Bitmap sized to fill view
factoryOptions.inJustDecodeBounds = false;
factoryOptions.inSampleSize = scaleFactor;
factoryOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(/*mURI.getPath()*/ mPhotoPath, factoryOptions);
mImageView.setImageBitmap(bitmap);
}
}
}
}
I had same problem in some devices of samsung android Then I implemented logic to get path of captured photo.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == PICK_IMAGE)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added ASC");
if(cursor != null && cursor.moveToFirst())
{
do {
uri = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));
photoPath = uri.toString();
}while(cursor.moveToNext());
cursor.close();
}
if(photoPath != null) {
Bitmap bitmap = BitmapFactory.decodeFile(photoPath);
///Do Implement your logic whatever you want.
mImageView.setImageBitmap(bitmap);
}
}
}
Tried and tested to work on a Galaxy S3 phone. Credit to TGMCians for his help.
public class MainActivity extends Activity
{
private static final int PICK_IMAGE = 0;
private static final int PICK_IMAGE_FROM_GALLERY = 1;
private Button mBtnCamera, mBtnGallery, mBtnCancel;
private ImageView mImageView;
private Uri mURI;
private String mPhotoPath;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.imgDisplayImage);
mBtnCamera = (Button) findViewById(R.id.btnPhotoCamera);
mBtnGallery = (Button) findViewById(R.id.btnPhotoGallery);
mBtnCancel = (Button) findViewById(R.id.btnCancel);
mBtnCamera.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent camera = new Intent();
camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
camera.putExtra("crop", "true");
File f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
mURI = Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myFile.jpg"));
camera.putExtra(MediaStore.EXTRA_OUTPUT, mURI);
startActivityForResult(camera, PICK_IMAGE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == Activity.RESULT_OK)
{
if (requestCode == PICK_IMAGE)
{
Cursor cursor = getContentResolver().query(
Media.EXTERNAL_CONTENT_URI, new String[]
{
Media.DATA,
Media.DATE_ADDED,
MediaStore.Images.ImageColumns.ORIENTATION
},
Media.DATE_ADDED,
null,
"date_added ASC"
);
if (cursor != null && cursor.moveToFirst())
{
do
{
mURI = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));
mPhotoPath = mURI.toString();
}
while (cursor.moveToNext());
cursor.close();
}
if (data != null)
{
if (data.hasExtra("data"))
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
mImageView.setImageBitmap(thumbnail);
}
else
{
System.out.println("Intent bundle does not have the 'data' Extra");
int width = mImageView.getWidth();
int height = mImageView.getHeight();
BitmapFactory.Options factoryOptions = new BitmapFactory.Options();
factoryOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(/*mURI.getPath()*/ mPhotoPath, factoryOptions);
int imageWidth = factoryOptions.outWidth;
int imageHeight = factoryOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(
imageWidth/width,
imageHeight/height
);
// Decode the image file into a Bitmap sized to fill view
factoryOptions.inJustDecodeBounds = false;
factoryOptions.inSampleSize = scaleFactor;
factoryOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(/*mURI.getPath()*/ mPhotoPath, factoryOptions);
mImageView.setImageBitmap(bitmap);
}
}
}
}
else
{
System.out.println("Picture taking activity NOT returning RESULT_OK");
}
}
}
Try adding
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
(and a slight delay) right inside the
if (requestCode == PICK_IMAGE)
block. I think the problem might be that your device isn't refreshing the Media store correctly.
(of course the better action would be to use MediaScanner to scan your file)

dismiss() method of Progress dialog is not working

May be same question is encountered to you before, I am sorry for that but I really need to ask this.I am trying to show Progress dialog and then dismissing it But I am not able to do it. I have searched a lot and tried many ways but cant really get through. I am uploading images after picking from gallery. and during upload i want to show the dialog and after uploading dialog should be dismissed here is my code.
public class FaceActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
private Button upbtn;
public Bitmap bm;
public ByteArrayOutputStream bos;
public byte[] bitmapdata;
public String picturePath;
private ProgressDialog pd;
private BitmapFactory.Options options;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_face);
//pd = new ProgressDialog(FaceActivity.this);
upbtn = (Button) findViewById(R.id.buttonLoadPicture);
upbtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && 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]);
picturePath = cursor.getString(columnIndex);
cursor.close();
options = new BitmapFactory.Options();
// will results in a much smaller image than the original
options.inSampleSize = 8;
upload();
}
}
public void upload(){
// Here I am showing the dialog
pd = ProgressDialog.show(FaceActivity.this, "Please Wait", "Loading...", true, false);
bm = BitmapFactory.decodeFile(picturePath);
bos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 40 , bos);
bitmapdata = bos.toByteArray();
ParseFile file = new ParseFile("pic.jpg", bitmapdata);
file.saveInBackground();
ParseObject po = new ParseObject("Images");
po.put("Images", file);
po.saveInBackground();
ImageView imageView = (ImageView) findViewById(R.id.targetimage);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath,options));
// want to dismiss dialog here
pd.dismiss();
Toast.makeText(this, "Image Uploaded Successfully", Toast.LENGTH_LONG).show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_face, menu);
return true;
}
}
try to do that in asyc task.
private class asynUpload extends AsyncTask<String, Void, Integer> {
protected Integer doInBackground(String... params) {
try {
runOnUiThread(new Runnable() {
public void run() {
pd = ProgressDialog.show(FaceActivity.this, "Please Wait", "Loading...", true, false);
}
});
bm = BitmapFactory.decodeFile(picturePath);
bos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 40 , bos);
bitmapdata = bos.toByteArray();
ParseFile file = new ParseFile("pic.jpg", bitmapdata);
file.saveInBackground();
ParseObject po = new ParseObject("Images");
po.put("Images", file);
po.saveInBackground();
ImageView imageView = (ImageView) findViewById(R.id.targetimage);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath,options));
} catch (Exception e) {
return 0;
}
return 1;
}
protected void onPostExecute(Integer result) {
try {
runOnUiThread(new Runnable() {
public void run() {
pd.dismiss();
}
});
} catch (Exception e) {}
super.onPostExecute(result);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && 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]);
picturePath = cursor.getString(columnIndex);
cursor.close();
options = new BitmapFactory.Options();
// will results in a much smaller image than the original
options.inSampleSize = 8;
// use the task here
new asynUpload().execute();
}
}
You can look here for the idea you've been looking for implementing a background process: doInBackground not working in Android fragment

Categories

Resources