单击时,我的应用可在相机和图库之间进行选择,然后该图片将显示在 ImageView 中。我最初尝试显示完整图像,然后尝试使用位图方式,但没有任何效果。我只是得到一个空白的图像视图。请给我一些关于我做错了什么的指导,并在必要时要求澄清:
相机/画廊照片代码:
Uri outputFileUri;
private void openImageIntent() {
// Determine Uri of camera image to save.
final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir" + File.separator);
root.mkdirs();
final String fname = "img_" + System.currentTimeMillis() + ".jpg";
final File sdImageMainDirectory = new File(root, fname);
outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
startActivityForResult(chooserIntent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent returnIntent) {
super.onActivityResult(resultCode, requestCode, returnIntent);
if(requestCode == 0) {
if(resultCode == RESULT_OK) {
final boolean isCamera;
if(returnIntent == null) {
isCamera = true;
}
else
{
final String action = returnIntent.getAction();
if(action == null) {
isCamera = false;
}
else {
isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
Uri selectedImageUri;
if(isCamera) {
selectedImageUri = outputFileUri;
mainImage.setImageURI(selectedImageUri); //trying full image
}
else {
selectedImageUri = returnIntent == null ? null : returnIntent.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
mainImage.setImageBitmap(bitmap); //trying bitmap
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
现在你可以使用毕加索图书馆:D
Picasso.with(context)
.load(item.getPhotoUri())
.resize(512,512)
.placeholder(R.drawable.noimage)
.error(R.drawable.error_image)
.into(photo);
如果你想用相机拍摄图像,你可以这样做:
public void fromCamera(){
String path= Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/";
File file = new File(path,"IMG_"+System.currentTimeMillis()+".jpg");
file.getParentFile().mkdirs();
try {
file.createNewFile();
}catch (IOException e) {
e.printStackTrace();
}
mPicCaptureUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mPicCaptureUri);
startActivityForResult(intent, REQUEST_CAMERA);
}
如果你想要图库中的图像,那么:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, getString(R.string.select_file)), REQUEST_GALLERY);
在onActivityResult上,您可以获取所选图像的图像路径,并在图像视图中设置图像。。。。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
if (requestCode == REQUEST_GALLERY && data != null && data.getData() != null) {
Uri selectedImageUri = data.getData();
//do set your image here
}else if(requestCode == REQUEST_CAMERA){
if(mPicCaptureUri!=null){
//do try to set image here
}
}
}
}
不要忘记在顶部将mPicCapture Uri定义为:
private Uri mPicCaptureUri = null;
您可以从上面的代码中获取想法..它可能会帮助你
您的问题是由于图像大小,您的ImageView无法显示图像。我用ImageView试试这个代码,如下所示
<ImageView
android:id="@+id/mainImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/abc_ab_bottom_solid_dark_holo" />
如果像这样将高度和宽度与dp一起使用
android:layout_width="100dp"
android:layout_height="100dp"
您需要压缩位图以在ImageView中显示它。编辑要转换的代码
if(isCamera) {
selectedImageUri = outputFileUri;
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);//You can use this bitmap if need full image to further use
Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap, 600 ,600, true);//this bitmap2 you can use only for display
mainImage.setImageBitmap(bitmap2); //trying full image
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
selectedImageUri = returnIntent == null ? null : returnIntent.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
bitmap = Bitmap.createScaledBitmap(bitmap, 600 ,600, true);
mainImage.setImageBitmap(bitmap); //trying bitmap
} catch (IOException e) {
e.printStackTrace();
}
}
我正在尝试将图像从SD卡设置为,但它不起作用。我做错了什么?我确定该文件存在。 这是的代码: 以下是布局的代码: : 最后,图像200 × 149像素:
Android Studio 3.4 styles.xml 在清单中: 在activity: 这里是布局: 但工具栏未显示:
问题内容: 我在程序中发现了一些问题。 我使用log4j进行日志记录, 但是,在日志文件中,所有行号都变为“?”。 对话模式如下: 问题答案: 您很可能在已编译的工件中缺少调试信息。也就是说,这不是log4j的错,您需要确保正在编译包含调试信息的东西。一个快速的测试是尝试使用您喜欢的IDE调试您的应用程序。如果没有调试信息,它将抱怨并且不会建立调试会话。
我已经制作了Hello World RCP应用程序,得到了以下类结构: 向Perspective.createInitialLayout()添加额外代码: 但不显示视图。 我将breakpoint设置到perspective.createInitialLayout()中,发现它没有执行。 我的观点声明是: ApplicationWorkbenchAdvisor.GetInitialWindowPe
我正在测试一个框架,并试图加载其他glTF文件,而不是教程中提供的示例。我从Sketchfab下载了几个glTF包文件,它们似乎没有加载。 如果我使用给定文件链接的框架,它会起作用:https://cdn.aframe.io/test-models/models/virtualcity/VC.gltf 但不是我从Sketchfab下载的。文件的路径是正确的-我有一个资产文件夹旁边的我的index.
我正在学习Spring框架。我添加了在。但当我去看的时候,它是看不见的http://localhost:8080.我的结构如下: taco cloud\src\main\java\tacos\tacoCloud应用程序。JAVA taco cloud\src\main\java\tacos\HomeController。JAVA Taco-Cloud\src\main\资源\静态\home.htm