我正在使用android.provider.ContactsContact.Contacts类的Contacts.Photo.PHOTO列名来获取电话联系人的照片。我的应用程序的最低sdk版本是10,所以当使用这个常量时,它会给我警告‘这个API在11中添加(当前是最小的10)’。
我查看了Android文档,发现API 11中添加了这个常量,但令人惊讶的是,它在安装了Android 2.3.6 (即sdk 10)的设备上工作得很好。
那件事怎么可能?
请看我的代码:
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, someContactId);
Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
Cursor cursor = getContentResolver().query(photoUri, new String[] {Contacts.Photo.PHOTO}, null, null, null);
Log.d(null, cursor.getColumnName(0)); //This returns 'data15' column.
cursor.moveToFirst(); // considering cursor is not null
Byte[] data = cursor.getBlob(0);
InputSteam inputStream = new ByteArrayInputStream(data);
Bitmap bitmapImage = BitmapFactory.decodeStream(inputStream);
imageView.setImageBitmap(bitmapImage); //imageView is of ImageView object i.e. displayed in view.我的min-min版本是10,target-min版本是10。
android.provider.ContactsContact.Intents类的Intents.Insert.DATA常量也发生了同样的事情
发布于 2016-03-09 17:59:45
Eclipse突出显示警告/错误。它还强调了其可能的解决方案。这对开发者来说是一件非常好的事情。
从那里我可以知道,当应用程序正在构建以支持API 10之后,并且如果在API级别10中没有任何API(相当于常量),那么该常量值被硬编码插入到代码中。因此,apk本身为每个API级别生成了硬编码常量。此时不应执行引用。
同样的事情也发生在Contacts.Photo.PHOTO API上。
您也可以在您的代码中尝试。请根据您的喜好使用以上API,最小sdk版本10和目标sdk版本。
https://stackoverflow.com/questions/35498252
复制相似问题