如何使用" easy"行动分享正确

时间:2014-03-30 11:58:39

标签: java android gridview share fullscreen

我有这个FullImageActivity

public class FullImageActivity extends Activity {

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

        // get intent data
        Intent i = getIntent();

        // Selected image id
        int position = i.getExtras().getInt("id");
        ImageAdapter imageAdapter = new ImageAdapter(this);

        ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
        imageView.setImageResource(imageAdapter.mThumbIds[position]);
        BitmapDrawable bm = (BitmapDrawable) imageView.getDrawable();
        Bitmap mysharebmp = bm.getBitmap();
        String path = Images.Media.insertImage(getContentResolver(),
                            mysharebmp, "MyImage", null);
        Uri uri = Uri.parse(path);
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("image/*");
        sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
        startActivity(Intent.createChooser(sharingIntent,
                            "Share image using"));
    }
}

按下图片后,全屏启动,同时显示共享菜单。 但我想要的是: 仅显示全屏,并且像谷歌示例中那样使用菜单项共享。 Adding an Easy Share Action 我不明白如何将它集成到我的代码中。

帮助男人的帮助

编辑 ImageAdapter

import java.lang.ref.WeakReference;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;



public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    // Keep all Images in array
    public Integer[] mThumbIds = {

    //There are 100 pics at the moment i removed for the post here. 
   R.drawable.angry_shaking,                                
                                                    R.drawable.new99   


    };


    // Constructor
    public ImageAdapter(Context c){
        mContext = c;
    }

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }



public View getView(int position, View convertView, ViewGroup parent) {
    //This actually is a bad solution, because every time convertView is reused, you will still initialize new ImageView, which is wrong
    //ImageView imageView = new ImageView(this.mContext);
    //new BitmapWorkerTask(imageView).execute(Tattoos[position]);
    //return imageView;

    //Better solution
    ImageView imageView = null;

    if (convertView == null) {
        imageView = new ImageView(this.mContext);
        new BitmapWorkerTask(imageView).execute(mThumbIds[position]);
        //create new ImageView if it is not present and populate it with some image
    } else {
        imageView = (ImageView) convertView;
        //re-use ImageView that already exists in memory
    }// clean up your old bitmap first, if there is one.
    if(imageView.getDrawable() instanceof BitmapDrawable){
        BitmapDrawable bd = (BitmapDrawable) imageView.getDrawable();
        imageView.setImageDrawable(null);
        if(bd.getBitmap() != null){
            bd.getBitmap().recycle();
        }
        bd = null;
    }

    new BitmapWorkerTask(imageView).execute(mThumbIds[position]);

return imageView;
}


class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
        private final WeakReference<ImageView> imageViewReference;
        private int data = 0;

        public BitmapWorkerTask(ImageView imageView) {
            // Use a WeakReference to ensure the ImageView can be garbage collected
            imageViewReference = new WeakReference<ImageView>(imageView);
        }

        // Decode image in background.
        @Override
        protected Bitmap doInBackground(Integer... params) {
            data = params[0];
            return decodeSampledBitmapFromResource(ImageAdapter.this.mContext.getResources(), data, 100, 100);
        }

        // Once complete, see if ImageView is still around and set bitmap.
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (isCancelled()) {
                bitmap = null;
            }

            if (imageViewReference != null && bitmap != null) {
                final ImageView imageView = imageViewReference.get();
                if (imageView != null) {
                    imageView.setImageBitmap(bitmap);
                    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                    imageView.setLayoutParams(new GridView.LayoutParams(105, 105));

                }
            }
        }
    }

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                         int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            // Calculate ratios of height and width to requested height and width
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }

        return inSampleSize;
    }

}

编辑2

 import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.ShareActionProvider;




public class FullImageActivity extends ActionBarActivity {

    private ShareActionProvider mShareActionProvider;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate menu resource file.
        getMenuInflater().inflate(R.menu.menu_item_share, menu);
        // Locate MenuItem with ShareActionProvider
        MenuItem item = menu.findItem(R.id.menu_item_share);

        // Fetch and store ShareActionProvider
        mShareActionProvider = (ShareActionProvider) item.getActionProvider();

        // Return true to display menu
        return true;}

     // Call to update the share intent
        private void setShareIntent(Intent shareIntent) {
            if (mShareActionProvider != null) {
                mShareActionProvider.setShareIntent(shareIntent);
            }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.full_image);
        Intent i = getIntent();

        // Selected image id
        int position = i.getExtras().getInt("id");
        ImageAdapter imageAdapter = new ImageAdapter(this);

        ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
        imageView.setImageResource(imageAdapter.mThumbIds[position]);
        BitmapDrawable bm = (BitmapDrawable) imageView.getDrawable();
        Bitmap mysharebmp = bm.getBitmap();
        String path = Images.Media.insertImage(getContentResolver(),
                            mysharebmp, "MyImage", null);
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    Uri uri = Uri.parse(path);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.setType("image/jpeg");
    startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share_via)));;
        }
    }

这是我的新FullImageActivity与新库工作:) 但是当我进入全屏时它崩溃了?

03-30 20:11:24.570: E/AndroidRuntime(13720): FATAL EXCEPTION: main
03-30 20:11:24.570: E/AndroidRuntime(13720): Process: com.example.mo, PID: 13720
03-30 20:11:24.570: E/AndroidRuntime(13720): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mo/skull.droid.mo.FullImageActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
03-30 20:11:24.570: E/AndroidRuntime(13720):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
03-30 20:11:24.570: E/AndroidRuntime(13720):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248)
03-30 20:11:24.570: E/AndroidRuntime(13720):    at android.app.ActivityThread.access$800(ActivityThread.java:138)
03-30 20:11:24.570: E/AndroidRuntime(13720):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
03-30 20:11:24.570: E/AndroidRuntime(13720):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-30 20:11:24.570: E/AndroidRuntime(13720):    at android.os.Looper.loop(Looper.java:136)
03-30 20:11:24.570: E/AndroidRuntime(13720):    at android.app.ActivityThread.main(ActivityThread.java:5050)
03-30 20:11:24.570: E/AndroidRuntime(13720):    at java.lang.reflect.Method.invokeNative(Native Method)
03-30 20:11:24.570: E/AndroidRuntime(13720):    at java.lang.reflect.Method.invoke(Method.java:515)
03-30 20:11:24.570: E/AndroidRuntime(13720):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-30 20:11:24.570: E/AndroidRuntime(13720):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-30 20:11:24.570: E/AndroidRuntime(13720):    at dalvik.system.NativeStart.main(Native Method)
03-30 20:11:24.570: E/AndroidRuntime(13720): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
03-30 20:11:24.570: E/AndroidRuntime(13720):    at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:108)
03-30 20:11:24.570: E/AndroidRuntime(13720):    at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
03-30 20:11:24.570: E/AndroidRuntime(13720):    at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
03-30 20:11:24.570: E/AndroidRuntime(13720):    at skull.droid.mo.FullImageActivity.onCreate(FullImageActivity.java:48)
03-30 20:11:24.570: E/AndroidRuntime(13720):    at android.app.Activity.performCreate(Activity.java:5241)
03-30 20:11:24.570: E/AndroidRuntime(13720):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-30 20:11:24.570: E/AndroidRuntime(13720):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
03-30 20:11:24.570: E/AndroidRuntime(13720):    ... 11 more

我必须使用其他东西,然后使用我的ImageAdapter或我能做什么?

MainActivity

public class MainActivity extends Activity {

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

    GridView gridView = (GridView) findViewById(R.id.gridview);

    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(this));

    /**
     * On Click event for Single Gridview Item
     * */
    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            // Sending image id to FullScreenActivity
            Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
            // passing array index
            i.putExtra("id", position);
            startActivity(i);
        }
    });
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }

}

Thx dudes!

修改

我的清单

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

2 个答案:

答案 0 :(得分:1)

  1. 将ActionBarActivity扩展为Activity。

  2. 创建共享菜单XML

  3. 覆盖onCreateOptionsMenu并对共享菜单XML进行充气。

  4. 使用共享操作提供程序

  5. 设置共享意图

答案 1 :(得分:0)

我可能错了,但根据您对上述Libin的评论,我猜您无法在项目中使用ActionBarActivity

首先,关于您想要的Share Intent,正如您所说example from Google是通过ActionBar完成的。要阅读有关操作栏的更多信息,请参阅this

现在,因为您收到错误ActionBarActivity cannot be resolved to a type很可能是因为您没有在项目中添加支持库。 ActionBar是从API 11开始引入的。用于早期版本的Android ActionBarActivity在支持包中提供,该支持包向后兼容eclair。

要在项目中添加支持库,请阅读this。 Lipin在他的回答中已经提到的其余步骤。按照这些步骤,您将能够实现您想要的。