2012-11-16 215 views
5

如何顯示Android圖庫上特定文件夾的所有圖像,例如whatapp。 我真的使用MediaScannerConnectionClient如何顯示Android圖庫上特定文件夾的圖像

File folder = new File("/sdcard/myfolder/"); 
allFiles = folder.list(); 
SCAN_PATH=Environment.getExternalStorageDirectory().toString()+"/myfolder/"+allFiles[0]; 
@Override 
public void onScanCompleted(String path, Uri uri) { 
    try { 
     if (uri != null) { 
      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setData(uri); 
      startActivity(intent); 

     } 
    } finally { 
     conn.disconnect(); 
     conn = null; 
    } 
} 

private void startScan() { 
    if (conn != null) { 
     conn.disconnect(); 
    } 
    conn = new MediaScannerConnection(this, this); 
    conn.connect(); 
} 
    @Override 
public void onMediaScannerConnected() { 
    conn.scanFile(SCAN_PATH, "image/*"); 
} 

但我真的在這一點上得到一個錯誤:

Intent intent = new Intent(Intent.ACTION_VIEW); 
    intent.setData(uri); 
    startActivity(intent); 

具體位置:

startActivity(intent); 

輪不到類型:內容://media/external/images/media/267830 未找到活動處理意圖

on onScan完成我的路徑和uri參數不爲null。

+0

文你給一個靜態路徑ü不需要調用,您的使用意圖 –

+0

你說,我不需要內部onScanCompleted代碼試圖畫廊? – Rafael

+0

:是的..動作視圖不會給你你想要做的靜態路徑。 –

回答

11

嗨,你可以使用下面的代碼,我希望它可以幫助你。

package com.example.browsepicture; 

import java.io.File; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.media.MediaScannerConnection; 
import android.media.MediaScannerConnection.MediaScannerConnectionClient; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class BrowsePicture2 extends Activity { 
    String SCAN_PATH; 
    File[] allFiles ; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_browse_picture); 

     File folder = new File(Environment.getExternalStorageDirectory().getPath()+"/aaaa/"); 
     allFiles = folder.listFiles(); 

     ((Button) findViewById(R.id.button1)) 
       .setOnClickListener(new OnClickListener() { 
        public void onClick(View arg0) { 
         new SingleMediaScanner(BrowsePicture2.this, allFiles[0]); 
        } 
       }); 
    } 

    public class SingleMediaScanner implements MediaScannerConnectionClient { 

     private MediaScannerConnection mMs; 
     private File mFile; 

     public SingleMediaScanner(Context context, File f) { 
      mFile = f; 
      mMs = new MediaScannerConnection(context, this); 
      mMs.connect(); 
     } 

     public void onMediaScannerConnected() { 
      mMs.scanFile(mFile.getAbsolutePath(), null); 
     } 

     public void onScanCompleted(String path, Uri uri) { 
      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setData(uri); 
      startActivity(intent); 
      mMs.disconnect(); 
     } 

    } 
} 
+0

talhakosen,我認爲這個例子有點不同。有沒有一種方法,而不是我開發我自己的查看器,我使用android本地圖庫來顯示我的圖像文件夾? – Rafael

+0

好的,我理解並編輯了我的答案。希望它有助於:) – Talha

+1

我仍然不能投票,但talhakosen代碼是正確的,謝謝 – Rafael

0

@Talha給出的答案很好,但它嘗試使用圖像應用程序選項打開圖像。如果你想只刷新畫廊與SD卡的文件夾,你可以SingleMediaScanner

class SingleMediaScanner implements MediaScannerConnectionClient { 

    private MediaScannerConnection mMs; 
    private File mFile; 

    public SingleMediaScanner(Context context, File f) { 
     mFile = f; 
     mMs = new MediaScannerConnection(context, this); 
     mMs.connect(); 
    } 

    public void onMediaScannerConnected() { 
     mMs.scanFile(mFile.getAbsolutePath(), null); 
    } 

    public void onScanCompleted(String path, Uri uri) { 

     mMs.disconnect(); 
    } 

} 

而且在點擊按鈕循環每個文件,如下修改代碼,您可以:

File folder = new File(Environment.getExternalStorageDirectory().getPath()+"/aaaa/"); 
    allFiles = folder.listFiles(); 

並傳遞到SingleMediaScanner一次。

它工作在我的情況。

1

您應該添加網格視圖Adapter類。

public class GalleryPictureActivity extends Activity 
{ 
    private String[]  FilePathStrings; 
    private File[]   listFile; 
    GridView    grid; 
    GridViewAdapter   adapter; 
    File     file; 
    public static Bitmap bmp = null; 
    ImageView    imageview; 

    @Override 
    protected void onCreate (Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_gallery_picture); 
     // Check for SD Card 
     if (!Environment.getExternalStorageState().equals(
       Environment.MEDIA_MOUNTED)) 
     { 
      Toast.makeText(this, "Error! No SDCARD Found!", 
        Toast.LENGTH_LONG).show(); 
     } 
     else 
     { 
      // Locate the image folder in your SD Card 
      file = new File(Environment.getExternalStorageDirectory() 
        .getPath() + "/images"); 
     } 
     if (file.isDirectory()) 
     { 
      listFile = file.listFiles(); 
      FilePathStrings = new String[listFile.length]; 
      for (int i = 0; i < listFile.length; i++) 
      { 
       FilePathStrings[i] = listFile[i].getAbsolutePath(); 
      } 
     } 
     grid = (GridView)findViewById(R.id.gridview); 
     adapter = new GridViewAdapter(this, FilePathStrings); 
     grid.setAdapter(adapter); 

     grid.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick (AdapterView<?> parent, View view, 
        int position, long id) 
      { 
       imageview = (ImageView)findViewById(R.id.imageView1); 
       int targetWidth = 700; 
       int targetHeight = 500; 
       BitmapFactory.Options bmpOptions = new BitmapFactory.Options(); 
       bmpOptions.inJustDecodeBounds = true; 
       BitmapFactory.decodeFile(FilePathStrings[position], 
         bmpOptions); 
       int currHeight = bmpOptions.outHeight; 
       int currWidth = bmpOptions.outWidth; 
       int sampleSize = 1; 
       if (currHeight > targetHeight || currWidth > targetWidth) 
       { 
        if (currWidth > currHeight) 
         sampleSize = Math.round((float)currHeight 
           /(float)targetHeight); 
        else 
         sampleSize = Math.round((float)currWidth 
           /(float)targetWidth); 
       } 
       bmpOptions.inSampleSize = sampleSize; 
       bmpOptions.inJustDecodeBounds = false; 
       bmp = BitmapFactory.decodeFile(FilePathStrings[position], 
         bmpOptions); 
       imageview.setImageBitmap(bmp); 
       imageview.setScaleType(ImageView.ScaleType.FIT_XY); 
       bmp = null; 

      } 
     }); 

    } 
} 

另一個類的GridView適配器:

public class GridViewAdapter extends BaseAdapter 
{ 
    private Activity    activity; 
    private String[]    filepath; 
    private static LayoutInflater inflater = null; 
    Bitmap       bmp   = null; 

    public GridViewAdapter (Activity a, String[] fpath) 
    { 
     activity = a; 
     filepath = fpath; 
     inflater = (LayoutInflater)activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public int getCount() 
    { 
     return filepath.length; 
    } 

    public Object getItem (int position) 
    { 
     return position; 
    } 

    public long getItemId (int position) 
    { 
     return position; 
    } 

    public View getView (int position, View convertView, ViewGroup parent) 
    { 
     View vi = convertView; 
     if (convertView == null) 
      vi = inflater.inflate(R.layout.gridview_item, null); 
     ImageView image = (ImageView)vi.findViewById(R.id.image); 
     int targetWidth = 100; 
     int targetHeight = 100; 
     BitmapFactory.Options bmpOptions = new BitmapFactory.Options(); 
     bmpOptions.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(filepath[position], bmpOptions); 
     int currHeight = bmpOptions.outHeight; 
     int currWidth = bmpOptions.outWidth; 
     int sampleSize = 1; 
     if (currHeight > targetHeight || currWidth > targetWidth) 
     { 
      if (currWidth > currHeight) 
       sampleSize = Math.round((float)currHeight 
         /(float)targetHeight); 
      else 
       sampleSize = Math.round((float)currWidth 
         /(float)targetWidth); 
     } 
     bmpOptions.inSampleSize = sampleSize; 
     bmpOptions.inJustDecodeBounds = false; 
     bmp = BitmapFactory.decodeFile(filepath[position], bmpOptions); 
     image.setImageBitmap(bmp); 
     image.setScaleType(ImageView.ScaleType.FIT_XY); 
     bmp = null; 
     return vi; 
    } 
} 

活動:

activity_gallery_picture:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/LinearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" > 

    <GridView 
      android:id="@+id/gridview" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:layout_weight=".85"> 

    </GridView> 

    <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight=".25" 
      android:scaleType="fitXY" 
      android:src="@drawable/galleryimage" /> 

    </LinearLayout> 

另一個活動佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" > 
    <ImageView 
      android:id="@+id/image" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 
</RelativeLayout> 
+0

Mediascanner將在您掃描時顯示已刪除的圖像 – krishnan

0

您可以使用android.database.Cursor

public boolean OpenGalleryFromFolder(android.content.Context context, String folderName) 
{ 
    String filePath = android.os.Environment.getExternalStorageDirectory().getPath() + "/Pictures/" + folderName + "/"; 
    return OpenGalleryFromPathToFolder(context, filePath); 
} 

// Finds the first image in the specified folder and uses it to open a the devices native gallery app with all images in that folder. 
public boolean OpenGalleryFromPathToFolder(android.content.Context context, String folderPath) 
{ 
    java.io.File folder = new java.io.File(folderPath); 
    java.io.File[] allFiles = folder.listFiles(); 

    if (allFiles != null && allFiles.length > 0) 
    { 
     android.net.Uri imageInFolder = getImageContentUri(context, allFiles[0]); 
     if (imageInFolder != null) 
     { 
      android.content.Intent intent = new android.content.Intent(android.content.Intent.ACTION_VIEW); 
      intent.setData(imageInFolder); 
      context.startActivity(intent); 
      return true; 
     } 
    } 
    return false; 
} 

// converts the absolute path of a file to a content path 
// absolute path example: /storage/emulated/0/Pictures/folderName/Image1.jpg 
// content path example: content://media/external/images/media/47560 
private android.net.Uri getImageContentUri(android.content.Context context, java.io.File imageFile) { 
    String filePath = imageFile.getAbsolutePath(); 
    android.database.Cursor cursor = context.getContentResolver().query(
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
      new String[]{android.provider.MediaStore.Images.Media._ID}, 
      android.provider.MediaStore.Images.Media.DATA + "=? ", 
      new String[]{filePath}, null); 
    if (cursor != null && cursor.moveToFirst()) { 
     int id = cursor.getInt(cursor.getColumnIndex(android.provider.MediaStore.MediaColumns._ID)); 
     return android.net.Uri.withAppendedPath(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + id); 
    } else { 
     if (imageFile.exists()) { 
      android.content.ContentValues values = new android.content.ContentValues(); 
      values.put(android.provider.MediaStore.Images.Media.DATA, filePath); 
      return context.getContentResolver().insert(
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
     } else { 
      return null; 
     } 
    } 
} 
相關問題