Skip to content
Snippets Groups Projects
Unverified Commit a1c70834 authored by i-robot's avatar i-robot Committed by Gitee
Browse files

!3162 [MS][LITE][DOC]android app camera and save button not working functional.

Merge pull request !3162 from HuKang/master
parents 472c21a6 5e96f15d
No related branches found
No related tags found
No related merge requests found
......@@ -16,6 +16,7 @@
package com.mindspore.imagesegmentation.help;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
......@@ -27,7 +28,6 @@ import android.media.ExifInterface;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
......@@ -36,11 +36,13 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class BitmapUtils {
private static final String TAG = "BitmapUtils";
private static final int FULL_QUALITY = 100;
public static void recycleBitmap(Bitmap... bitmaps) {
for (Bitmap bitmap : bitmaps) {
......@@ -242,58 +244,20 @@ public class BitmapUtils {
return inputImage;
}
// Save the picture to the system album and refresh it.
public static void saveToAlbum(final Context context, Bitmap bitmap) {
File file = null;
String fileName = System.currentTimeMillis() + ".jpg";
File root = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), context.getPackageName());
File dir = new File(root, "image");
if (dir.mkdirs() || dir.isDirectory()) {
file = new File(dir, fileName);
}
FileOutputStream os = null;
Uri saveUri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
OutputStream outputStream = null;
try {
os = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
outputStream = context.getContentResolver().openOutputStream(saveUri);
} catch (FileNotFoundException e) {
Log.e(TAG, e.getMessage());
} catch (IOException e) {
Log.e(TAG, e.getMessage());
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
}
if (file == null) {
return;
e.printStackTrace();
}
// Gallery refresh.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
String path = null;
try {
path = file.getCanonicalPath();
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
MediaScannerConnection.scanFile(context, new String[]{path}, null,
new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(uri);
context.sendBroadcast(mediaScanIntent);
}
});
} else {
String relationDir = file.getParent();
File file1 = new File(relationDir);
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(file1.getAbsoluteFile())));
if (bitmap.compress(Bitmap.CompressFormat.JPEG, FULL_QUALITY, outputStream)) {
Intent intent = new Intent();
intent.setData(saveUri);
context.sendBroadcast(intent);
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, saveUri));
}
}
}
......@@ -15,6 +15,7 @@
*/
package com.mindspore.styletransferdemo;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
......@@ -22,26 +23,24 @@ import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.provider.MediaStore;
import androidx.annotation.NonNull;
import androidx.exifinterface.media.ExifInterface;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class ImageUtils {
private static final String TAG = "ImageUtils";
private static final int FULL_QUALITY = 100;
private static Matrix decodeExifOrientation(int orientation) {
Matrix matrix = new Matrix();
......@@ -211,56 +210,19 @@ public class ImageUtils {
// Save the picture to the system album and refresh it.
public static void saveToAlbum(final Context context, Bitmap bitmap) {
File file = null;
String fileName = System.currentTimeMillis() + ".jpg";
File root = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), context.getPackageName());
File dir = new File(root, "image");
if (dir.mkdirs() || dir.isDirectory()) {
file = new File(dir, fileName);
}
FileOutputStream os = null;
Uri saveUri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
OutputStream outputStream = null;
try {
os = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
outputStream = context.getContentResolver().openOutputStream(saveUri);
} catch (FileNotFoundException e) {
Log.e(TAG, e.getMessage());
} catch (IOException e) {
Log.e(TAG, e.getMessage());
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
}
if (file == null) {
return;
e.printStackTrace();
}
// Gallery refresh.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
String path = null;
try {
path = file.getCanonicalPath();
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
MediaScannerConnection.scanFile(context, new String[]{path}, null,
new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(uri);
context.sendBroadcast(mediaScanIntent);
}
});
} else {
String relationDir = file.getParent();
File file1 = new File(relationDir);
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(file1.getAbsoluteFile())));
if (bitmap.compress(Bitmap.CompressFormat.JPEG, FULL_QUALITY, outputStream)) {
Intent intent = new Intent();
intent.setData(saveUri);
context.sendBroadcast(intent);
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, saveUri));
}
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment