什么时候我分享图像应用程序facebook等。它无法归档读取和抛出异常引起:
java.lang.ClassCastException: android.graphics.Bitmap cannot be cast to android.net.Uri
11-23 12:32:02.835: E/AndroidRuntime(11662): at com.whatsapp.ContactPicker.d(ContactPicker.java:531)
11-23 12:32:02.835: E/AndroidRuntime(11662): at com.whatsapp.ContactPicker.onCreate(ContactPicker.java:306)
11-23 12:32:02.835: E/AndroidRuntime(11662): at android.app.Activity.performCreate(Activity.java:4479)
11-23 12:32:02.835: E/AndroidRuntime(11662): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
11-23 12:32:02.835: E/AndroidRuntime(11662): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)
我的活动:
public class FullImageActivity extends Activity {
ImageView imageView;
Button buttonsetwallpaper, share;
String sp_items[] = new String[] { "Setwallpaper", "Share", "Exit" };
Spinner spinner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
buttonsetwallpaper = (Button) findViewById(R.id.wallpaper);
buttonsetwallpaper.getBackground().setColorFilter(0xFF00FF00,
PorterDuff.Mode.MULTIPLY);
share = (Button) findViewById(R.id.button2);
// get intent data
Intent i = getIntent();
final int position = i.getExtras().getInt("id");
final ImageAdapter imageAdapter = new ImageAdapter(this);
imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
share.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// Bitmap o = BitmapFactory.decodeFile("/sdcard/." + img_name);
// Bitmap o =
// BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/wallpaperapp/");
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),
imageAdapter.mThumbIds[position]);
// Bitmap mBitmap =
// BitmapFactory.decodeResource(getResources(),(int)i.getItemId(position));
try {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
System.out.println("*********************" + mBitmap);
share.putExtra(
Intent.EXTRA_STREAM,
scaleDownBitmap(mBitmap, 90, FullImageActivity.this));
startActivity(Intent.createChooser(share, "Share Image"));
} catch (Exception e) {
e.printStackTrace();
// Toast.makeText(FullImageActivity.this,
// "Error setting wallpaper", Toast.LENGTH_SHORT).show();
}
}
});
buttonsetwallpaper.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// Bitmap o = BitmapFactory.decodeFile("/sdcard/." + img_name);
// Bitmap o =
// BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/wallpaperapp/");
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),
imageAdapter.mThumbIds[position]);
// Bitmap mBitmap =
// BitmapFactory.decodeResource(getResources(),(int)i.getItemId(position));
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getApplicationContext());
try {
myWallpaperManager.setBitmap(mBitmap);
Toast.makeText(FullImageActivity.this, "Wallpaper set",
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(FullImageActivity.this,
"Error setting wallpaper", Toast.LENGTH_SHORT)
.show();
}
}
});
}
Bitmap scaleDownBitmap(Bitmap photo, int newHeight, Context context) {
final float densityMultiplier = context.getResources()
.getDisplayMetrics().density;
int h = (int) (newHeight * densityMultiplier);
int w = (int) (h * photo.getWidth() / ((double) photo.getHeight()));
photo = Bitmap.createScaledBitmap(photo, w, h, true);
return photo;
}
我的图像在布局文件夹中保存到SD卡我想在这里分享我给我的代码PLZ帮助我。
public class ImageAdapter extends BaseAdapter {
private Context mContext;
ImageView imageView;
// Keep all Images in array
public Integer[] mThumbIds = { R.drawable.a1, R.drawable.a2, R.drawable.a3,
R.drawable.a10, R.drawable.a11, R.drawable.a12, R.drawable.a13,
R.drawable.a14, R.drawable.a15, R.drawable.a16, R.drawable.a17,
R.drawable.a18, R.drawable.a19, R.drawable.a20, R.drawable.a21,
R.drawable.a22, R.drawable.a23, R.drawable.a24, R.drawable.a25,
R.drawable.a26, R.drawable.a27, R.drawable.a28, R.drawable.a30,
R.drawable.a31, R.drawable.a32, R.drawable.a33, R.drawable.a34,
R.drawable.a35, R.drawable.a36, R.drawable.a37,
R.drawable.a38, R.drawable.a39, R.drawable.a4, R.drawable.a40,
R.drawable.a41, R.drawable.a42, R.drawable.a43, R.drawable.a5,
R.drawable.a6, R.drawable.a7, R.drawable.a8, R.drawable.a9,
R.drawable.aa9
};
// 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;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(250, 300));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(4, 4, 4, 4);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}
答案 0 :(得分:2)
在此行中,您必须传递已保存的图像uri而不是Bitamp
share.putExtra(Intent.EXTRA_STREAM,
scaleDownBitmap(mBitmap, 90, FullImageActivity.this));
像这样改变..
Bitmap icon=scaleDownBitmap(mBitmap, 90, FullImageActivity.this);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg")
screenshotUri是您图片的Uri路径。
另一种方式是..
Bitmap icon=scaleDownBitmap(mBitmap, 90, FullImageActivity.this);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
share.putExtra(Intent.EXTRA_STREAM, bytes.toByteArray());
答案 1 :(得分:0)
在点击监听器的share
中,您在下面写了这一行,这一行是错误: -
share.putExtra(Intent.EXTRA_STREAM,scaleDownBitmap(mBitmap, 90, FullImageActivity.this));
在此,您正在调用返回BITMAP的scaleDownBtmap
函数。现在,inten.putExtra方法没有采用Bitmap Argument,这就是为什么你得到那个类转换异常。您需要在字节流中转换位图,然后在put extra方法中传递它。
位图scaleDownBitmap(位图照片,int newHeight,上下文上下文){
final float densityMultiplier = context.getResources()
.getDisplayMetrics().density;
int h = (int) (newHeight * densityMultiplier);
int w = (int) (h * photo.getWidth() / ((double) photo.getHeight()));
photo = Bitmap.createScaledBitmap(photo, w, h, true);
return photo;
}
putExtra方法不采用Bitmap参数。 这样做: -
Bitmap b = scaleDownBitmap(mBitmap, 90, FullImageActivity.this); // your bitmap // use your scaleDownBitmap ere
ByteArrayOutputStream bs = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 50, bs);
share.putExtra("byteArray", bs.toByteArray());
然后在接收活动中,将此字节流转换为位图: -
Bitmap b = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);