第一种方法:
String path = "file:///android_asset/文件名";
第二种方法:
InputStream abpath = getClass()getResourceAsStream("/assets/文件名");
若要想要转换成String类型
String path = new String(InputStreamToByte(abpath ));
private byte[] InputStreamToByte(InputStream is) throws IOException {
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = isread()) != -1) {
bytestreamwrite(ch);
}
byte imgdata[] = bytestreamtoByteArray();
bytestreamclose();
return imgdata;
}
public class TestCameraActivity extends Activity implements OnClickListener{ private Uri mOutPutFileUri;
@Override
public void onCreate(Bundle savedInstanceState) {
superonCreate(savedInstanceState);
setContentView(Rlayoutmain);
Button takePiCButton = (Button) thisfindViewById(Ridbutton1);
takePiCButtonsetOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (vgetId()) {
case Ridbutton1:
saveFullImage();
break;
default:
break;
}
}
private void saveFullImage(){
Intent intent = new Intent(MediaStoreACTION_IMAGE_CAPTURE);
//文件夹aaaa
String path = EnvironmentgetExternalStorageDirectory()toString()+"/aaaa";
File path1 = new File(path);
if(!path1exists()){
path1mkdirs();
}
File file = new File(path1,SystemcurrentTimeMillis()+"jpg");
mOutPutFileUri = UrifromFile(file);
intentputExtra(MediaStoreEXTRA_OUTPUT, mOutPutFileUri);
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
superonActivityResult(requestCode, resultCode, data);
if(requestCode == 1){
Uri imageUri = null;
if(data != null){
if(datahasExtra("data")){
Bitmap thunbnail = datagetParcelableExtra("data");
//处理缩略图
}
}else{
//处理mOutPutFileUri中的完整图像
}
}
}
}
直接调用文件管理器选择即可。
1、调用系统提供的选择器,代码如下:
//注意,在Android44系统下建议使用 IntentACTION_OPEN_DOCUMENT方式
if (UtilityisKK()) {
Intent intent = new Intent(IntentACTION_OPEN_DOCUMENT);
intentaddCategory(IntentCATEGORY_OPENABLE);
intentsetType("image
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
处理返回结果:
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
superonActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case PIC_RESULT://选择图库
case PIC_RESULT_KK:
imageFileUri = intentgetData();//获取选择的URI
break;
}
}
2、除此自外,系统还提供一种选择器,这个选择器可以屏蔽掉那个auto backup的目录所以就开始打算用这个选择器来选了
Intent intent=new Intent(IntentACTION_GET_CONTENT);//ACTION_OPEN_DOCUMENT
intentaddCategory(IntentCATEGORY_OPENABLE);
intentsetType("image/jpeg");
if(androidosBuildVERSIONSDK_INT>=androidosBuildVERSION_CODESKITKAT){
startActivityForResult(intent, SELECT_PIC_KITKAT);
}else{
startActivityForResult(intent, SELECT_PIC);
}
为什么要分开不同版本呢其实在43或以下可以直接用ACTION_GET_CONTENT的,在44或以上,官方建议用ACTION_OPEN_DOCUMENT,主要区别是他们返回的Uri43返回的是带文件路径的,而44返回的却是content://comandroidprovidersmediadocuments/document/image:3951这样的,没有路径,只有编号的uri可以通过以下方式,处理URI。
参考:Android 44从图库选择,获取路径并裁剪
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = BuildVERSIONSDK_INT >= BuildVERSION_CODESKITKAT;
// DocumentProvider
if (isKitKat && DocumentsContractisDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContractgetDocumentId(uri);
final String[] split = docIdsplit(":");
final String type = split[0];
if ("primary"equalsIgnoreCase(type)) {
return EnvironmentgetExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContractgetDocumentId(uri);
final Uri contentUri = ContentUriswithAppendedId(
Uriparse("content://downloads/public_downloads"), LongvalueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContractgetDocumentId(uri);
final String[] split = docIdsplit(":");
final String type = split[0];
Uri contentUri = null;
if ("image"equals(type)) {
contentUri = MediaStoreImagesMediaEXTERNAL_CONTENT_URI;
} else if ("video"equals(type)) {
contentUri = MediaStoreVideoMediaEXTERNAL_CONTENT_URI;
} else if ("audio"equals(type)) {
contentUri = MediaStoreAudioMediaEXTERNAL_CONTENT_URI;
}
final String selection = "_id=";
final String[] selectionArgs = new String[] {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content"equalsIgnoreCase(urigetScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return urigetLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file"equalsIgnoreCase(urigetScheme())) {
return urigetPath();
}
return null;
}
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = contextgetContentResolver()query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursormoveToFirst()) {
final int index = cursorgetColumnIndexOrThrow(column);
return cursorgetString(index);
}
} finally {
if (cursor != null)
cursorclose();
}
return null;
}
public static boolean isExternalStorageDocument(Uri uri) {
return "comandroidexternalstoragedocuments"equals(urigetAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "comandroidprovidersdownloadsdocuments"equals(urigetAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "comandroidprovidersmediadocuments"equals(urigetAuthority());
}
public static boolean isGooglePhotosUri(Uri uri) {
return "comgoogleandroidappsphotoscontent"equals(urigetAuthority());
}
3、使用其它开源组件如PhotoView。
以上就是关于如何获得android assert 里图片的绝对路径全部的内容,包括:如何获得android assert 里图片的绝对路径、android 调用系统照相机拍照后获得图片路径、android怎么从sd卡指定的文件夹中获取所有图片的路径URL,谢谢~感谢各位大神了等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)