我正在尝试选择存储在当前位于Drawable文件夹中的 我的应用内的图片,因为如果我 错误点我还不知道该去哪里我在哪里。
目标:由于我不想将相应的用户的整个图片发送到服务器,而是 存储 一些图片(Zodiac Signs)让我们说图书馆图像。如果我要从图库中加载图片,如果打开此可绘制窗口以选择图片,当我点击上传图片时会打开图库窗口。
这就是我为画廊做的事情:
prof.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI );
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
Glide.with(this).load(picturePath)
.crossFade()
.thumbnail(0.5f)
.bitmapTransform(new CircleTransform(this))
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(prof);
}
}
我从中做到了什么:我只能发送将在用户屏幕上设置的 图片名称 如果他查看其他 用户个人资料 ,因为这些图片都在所有应用中。
答案 0 :(得分:-14)
不幸的是,你无法得到一个drawables列表。
但是你可以构建静态列表int []或其他一些结构
这里有两个例子:
SimpleFragment
仅使用自定义适配器拍摄图像。您需要构建imagesOnly
public static class SimpleFragment extends DialogFragment {
final static int[] imagesOnly = new int[]{R.drawable.ic_android, R.drawable.ic_apps, R.drawable.ic_laptop_mac, R.drawable.ic_mood};
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
final BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
return imagesOnly.length;
}
@Override
public Object getItem(int i) {
return imagesOnly[i];
}
@Override
public long getItemId(int i) {
return imagesOnly[i];
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null)
view = new ImageView(getContext());
((ImageView) view).setImageResource((int) getItem(i));
return view;
}
};
return new AlertDialog.Builder(getContext()).setAdapter(adapter,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
((MainActivity) getContext()).OnSelected(SimpleFragment.this.getClass(), (int) adapter.getItem(i));
}
}).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
}).setCancelable(true).setTitle("Pick image(simple)").create();
}
}
WithNamesFragment
使用SimpleAdapter显示带有名称的图片。您需要构建imagesWithNames
public static class WithNamesFragment extends DialogFragment {
final static ArrayList<HashMap<String, ?>> imagesWithNames = new ArrayList<>();
static {
HashMap<String, Object> row = new HashMap<>();
row.put("imageID", R.drawable.ic_android);
row.put("name", "Android");
imagesWithNames.add(row);
row = new HashMap<>();
row.put("imageID", R.drawable.ic_apps);
row.put("name", "Apps");
imagesWithNames.add(row);
row = new HashMap<>();
row.put("imageID", R.drawable.ic_laptop_mac);
row.put("name", "Laptop Mac");
imagesWithNames.add(row);
row = new HashMap<>();
row.put("imageID", R.drawable.ic_mood);
row.put("name", "Mud black");
imagesWithNames.add(row);
}
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
final SimpleAdapter adapter = new SimpleAdapter(getContext(), imagesWithNames, R.layout.simple_row_with_image,
new String[]{"name", "imageID"}, new int[]{R.id.text1, R.id.image1});
return new AlertDialog.Builder(getContext()).setAdapter(adapter,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
((MainActivity) getContext()).OnSelected(WithNamesFragment.this.getClass(), (int) ((HashMap<String, Object>) adapter.getItem(i)).get("imageID"));
}
}).setCancelable(true).setTitle("Pick image(with names)").create();
}
}
用法 - 重要的是回调public void OnSelected(Class<?> clazz, int i)
当然clazz
仅用于记录目的,因此您可以删除它...也没有取消回调 - 这取决于您
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String PICKER_TAG = "PICKER_TAG";
static boolean mShowSimple = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Button button = new Button(this);
button.setText("Click me!");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mShowSimple)
showSimple();
else
show();
mShowSimple = !mShowSimple;
}
});
setContentView(button);
}
;
void show() {
new WithNamesFragment().show(getSupportFragmentManager(), PICKER_TAG);
}
void showSimple() {
new SimpleFragment().show(getSupportFragmentManager(), PICKER_TAG);
}
public void OnSelected(Class<?> clazz, int i) {
Toast toast = Toast.makeText(this, "Class \"" + clazz.getSimpleName() + "\" returns this image with id: " + getResources().getResourceName(i), Toast.LENGTH_SHORT);
final TextView toastView = (TextView) toast.getView().findViewById(android.R.id.message);
toastView.setCompoundDrawablesWithIntrinsicBounds(0, i, 0, 0);
toast.show();
}
}
simple_row_with_image.xml(只有名称版本需要)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@id/image1"
android:layout_toRightOf="@id/image1" />
</RelativeLayout>