gpt4 book ai didi

Android 图片选择器

转载 作者:行者123 更新时间:2023-11-29 01:41:20 25 4
gpt4 key购买 nike

我写了一段代码在Android中选择图片,并在创建drawable文件夹后将某些图片复制到res/drawable文件夹,而没有将内容复制到其他drawable文件夹。

这是我的代码。

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.imagepicker.MainActivity"
tools:ignore="MergeRootFrame" >

<Button
android:id="@+id/btnPick"
android:onClick="pickImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:text="Load Image" />

<ImageView
android:id="@+id/imgView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/btnPick"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />

</RelativeLayout>

MainActivity.java

package com.example.imagepicker;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
//Image loading result to pass to startActivityForResult method
public static final int LOAD_IMAGE_RESULTS = 1;
private Bitmap bitmap;

//GUI components
private Button button; //the button
private ImageView image; //the imageview

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Find references to the GUI objects
button = (Button)findViewById(R.id.btnPick);
image = (ImageView)findViewById(R.id.imgView);

//Set button's onClick listener object
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
//create the intent for ImageGallery
Intent i = new Intent(Intent.ACTION_PICK);
i.setType("image/*");

//Start new activity with the LOAD_IMAGE_RESULT to handle back the result when the image is picked from the Image Gallery
startActivityForResult(i, LOAD_IMAGE_RESULTS);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

InputStream ImageStream = null;

/*Checking if the activity that was triggered was the ImageGallery
If so then requestCode will match the LOAD_IMAGE_RESULTS value
If the resultCode is RESULT_OK &
There is some data that we know that image was picked*/
if (requestCode == LOAD_IMAGE_RESULTS && resultCode == Activity.RESULT_OK && data != null) {
try {
//Let's read the picked image -its URI
Uri pickedImage = data.getData();

//Let's read the image path using content resolver
ImageStream = getContentResolver().openInputStream(pickedImage);

//Now let's set the GUI ImageView data with data read from the picked file
Bitmap selectedImage = BitmapFactory.decodeStream(ImageStream);
}

catch(FileNotFoundException e) {
e.printStackTrace();
}

finally {
if (ImageStream != null) {
try {
ImageStream.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
}
}
}

在运行时,当我点击按钮加载图像时,我收到一条消息“NO MEDIA FOUND”。

这是怎么回事?我已将图像复制到 res/drawable 文件夹,而不是其他 drawable 文件夹。错了吗?

最佳答案

NO MEDIA FOUND 表示您的图库中没有图片。通过在您的设备(手机)中放置任何图像都可以解决此问题。

您正在使用以下代码加载/选择图像。

//create the intent for ImageGallery
Intent i = new Intent(Intent.ACTION_PICK);
i.setType("image/*");

代码不会从 res/drawable 加载图像。它将从您的设备/图库加载图像。

如果你想从 drawable 文件夹中加载图像并想选择一个。然后你负责(编写代码)加载图像并挑选(选择)一个

关于Android 图片选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24473459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com