ギャラリー機能を備えたアンドロイドと初心者に比較的新しいです。私のアプリではギャラリーフォルダ(ギャラリー/ MyAppFolder)に画像を保存しています。これらのフォルダをGridView内に表示しています。ここまで期待通りにうまくいきます。今私は特定の画像を表示する必要がありますグリッドビュー上でクリックリスナーを実装しようとしています。以下は、私が意図していない成功のが、onActivityResultメソッドを使用してみていると述べた目的オープニング画像はアプリフォルダ内に保存されました
public String FolderPath = Environment.getExternalStorageDirectory().getPath() + "/MyAppFolder";
private void setGridAdapter(String path) {
// Create a new grid adapter
gridItems = createGridItems(path);
MyGridAdapter adapter = new MyGridAdapter(this, gridItems);
// Set the grid adapter
GridView gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(adapter);
// Set the onClickListener
gridView.setOnItemClickListener(this);
}
private List<GridViewItem> createGridItems(String directoryPath) {
List<GridViewItem> items = new ArrayList<GridViewItem>();
// List all the items within the folder.
files = new File(directoryPath).listFiles(new ImageFileFilter());
for (File file : files) {
// Add the directories containing images or sub-directories
if (file.isDirectory()
&& file.listFiles(new ImageFileFilter()).length > 0) {
items.add(new GridViewItem(file.getAbsolutePath(), true, null));
}
// Add the images
else {
Bitmap image = BitmapHelper.decodeBitmapFromFile(file.getAbsolutePath(),
50,
50);
items.add(new GridViewItem(file.getAbsolutePath(), false, image));
}
}
return items;
}
private boolean isImageFile(String filePath) {
if (filePath.endsWith(".jpg") || filePath.endsWith(".png") || filePath.endsWith(".pdf"))
// Add other formats as desired
{
return true;
}
return false;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (gridItems.get(position).isDirectory()) {
setGridAdapter(gridItems.get(position).getPath());
}
else {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"),SELECT_PICTURE);
Log.e("Path for clicked Item::",gridItems.get(position).getPath());
}
}
private class ImageFileFilter implements FileFilter {
@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
else if (isImageFile(file.getAbsolutePath())) {
return true;
}
return false;
}
}
を達成するために、私の活動にしようとしているコードがあります。これは私のgridviewを設定し、私のアプリの中にフォルダを表示するために従ったsolutionです。私のレイアウトで直接画像を開くために、あなたはonClick Listenerで何をすべきか教えてください。私はギャラリーを通過してイメージを表示したくありません。
ピカソについては、基本的には、いくつかの非同期タスクを実行しているときには使用されません。つまり、画像のネットを経由するときです。この場合、私は自分のデバイスにアクセスしています。 – Umair