![]() |
可爱的长颈鹿 · 导致慢SQL的原因和解决方案_云数据库 ...· 1 月前 · |
![]() |
骑白马的饭卡 · 苹果手机微信聊天记录怎么恢复?4招搞定,建议 ...· 4 月前 · |
![]() |
风度翩翩的面包 · 《蜘蛛侠:英雄无归》首映现场观众反应 ...· 1 年前 · |
![]() |
烦恼的野马 · 我的后宫靠�%8 - 搜狗图片搜索· 1 年前 · |
![]() |
安静的羽毛球 · 零跑C11高配低价,对标宋plus ...· 1 年前 · |
![]() |
鼻子大的红酒 · 梁朝伟生活曝光:又破圈!没想到他是这样的男神 ...· 1 年前 · |
![]() |
博学的鸵鸟
4 月前 |
当我有时间在什么应用上分享图片的时候,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;
}
我的图片在布局文件夹与保存到sdcard,我想在这里分享,我给我的代码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;
}
}
发布于 2013-11-23 07:32:32
在这一行中,您必须传递保存的映像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());
发布于 2013-11-23 07:40:10
在单击侦听器的
share
中,您在下面写了这一行,这一行是错误:-
share.putExtra(Intent.EXTRA_STREAM,scaleDownBitmap(mBitmap, 90, FullImageActivity.this));
在这种情况下,您将调用返回位图的
scaleDownBtmap
函数。现在,inten.putExtra方法不使用位图参数,这就是为什么要获得类强制转换异常。您需要在字节流中转换位图,然后在put额外方法中传递它。位图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);
![]() |
烦恼的野马 · 我的后宫靠�%8 - 搜狗图片搜索 1 年前 |