1. 程式人生 > >android分享多張圖片在ContentResolver.insert返回null時的解決方案

android分享多張圖片在ContentResolver.insert返回null時的解決方案

<pre name="code" class="java">					String path = file.getAbsolutePath();
					ContentResolver cr = mContext.getContentResolver();
					StringBuffer buff = new StringBuffer();
					buff.append("(")
						.append(Images.ImageColumns.DATA)
				        .append("=")
				        .append("'" + path + "'")
				        .append(")");
					Cursor cur = cr.query(
					        Images.Media.EXTERNAL_CONTENT_URI,
					        new String[] { Images.ImageColumns._ID },
					        buff.toString(), null, null);
					int index = 0;
					//match the index to parse the uri
					for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
					    index = cur.getColumnIndex(Images.ImageColumns._ID);
					    index = cur.getInt(index);
					}
					if (index != 0) {
					    uri = Uri.parse("content://media/external/images/media/"+ index);
					}
					//protection. 
					if(uri == null)
					{
						uri = Uri.fromFile((File)file.getContent());
					}
					uriList.add(uri); 
					
					if (MULTIPART_TYPE.equals(mimeType))
						continue;
					String singleType = FileHelper.getSharedMIMEType(file);
					if (mimeType == null) {
						mimeType = singleType;
					} else if (singleType.equals(mimeType))
						continue;
					else {
						mimeType = MULTIPART_TYPE;
					}
					Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
					intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
					intent.setType(</span>"image/*"<span style="white-space:pre">);
					mContext.startActivity(Intent.createChooser(intent, mContext.getText(R.string.sharing)));