相关疑难解决方法(0)

如何确定android中的MIME类型?

假设我有一个完整的文件路径,如:(/ sdcard/tlogo.png).我想知道它的mime类型.

我为它创建了一个函数

public static String getMimeType(File file, Context context)    
{
    Uri uri = Uri.fromFile(file);
    ContentResolver cR = context.getContentResolver();
    MimeTypeMap mime = MimeTypeMap.getSingleton();
    String type = mime.getExtensionFromMimeType(cR.getType(uri));
    return type;
}
Run Code Online (Sandbox Code Playgroud)

但是当我调用它时,它返回null.

File file = new File(filePath);
String fileType=CommonFunctions.getMimeType(file, context);
Run Code Online (Sandbox Code Playgroud)

filesystems android mime-types

162
推荐指数
11
解决办法
12万
查看次数

从图库或相机中选取的图像中获取文件扩展名,作为字符串

我想得到从图库加载或从相机中拾取的图像的图像扩展名(例如"jpg","png","bmp"ecc.).

我已经使用此表单中的方法从库中加载图像

    private static final int SELECT_PICTURE_ACTIVITY_REQUEST_CODE = 0;
....
private void selectPicture() {
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType("image/*");
    startActivityForResult(intent, SELECT_PICTURE_ACTIVITY_REQUEST_CODE);
}
....
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    switch (requestCode) {
        case SELECT_PICTURE_ACTIVITY_REQUEST_CODE:
            if (resultCode == RESULT_OK) {
                Uri selectedImage = imageReturnedIntent.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                if (cursor.moveToFirst()) {
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String filePath = cursor.getString(columnIndex);
                    Bitmap bitmap = BitmapFactory.decodeFile(filePath);
                    .........
                } …
Run Code Online (Sandbox Code Playgroud)

android android-gallery android-camera

31
推荐指数
7
解决办法
4万
查看次数

如何从uri确定文件的文件扩展名

假设我有一个URI,并且我想找到返回的文件的文件扩展名,我在Java中需要做什么.

例如,http : //www.daml.org/2001/08/baseball/baseball-ont上的文件是http://www.daml.org/2001/08/baseball/baseball-ont.owl

当我做

    URI uri = new URI(address); 
    URL url = uri.toURL();
    String file = url.getFile();
    System.out.println(file);
Run Code Online (Sandbox Code Playgroud)

我无法看到带.owl扩展名的完整文件名,/2001/08/baseball/baseball-ont我也是如何获得文件扩展名的.``

java url uri file

18
推荐指数
5
解决办法
5万
查看次数

Android - 如何使用 OkHTTP 分块上传视频?

请查看我用于将视频上传到服务器的以下代码。但是,对于足够大的视频,我收到 OutOfMemory 异常。

        InputStream stream = getContentResolver().openInputStream(videoUri);
        byte[] byteArray = IOUtils.toByteArray(stream);
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", "fname",
                        RequestBody.create(MediaType.parse("video/mp4"), byteArray))
                .build();
        Request request = new Request.Builder()
                .url(uploadURL)
                .post(requestBody)
                .build();

        OkHttpClient client = new OkHttpClient.Builder().build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
            }
        });
Run Code Online (Sandbox Code Playgroud)

有人可以指出我如何避免 OutOfMemory 异常的正确方向吗?有没有办法可以从 InputStream 转到 requestBody?

video android okhttp

5
推荐指数
1
解决办法
4179
查看次数