|
|
|
|
@ -27,6 +27,7 @@ import android.content.Context;
|
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
|
import android.graphics.Paint;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
|
@ -56,6 +57,9 @@ import android.widget.ImageButton;
|
|
|
|
|
import android.widget.ImageView;
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
|
import androidx.core.app.ActivityCompat;
|
|
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
|
import android.Manifest;
|
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
|
@ -73,6 +77,8 @@ import android.os.Bundle;
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import android.content.pm.ResolveInfo;
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
|
|
@ -216,6 +222,8 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
private static final int REQUEST_CODE_IMAGE_PICK = 1002;
|
|
|
|
|
/** 拍照请求码 */
|
|
|
|
|
private static final int REQUEST_CODE_CAMERA = 1003;
|
|
|
|
|
/** 相机权限请求码 */
|
|
|
|
|
private static final int REQUEST_CAMERA_PERMISSION = 1004;
|
|
|
|
|
|
|
|
|
|
/** 清单模式下的编辑文本列表 */
|
|
|
|
|
private LinearLayout mEditTextList;
|
|
|
|
|
@ -1688,11 +1696,21 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
break;
|
|
|
|
|
case REQUEST_CODE_CAMERA:
|
|
|
|
|
// 拍照结果
|
|
|
|
|
if (data != null && data.getExtras() != null) {
|
|
|
|
|
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
|
|
|
|
|
if (bitmap != null) {
|
|
|
|
|
insertImageToNote(bitmap);
|
|
|
|
|
if (resultCode == RESULT_OK) {
|
|
|
|
|
if (data != null && data.getExtras() != null) {
|
|
|
|
|
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
|
|
|
|
|
if (bitmap != null) {
|
|
|
|
|
insertImageToNote(bitmap);
|
|
|
|
|
} else {
|
|
|
|
|
showToast(R.string.error_image_capture);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
showToast(R.string.error_image_capture);
|
|
|
|
|
}
|
|
|
|
|
} else if (resultCode == RESULT_CANCELED) {
|
|
|
|
|
// 用户取消拍照,不显示错误
|
|
|
|
|
} else {
|
|
|
|
|
showToast(R.string.error_image_capture);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
@ -1714,11 +1732,15 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
switch (which) {
|
|
|
|
|
case 0:
|
|
|
|
|
// 拍照
|
|
|
|
|
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
|
|
|
|
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
|
|
|
|
|
startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA);
|
|
|
|
|
if (ContextCompat.checkSelfPermission(NoteEditActivity.this, Manifest.permission.CAMERA)
|
|
|
|
|
!= PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
// 请求相机权限
|
|
|
|
|
ActivityCompat.requestPermissions(NoteEditActivity.this,
|
|
|
|
|
new String[]{Manifest.permission.CAMERA},
|
|
|
|
|
REQUEST_CAMERA_PERMISSION);
|
|
|
|
|
} else {
|
|
|
|
|
showToast(R.string.error_image_selection);
|
|
|
|
|
// 权限已授予,启动相机
|
|
|
|
|
launchCamera();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
@ -1734,6 +1756,71 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
builder.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 启动相机
|
|
|
|
|
*/
|
|
|
|
|
private void launchCamera() {
|
|
|
|
|
Log.d(TAG, "Attempting to launch camera");
|
|
|
|
|
|
|
|
|
|
// 检查相机权限状态
|
|
|
|
|
int permissionStatus = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
|
|
|
|
|
Log.d(TAG, "Camera permission status: " + permissionStatus);
|
|
|
|
|
|
|
|
|
|
// 尝试使用不同的相机 Intent 构造方式
|
|
|
|
|
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
|
|
|
|
|
|
|
|
|
// 检查是否有应用可以处理相机 Intent
|
|
|
|
|
PackageManager packageManager = getPackageManager();
|
|
|
|
|
if (packageManager != null) {
|
|
|
|
|
Log.d(TAG, "PackageManager obtained successfully");
|
|
|
|
|
|
|
|
|
|
// 方法1:使用 resolveActivity
|
|
|
|
|
if (cameraIntent.resolveActivity(packageManager) != null) {
|
|
|
|
|
Log.d(TAG, "Camera intent resolved successfully using resolveActivity");
|
|
|
|
|
try {
|
|
|
|
|
startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA);
|
|
|
|
|
return;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "Error starting camera: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Log.d(TAG, "Camera intent not resolved using resolveActivity");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 方法2:使用 queryIntentActivities
|
|
|
|
|
List<ResolveInfo> activities = packageManager.queryIntentActivities(cameraIntent, 0);
|
|
|
|
|
if (activities != null && !activities.isEmpty()) {
|
|
|
|
|
Log.d(TAG, "Camera intent resolved successfully using queryIntentActivities, found " + activities.size() + " activities");
|
|
|
|
|
try {
|
|
|
|
|
startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA);
|
|
|
|
|
return;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "Error starting camera: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Log.d(TAG, "Camera intent not resolved using queryIntentActivities");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "Failed to obtain PackageManager");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 尝试使用更具体的相机 Intent
|
|
|
|
|
Intent explicitCameraIntent = new Intent();
|
|
|
|
|
explicitCameraIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
|
|
|
|
|
explicitCameraIntent.addCategory(Intent.CATEGORY_DEFAULT);
|
|
|
|
|
try {
|
|
|
|
|
Log.d(TAG, "Attempting to start camera with explicit intent");
|
|
|
|
|
startActivityForResult(explicitCameraIntent, REQUEST_CODE_CAMERA);
|
|
|
|
|
return;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "Error starting camera with explicit intent: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 所有方法都失败,显示错误提示
|
|
|
|
|
Log.e(TAG, "All attempts to launch camera failed");
|
|
|
|
|
showToast(R.string.error_image_selection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理从相册选择的图片
|
|
|
|
|
*/
|
|
|
|
|
@ -1852,4 +1939,21 @@ public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
private void showToast(int resId, int duration) {
|
|
|
|
|
Toast.makeText(this, resId, duration).show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理权限请求结果
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
|
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
|
|
if (requestCode == REQUEST_CAMERA_PERMISSION) {
|
|
|
|
|
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
// 权限授予成功,启动相机
|
|
|
|
|
launchCamera();
|
|
|
|
|
} else {
|
|
|
|
|
// 权限被拒绝,显示错误提示
|
|
|
|
|
showToast(R.string.error_camera_permission);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|