2

I am able to load image gallery and display image in Imageview. I need to save the image which user selects in parse.com as blackened. Its a picturepath that I am able to load using BitmapFactory. I have created a column in data browser with field as File. I have also gone through the tutorials on Parse.com and implemented each step. But its not working when click on save button. App is crashing each time.

My Code is :

public class LoadImg extends Activity  {
private static int RESULT_LOAD_IMAGE = 1;
FrameLayout layout;
Button save ;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    save=(Button) findViewById(R.id.button2);
    String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
    if(!picturePath.equals(""))
    {
       ImageView imageView = (ImageView) findViewById(R.id.imageView1);
       imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }        
    Button buttonLoadImage = (Button) findViewById(R.id.button1);
    buttonLoadImage.setOnClickListener(new View.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);
        }
    });        
    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // Locate the image in res > 
            Bitmap bitmap = BitmapFactory.decodeFile("picturePath");
            // 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("picturePath", image);
            // Upload the image into Parse Cloud
            file.saveInBackground();

            // Create a New Class called "ImageUpload" in Parse
            ParseObject imgupload = new ParseObject("ImageUpload");

            // Create a column named "ImageName" and set the string
            imgupload.put("Image", "picturePath");

            // Create a column named "ImageFile" and insert the image
            imgupload.put("ImageFile", file);

            // Create the class and the columns
            imgupload.saveInBackground();

            // Show a simple toast message
            Toast.makeText(LoadImg.this, "Image Uploaded",Toast.LENGTH_SHORT).show();           
        }
    });
}
@Override
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]);
        String picturePath = cursor.getString(columnIndex);
        PreferenceManager.getDefaultSharedPreferences(this).edit().putString("picturePath", picturePath).commit();
        cursor.close();
        ImageView imageView = (ImageView) findViewById(R.id.imageView1);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));                        
           } 
    }}

LogCat is :

01-05 20:43:54.678: E/JavaBinder(1298): !!! FAILED BINDER TRANSACTION !!! 01-05 20:44:48.418: E/AndroidRuntime(3453): Uncaught handler: thread main exiting due to uncaught exception 01-05 20:44:48.428: E/AndroidRuntime(3453): java.lang.NullPointerException 01-05 20:44:48.428: E/AndroidRuntime(3453): at com.lovy.parse.LoadImg$2.onClick(LoadImg.java:56) 01-05 20:44:48.428: E/AndroidRuntime(3453): at android.view.View.performClick(View.java:2418) 01-05 20:44:48.428: E/AndroidRuntime(3453): at android.view.View.onTouchEvent(View.java:4233) 01-05 20:44:48.428: E/AndroidRuntime(3453): at android.widget.TextView.onTouchEvent(TextView.java:6645) 01-05 20:44:48.428: E/AndroidRuntime(3453): at android.view.View.dispatchTouchEvent(View.java:3763) 01-05 20:44:48.428: E/AndroidRuntime(3453): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:968) 01-05 20:44:48.428: E/AndroidRuntime(3453): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:968) 01-05 20:44:48.428: E/AndroidRuntime(3453): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:968) 01-05 20:44:48.428: E/AndroidRuntime(3453): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:968) 01-05 20:44:48.428: E/AndroidRuntime(3453): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:968) 01-05 20:44:48.428: E/AndroidRuntime(3453): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659) 01-05 20:44:48.428: E/AndroidRuntime(3453): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107) 01-05 20:44:48.428: E/AndroidRuntime(3453): at android.app.Activity.dispatchTouchEvent(Activity.java:2064) 01-05 20:44:48.428: E/AndroidRuntime(3453): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643) 01-05 20:44:48.428: E/AndroidRuntime(3453): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691) 01-05 20:44:48.428: E/AndroidRuntime(3453): at android.os.Handler.dispatchMessage(Handler.java:99) 01-05 20:44:48.428: E/AndroidRuntime(3453): at android.os.Looper.loop(Looper.java:123) 01-05 20:44:48.428: E/AndroidRuntime(3453): at android.app.ActivityThread.main(ActivityThread.java:4370) 01-05 20:44:48.428: E/AndroidRuntime(3453): at java.lang.reflect.Method.invokeNative(Native Method) 01-05 20:44:48.428: E/AndroidRuntime(3453): at java.lang.reflect.Method.invoke(Method.java:521) 01-05 20:44:48.428: E/AndroidRuntime(3453): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 01-05 20:44:48.428: E/AndroidRuntime(3453): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 01-05 20:44:48.428: E/AndroidRuntime(3453): at dalvik.system.NativeStart.main(Native Method) 01-05 20:44:48.438: E/SemcCheckin(3453): Get crash dump level : java.io.FileNotFoundException: /data/semc-checkin/crashdump 01-05 20:44:48.508: E/SemcCheckin(1565): Get Crash Level : java.io.FileNotFoundException: /data/semc-checkin/crashdump 01-05 20:44:58.378: E/ATK(2142): widget kill start

7
  • What is the question? Commented Jan 5, 2014 at 15:00
  • @cYrixmorten I have edited the question above. Commented Jan 5, 2014 at 15:03
  • Can you post the LogCat message? Commented Jan 5, 2014 at 15:03
  • @cYrixmorten I have edited the code and posted the logcat. Thanx Commented Jan 5, 2014 at 15:21
  • nullpointer at com.lovy.parse.LoadImg$2.onClick(LoadImg.java:56). What is on line 56? Commented Jan 5, 2014 at 15:55

2 Answers 2

5
save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // Locate the image in res > 
            //Bitmap bitmap = BitmapFactory.decodeFile("picturePath");
            // Convert it to byte
            //ByteArrayOutputStream stream = new ByteArrayOutputStream();
            // Compress image to lower quality scale 1 - 100
            //bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

            try {
                image = readInFile(path);
            }
            catch(Exception e) { 
                e.printStackTrace();
            }

            // Create the ParseFile
            ParseFile file = new ParseFile("picturePath", image);
            // Upload the image into Parse Cloud
            file.saveInBackground();

            // Create a New Class called "ImageUpload" in Parse
            ParseObject imgupload = new ParseObject("Image");

            // Create a column named "ImageName" and set the string
            imgupload.put("Image", "picturePath");


            // Create a column named "ImageFile" and insert the image
            imgupload.put("ImageFile", file);

            // Create the class and the columns
            imgupload.saveInBackground();

            // Show a simple toast message
            Toast.makeText(LoadImg.this, "Image Saved, Upload another one ",Toast.LENGTH_SHORT).show(); 

        }
    });
}

private byte[] readInFile(String path) throws IOException {
    // TODO Auto-generated method stub
    byte[] data = null;
    File file = new File(path);
    InputStream input_stream = new BufferedInputStream(new FileInputStream(
            file));
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    data = new byte[16384]; // 16K
    int bytes_read;
    while ((bytes_read = input_stream.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, bytes_read);
    }
    input_stream.close();
    return buffer.toByteArray();

}
Sign up to request clarification or add additional context in comments.

Comments

0

No LogCat yet but I suspect:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.id.imageView1);

To cause problems, as R.id.imageView1 is a id to a View and not an image resource.

You could try this instead:

Bitmap bitmap = decodeFile(picturePath);

if (bitmap != null) {
   // ...
}

Note:

you also have this:

// Create a column named "ImageName" and set the string
imgupload.put("Image", R.id.imageView1);

From the comment, your intention is to save a String but actually you save the id Integer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.