为什么我可以分享文本但不分享不了图片
我以前做了一个是可以的。但是删除了。今天做的就不行了。我是调用图库里的,但是一直显示图片文件不存在。大神请指点,我是初学者。谢谢
代码如下:
重定了下onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode)
{
case PICK_CODE:
if (data != null)
{
Uri uri = data.getData();
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
pick_rul = cursor.getString(index);
cursor.close();
fenxiangtupian();
}
break;
case PICK_URL:
if (data != null)
{
Uri uri = data.getData();
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
pick_rul = cursor.getString(index);
cursor.close();
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
这里对不对?
case R.id.bt_fenxiangtupian:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent,PICK_CODE);
//自定义系统onActivityResult方法
break;
下面是压缩和分享
public void fenxiangtupian()
{
File file = new File(pick_rul);
if (!file.exists()) {
Toast.makeText(this, "不存在", Toast.LENGTH_LONG).show();
return;
}
WXImageObject ima = new WXImageObject();
ima.setImagePath(pick_rul);
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = ima;
//压缩图片
Bitmap bitmap = BitmapFactory.decodeFile(pick_rul);
Bitmap thumap = Bitmap.createScaledBitmap(bitmap,150,150,true);
//释放内存
bitmap.recycle();
msg.thumbData = bmpToByteArray(thumap,true);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.message = msg;
req.transaction = buildTransaction("image");
req.scene = cb_friends.isChecked()? SendMessageToWX.Req.WXSceneTimeline:SendMessageToWX.Req.WXSceneSession;
Toast.makeText(getApplicationContext(),String.valueOf(api.sendReq(req)),Toast.LENGTH_SHORT).show();
finish();
}