Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for file uri #847

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Handler;
import android.os.ParcelFileDescriptor;

import com.otaliastudios.cameraview.controls.Facing;
import com.otaliastudios.cameraview.engine.mappers.Camera1Mapper;
Expand Down Expand Up @@ -98,6 +100,34 @@ public static File writeToFile(@NonNull final byte[] data, @NonNull File file) {
}
}

@SuppressWarnings("WeakerAccess")
@Nullable
@WorkerThread
@SuppressLint("NewApi")
public static Uri writeToFile(Context context, @NonNull final byte[] data, @NonNull Uri file) {
OutputStream stream = null;
Uri result;
try {
ParcelFileDescriptor fd = context.getContentResolver().openFileDescriptor(file, "w");
stream = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
stream.write(data);
stream.flush();
result = file;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
try {
if (stream != null) {
stream.close();
}
} catch (Exception ignored) {

}
}
return result;
}


/**
* Writes the given data to the given file in a background thread, returning on the
Expand Down Expand Up @@ -128,6 +158,26 @@ public void run() {
});
}

@SuppressWarnings("WeakerAccess")
public static void writeToFile(final Context context,
@NonNull final byte[] data,
@NonNull final Uri file,
@NonNull final FileUriCallback callback) {
final Handler ui = new Handler();
WorkerHandler.execute(new Runnable() {
@Override
public void run() {
final Uri result = writeToFile(context, data, file);
ui.post(new Runnable() {
@Override
public void run() {
callback.onFileReady(result);
}
});
}
});
}

/**
* Decodes an input byte array and outputs a Bitmap that is ready to be displayed.
* The difference with {@link android.graphics.BitmapFactory#decodeByteArray(byte[], int, int)}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.otaliastudios.cameraview;

import android.net.Uri;

import androidx.annotation.Nullable;
import androidx.annotation.UiThread;

public interface FileUriCallback {
/**
* Notifies that the data was succesfully written to file.
* This is run on the UI thread.
* Returns a null object if an exception was encountered, for example
* if you don't have permissions to write to file.
*
* @param fileUri the written file, or null
*/
@UiThread
void onFileReady(@Nullable Uri fileUri);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.otaliastudios.cameraview;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.net.Uri;
import android.os.Build;

import com.otaliastudios.cameraview.controls.Facing;
Expand Down Expand Up @@ -172,4 +174,8 @@ public void toBitmap(@NonNull BitmapCallback callback) {
public void toFile(@NonNull File file, @NonNull FileCallback callback) {
CameraUtils.writeToFile(getData(), file, callback);
}

public void toFile(Context context, @NonNull Uri fileUri, @NonNull FileUriCallback callback) {
CameraUtils.writeToFile(context, getData(), fileUri, callback);
}
}