我在(android 2.3)中共享图像时遇到了麻烦,我尝试在 Jelly Bean 中共享并且它有效。我已经在 30 多台运行 Android 2.3 的设备上测试了我的应用程序,并且所有设备的共享选项都不起作用。当我单击共享按钮时,它会显示一条消息:“无法加载资源”..:/
这是我的代码:
public class ImagePager extends BaseActivity implements OnClickListener {
Button share, back;
private static final String STATE_POSITION = "STATE_POSITION";
public String image;
ViewPager pager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ac_image_pager);
share = (Button)findViewById(R.id.share);
back = (Button)findViewById(R.id.back);
share.setOnClickListener(this);
back.setOnClickListener(this);
Bundle bundle = getIntent().getExtras();
String[] imageUrls = bundle.getStringArray(Extra.IMAGES);
int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);
if (savedInstanceState != null) {
pagerPosition = savedInstanceState.getInt(STATE_POSITION);
}
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new ImagePagerAdapter(imageUrls));
pager.setCurrentItem(pagerPosition);
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(STATE_POSITION, pager.getCurrentItem());
}
private class ImagePagerAdapter extends PagerAdapter {
private String[] images;
private LayoutInflater inflater;
ImagePagerAdapter(String[] images) {
this.images = images;
inflater = getLayoutInflater();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
@Override
public void finishUpdate(View container) {
}
@Override
public int getCount() {
return images.length;
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.item_pager_image, view, false);
ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
imageLoader.displayImage(images[position], imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
spinner.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
String message = null;
switch (failReason.getType()) {
case IO_ERROR:
message = "Input/Output error";
break;
case DECODING_ERROR:
message = "Image can't be decoded";
break;
case NETWORK_DENIED:
message = "Downloads are denied";
break;
case OUT_OF_MEMORY:
message = "Out Of Memory error";
break;
case UNKNOWN:
message = "Unknown error";
break;
}
Toast.makeText(ImagePager.this, message, Toast.LENGTH_SHORT).show();
spinner.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
spinner.setVisibility(View.GONE);
}
});
((ViewPager) view).addView(imageLayout, 0);
return imageLayout;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View container) {
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.share:
int position = pager.getCurrentItem();
switch (position) {
case 0:
image="c1";
break;
case 1:
image="c2";
break;
case 2:
image="c3";
break;
case 3:
image="c4";
break;
case 4:
image="c5";
}
//Here is my share code.... I don't know really what the problem is.
//And why this not works in Android 2.3
Uri imageUri = Uri.parse("android.resource://mypackagename/drawable/"+image);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpg");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(intent , "Share"));
break;
case R.id.back:
this.finish();
break;
}
}
}
我有另一个用于设置图像资源的类,代码如下:
public final class Constants {
public static final String[] IMAGES = new String[] {
"drawable://" + R.drawable.c1,
"drawable://" + R.drawable.c2,
"drawable://" + R.drawable.c3,
"drawable://" + R.drawable.c4,
"drawable://" + R.drawable.c5
};
private Constants() {
}
public static class Config {
public static final boolean DEVELOPER_MODE = false;
}
public static class Extra {
public static final String IMAGES = "mypackagename.IMAGES";
public static final String IMAGE_POSITION = "mypackagename.IMAGE_POSITION";
}
}