Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

Commit

Permalink
Use content uri instead of file uri for taking photo
Browse files Browse the repository at this point in the history
  • Loading branch information
seven332 committed May 8, 2017
1 parent e5d1c0f commit 5e80f60
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 128 deletions.
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@
android:configChanges="screenSize|uiMode|orientation"
android:screenOrientation="unspecified"/>

<provider
android:name="com.hippo.nimingban.content.UniversalProvider"
android:authorities="com.hippo.nimingban.universal"
android:grantUriPermissions="true"
android:exported="false"/>

<provider
android:name="com.hippo.nimingban.content.ImageProvider"
android:authorities="com.hippo.nimingban.image"
Expand Down
132 changes: 5 additions & 127 deletions app/src/main/java/com/hippo/nimingban/content/ImageProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,135 +20,25 @@
* Created by Hippo on 11/11/2016.
*/

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.webkit.MimeTypeMap;

import com.hippo.nimingban.util.Settings;
import com.hippo.unifile.UniFile;

import java.io.FileNotFoundException;
import java.util.List;

public class ImageProvider extends ContentProvider {
public class ImageProvider extends SimpleFileProvider {

private static final String AUTHORITY = "com.hippo.nimingban.image";

private static final String[] COLUMNS = {
OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE };

@Override
public boolean onCreate() {
return true;
}

@Nullable
@Override
public Cursor query(@NonNull Uri uri, String[] projection,
String selection, String[] selectionArgs, String sortOrder) {
final UniFile file = getFileForUri(uri);
if (file == null) {
return null;
}

if (projection == null) {
projection = COLUMNS;
}

String[] cols = new String[projection.length];
Object[] values = new Object[projection.length];
int i = 0;
for (String col : projection) {
if (OpenableColumns.DISPLAY_NAME.equals(col)) {
cols[i] = OpenableColumns.DISPLAY_NAME;
values[i++] = file.getName();
} else if (OpenableColumns.SIZE.equals(col)) {
cols[i] = OpenableColumns.SIZE;
values[i++] = file.length();
} else if (MediaStore.MediaColumns.DATA.equals(col)) {
Uri originUri = file.getUri();
if (ContentResolver.SCHEME_FILE.equals(originUri.getScheme())) {
cols[i] = MediaStore.MediaColumns.DATA;
values[i++] = file.getUri().getPath();
} else {
// TODO handle document tree url
}
}
}

cols = copyOf(cols, i);
values = copyOf(values, i);

final MatrixCursor cursor = new MatrixCursor(cols, 1);
cursor.addRow(values);
return cursor;
}

@Nullable
@Override
public String getType(@NonNull Uri uri) {
final UniFile file = getFileForUri(uri);
if (file != null) {
final String name = file.getUri().getPath();
if (name != null) {
final int lastDot = name.lastIndexOf('.');
if (lastDot >= 0) {
final String extension = name.substring(lastDot + 1);
final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
if (mime != null) {
return mime;
}
}
}
}

return "application/octet-stream";
}

@Nullable
@Override
public Uri insert(@NonNull Uri uri, ContentValues values) {
throw new UnsupportedOperationException();
}

@NonNull
@Override
public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
throw new UnsupportedOperationException();
public String getAuthority() {
return AUTHORITY;
}

@Override
public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) {
throw new UnsupportedOperationException();
}

@Override
public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException {
UniFile file = getFileForUri(uri);
if (file != null) {
ParcelFileDescriptor pfd = getContext().getContentResolver().openFileDescriptor(file.getUri(), mode);
if (pfd != null) {
return pfd;
}
}
throw new FileNotFoundException("Can't find " + uri);
}

public UniFile getFileForUri(Uri uri) {
// Check authority
if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()) ||
!AUTHORITY.equals(uri.getAuthority())) {
return null;
}

public UniFile getFile(Uri uri) {
// Get dir
UniFile dir = Settings.getImageSaveLocation();
if (dir == null) {
Expand All @@ -171,16 +61,4 @@ public static Uri buildUri(String filename) {
.appendPath(filename)
.build();
}

private static String[] copyOf(String[] original, int newLength) {
final String[] result = new String[newLength];
System.arraycopy(original, 0, result, 0, newLength);
return result;
}

private static Object[] copyOf(Object[] original, int newLength) {
final Object[] result = new Object[newLength];
System.arraycopy(original, 0, result, 0, newLength);
return result;
}
}
171 changes: 171 additions & 0 deletions app/src/main/java/com/hippo/nimingban/content/SimpleFileProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
* Copyright 2017 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hippo.nimingban.content;

/*
* Created by Hippo on 5/8/2017.
*/

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.webkit.MimeTypeMap;
import com.hippo.unifile.UniFile;
import java.io.FileNotFoundException;

public abstract class SimpleFileProvider extends ContentProvider {

//private static final String AUTHORITY = "com.hippo.nimingban.image";

private static final String[] COLUMNS = {
OpenableColumns.DISPLAY_NAME,
OpenableColumns.SIZE
};

@Override
public boolean onCreate() {
return true;
}

@Nullable
@Override
public Cursor query(@NonNull Uri uri, String[] projection,
String selection, String[] selectionArgs, String sortOrder) {
final UniFile file = getFileForUri(uri);
if (file == null) {
return null;
}

if (projection == null) {
projection = COLUMNS;
}

String[] cols = new String[projection.length];
Object[] values = new Object[projection.length];
int i = 0;
for (String col : projection) {
if (OpenableColumns.DISPLAY_NAME.equals(col)) {
cols[i] = OpenableColumns.DISPLAY_NAME;
values[i++] = file.getName();
} else if (OpenableColumns.SIZE.equals(col)) {
cols[i] = OpenableColumns.SIZE;
values[i++] = file.length();
} else if (MediaStore.MediaColumns.DATA.equals(col)) {
Uri originUri = file.getUri();
if (ContentResolver.SCHEME_FILE.equals(originUri.getScheme())) {
cols[i] = MediaStore.MediaColumns.DATA;
values[i++] = file.getUri().getPath();
} else {
// TODO handle document tree url
}
}
}

cols = copyOf(cols, i);
values = copyOf(values, i);

final MatrixCursor cursor = new MatrixCursor(cols, 1);
cursor.addRow(values);
return cursor;
}

@Nullable
@Override
public String getType(@NonNull Uri uri) {
final UniFile file = getFileForUri(uri);
if (file != null) {
final String name = file.getUri().getPath();
if (name != null) {
final int lastDot = name.lastIndexOf('.');
if (lastDot >= 0) {
final String extension = name.substring(lastDot + 1);
final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
if (mime != null) {
return mime;
}
}
}
}

return "application/octet-stream";
}

@Nullable
@Override
public Uri insert(@NonNull Uri uri, ContentValues values) {
throw new UnsupportedOperationException();
}

@Override
public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
throw new UnsupportedOperationException();
}

@Override
public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) {
throw new UnsupportedOperationException();
}

@Override
public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException {
UniFile file = getFileForUri(uri);
if (file != null) {
ParcelFileDescriptor pfd = getContext().getContentResolver().openFileDescriptor(file.getUri(), mode);
if (pfd != null) {
return pfd;
}
}
throw new FileNotFoundException("Can't find " + uri);
}

@NonNull
public abstract String getAuthority();

@Nullable
public abstract UniFile getFile(Uri uri);

@Nullable
private UniFile getFileForUri(Uri uri) {
// Check authority
if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()) &&
getAuthority().equals(uri.getAuthority())) {
return getFile(uri);
} else {
return null;
}
}

private static String[] copyOf(String[] original, int newLength) {
final String[] result = new String[newLength];
System.arraycopy(original, 0, result, 0, newLength);
return result;
}

private static Object[] copyOf(Object[] original, int newLength) {
final Object[] result = new Object[newLength];
System.arraycopy(original, 0, result, 0, newLength);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2017 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hippo.nimingban.content;

/*
* Created by Hippo on 5/8/2017.
*/

import android.content.ContentResolver;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.hippo.unifile.UniFile;
import java.io.File;

public class UniversalProvider extends SimpleFileProvider {

private static final String AUTHORITY = "com.hippo.nimingban.universal";

@NonNull
@Override
public String getAuthority() {
return AUTHORITY;
}

@Nullable
@Override
public UniFile getFile(Uri uri) {
File file = new File(uri.getPath());
return UniFile.fromFile(file);
}

public static Uri buildUri(String filePath) {
return new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(AUTHORITY)
.appendPath(filePath)
.build();
}
}
Loading

0 comments on commit 5e80f60

Please sign in to comment.