2012-05-29 158 views
3

我想2個星期來找到如何共享存儲在SD卡上的圖像沒有成功。從SD卡共享圖像

answer不工作給我,也不是我要找的東西。

我與凸輪預覽應用存儲圖像,SD,然後在應用內展示他們的畫廊工作:

public class GalleryView extends Activity { 

    private static final int SELECT_PICTURE = 1; 

private String selectedImagePath; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.gallery_view); 

    Gallery g = (Gallery) findViewById(R.id.gallery); 
    g.setAdapter(new ImageAdapter(this, ReadSDCard())); 


    g.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
    Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE); 
     } 
    }); 
} 

    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (requestCode == SELECT_PICTURE) { 
      Uri selectedImageUri = data.getData(); 
      selectedImagePath = getPath(selectedImageUri); 

      Intent shareIntent = new Intent(); 
      shareIntent.setAction(Intent.ACTION_SEND); 
      shareIntent.putExtra(Intent.EXTRA_STREAM, selectedImagePath); 
      shareIntent.setType("image/*"); 
      startActivity(Intent.createChooser(shareIntent, "Share image")); 
     } 
    } 
} 

public String getPath(Uri uri) { 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = managedQuery(uri, projection, null, null, null); 
    int column_index = cursor 
      .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
    return cursor.getString(column_index); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy();  
} 

private List<String> ReadSDCard() { 

    createDirIfNotExists(); 

    List<String> tFileList = new ArrayList<String>(); 

    //It have to be matched with the directory in SDCard 
    File f = new File("/sdcard/Cam App"); 

    File[] files=f.listFiles(); 

    for(int i=0; i<files.length; i++) { 
     File file = files[i]; 
     /*It's assumed that all file in the path are in supported type*/ 
     tFileList.add(file.getPath()); 
    } 

    return tFileList; 
} 

class ViewHolder { 
    ImageView i; 
} 

public class ImageAdapter extends BaseAdapter { 
    int mGalleryItemBackground; 
    private Context mContext; 
    private List<String> FileList; 
    LayoutInflater inflater = getLayoutInflater(); 
    ViewHolder holder; 

    public ImageAdapter(Context c, List<String> fList) { 
     mContext = c; 
     FileList = fList; 
     TypedArray a = obtainStyledAttributes(R.styleable.GalleryTheme); 
     mGalleryItemBackground = a.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0); 
     a.recycle(); 
    } 

    public int getCount() { 
     return FileList.size(); 
    } 

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

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

    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView i = new ImageView(mContext); 

     Bitmap bm = BitmapFactory.decodeFile(FileList.get(position).toString()); 
     i.setImageBitmap(bm); 

     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.gallery_item, parent, false); 

      holder = new ViewHolder(); 
      holder.i = ((ImageView) convertView.findViewById(R.id.imagenGallery)); 

      convertView.setTag(holder); 
     } 
     holder.i.setScaleType(ImageView.ScaleType.FIT_XY); 
     holder.i.setBackgroundResource(mGalleryItemBackground); 

     return i; 
    } 
} 

public TypedArray obtainStyledAttributes(int theme) { 
    // TODO Auto-generated method stub 
    return null; 
} 
} 

畫廊工作正常,但我不明白如何分享保存在SD上的圖像。

如何訪問凸輪應用程序文件夾中的圖像並分享當時出現在圖庫中的圖像?

我試圖使用位置,但它只適用於不變的圖像。任何幫助/提示,​​將不勝感激。

+0

05-30 18:51: 11.515:E/AndroidRuntime(16500):java.lang.RuntimeException:未將結果resultInfo {who = null,request = 1,result = -1,data = Intent {(has extras)}}傳遞給activity {com.nightin。 camapp/com.nightin.ca.camapp.GalleryView}:java.la ng.NullPointerException。這是我最後的日誌:( –

+0

現在我已經試過這樣:公共無效onActivityResult(INT requestCode,INT resultCode爲,意圖數據){ \t如果(resultCode爲== RESULT_OK){ \t如果(requestCode == SELECT_PICTURE){ \t Uri selectedImageUri = data.getData(); \t selectedImagePath = getPath(selectedImageUri); –

回答

4

好了,我終於找到了一個解決這個,但它不正是我想要的:

g.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 

      Intent intent = new Intent(); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      intent.setDataAndType(Uri.parse("file://" + "/sdcard/Cam App"), "image/*"); 
      startActivityForResult(Intent.createChooser(intent, "Select image to share:"), SELECT_PICTURE); 
     } 
    }); 
} 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (requestCode == SELECT_PICTURE) { 
      Uri selectedImageUri = data.getData(); 
      //OI FILE Manager 
      filemanagerstring = selectedImageUri.getPath(); 

      //MEDIA GALLERY 
      selectedImagePath = getPath(selectedImageUri); 

      //NOW WE HAVE OUR WANTED STRING 
      if(selectedImagePath!=null) 
       System.out.println("selectedImagePath is the right one for you!"); 
      else 
       System.out.println("filemanagerstring is the right one for you!"); 

      Intent shareIntent = new Intent(); 
      shareIntent.setAction(Intent.ACTION_SEND); 
      shareIntent.putExtra(Intent.EXTRA_STREAM, selectedImageUri); 
      shareIntent.setType("image/*"); 
      startActivity(Intent.createChooser(shareIntent, "Share image via:")); 
     } 
    } 
} 

@SuppressWarnings("deprecation") 
public String getPath(Uri uri) { 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = managedQuery(uri, projection, null, null, null); 
    if(cursor!=null) { 
     //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL 
     //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA 
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } 
    else return null; 
} 

如果有人有什麼更好的解決辦法,請分享。

0

下面的代碼工作正常,如果您知道圖像

Intent shareit=new Intent(Intent.ACTION_SEND); 
shareit.setType("image/jpeg"); 
File f = new File("imagepath"); 
shareit.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f)); 
startActivity(Intent.createChooser(shareit, "Share Image Via")); 

創建映像文件名的文件的路徑,

File f = new File(Environment.getExternalStorageDirectory() + File.separator + "image_file.jpg");