marian
marian

Reputation: 287

Cannot resolve method onActivityResult

Im trying to create an application that allows sharing images and videos from gallery into facebook. There is error popping up in my coding in this lines (highlighted in red) : "onActivityResult", "getContentResolver", "PlusShare", "setType", "addStream", "setText" and "getIntent" saying cannot resolve method. Full coding as follows:

MainActivity.java

package com.example.dothis.facebook;


import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

private static final int REQ_SELECT_PHOTO = 1;
private static final int REQ_START_SHARE = 2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setResult(RESULT_OK);

    // listeners of our two buttons
    View.OnClickListener handler = new View.OnClickListener() {
        public void onClick(View v) {


            Button shareMediaButton = (Button)    findViewById(R.id.share_media);
            shareMediaButton.setOnClickListener(new   android.view.View.OnClickListener() {
                public void onClick(View v) {
                    Intent photoPicker = new     Intent(Intent.ACTION_PICK);
                    photoPicker.setType("video/*, image/*");
                    startActivityForResult(photoPicker, REQ_SELECT_PHOTO);
                }
            });

        }

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

            if (requestCode == REQ_SELECT_PHOTO) {
                if (resultCode == Activity.RESULT_OK) {
                    Uri selectedImage = intent.getData();
                    ContentResolver cr = this.getContentResolver();
                    String mime = cr.getType(selectedImage);

                    PlusShare.Builder share = new  PlusShare.Builder(this);
                    share.setText("hello everyone!");
                    share.addStream(selectedImage);
                    share.setType(mime);
                    startActivityForResult(share.getIntent(),   REQ_START_SHARE);

                }
            }
        }

    }
   }

Android manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dothis.facebook" >

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id" />


    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER"  />
        </intent-filter>
    </activity>
</application>

</manifest>

Can someone guide me on how to solve this problem? Any kind of suggestions are much appreciated. Thank you.

Upvotes: 1

Views: 5679

Answers (3)

KishuDroid
KishuDroid

Reputation: 5451

Try this code:

    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpeg");
    share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/path/to/image.ext"));
    
    startActivity(Intent.createChooser(share, "Share Image"));

Upvotes: 1

user5364525
user5364525

Reputation:

First of all you need to modify your Manifest.xml file like this

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature
        android:name="android.hardware.camera"
        android:required="false" />

Then modify your Method like:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQ_CODE) {
        if (resultCode == RESULT_OK) {
            taken = true;
            getContentResolver().notifyChange(uri, null);
            ContentResolver cr = getContentResolver();
            try {
                bmp = MediaStore.Images.Media.getBitmap(cr, uri);
                imagePhoto.setImageBitmap(bmp);
                Toast.makeText(UserProfile.this, "Image has been saved to " + file.getAbsolutePath() + " as " + file.getName(), Toast.LENGTH_LONG).show();

            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
        } else if (resultCode == RESULT_CANCELED) {
            taken = false;
            Toast.makeText(UserProfile.this, "Action canceled", Toast.LENGTH_SHORT).show();
        }
    }
}

Where you call the intent to use the came enter this:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "picture.jpg");
uri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, REQ_CODE);

i used those values to make it work

private static final int REQ_CODE = 1152;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;

Hope it helps!!!

Upvotes: 1

Derek Fung
Derek Fung

Reputation: 8211

You quote {} is in wrong place, your onCreate method anonymous class View.OnClickListener didn't close the with } properly.

Below is the fixed version with the quote placed correctly.

package com.example.dothis.facebook;


import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

  private static final int REQ_SELECT_PHOTO = 1;
  private static final int REQ_START_SHARE = 2;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setResult(RESULT_OK);

    // listeners of our two buttons
    View.OnClickListener handler = new View.OnClickListener() {
      public void onClick(View v) {

        Button shareMediaButton = (Button) findViewById(R.id.share_media);
        shareMediaButton.setOnClickListener(new android.view.View.OnClickListener() {
          public void onClick(View v) {
            Intent photoPicker = new Intent(Intent.ACTION_PICK);
            photoPicker.setType("video/*, image/*");
            startActivityForResult(photoPicker, REQ_SELECT_PHOTO);
          }
        });

      }
    };
  }


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

    if (requestCode == REQ_SELECT_PHOTO) {
      if (resultCode == Activity.RESULT_OK) {
        Uri selectedImage = intent.getData();
        ContentResolver cr = this.getContentResolver();
        String mime = cr.getType(selectedImage);

        PlusShare.Builder share = new PlusShare.Builder(this);
        share.setText("hello everyone!");
        share.addStream(selectedImage);
        share.setType(mime);
        startActivityForResult(share.getIntent(), REQ_START_SHARE);

      }
    }
  }

}

Upvotes: 0

Related Questions