android从SD卡读取图像

moe*_*moe 6 android sd-card

我正在尝试从我的SD卡读取图像,但我不确定我是否做得对.需要一些帮助

这是我阅读它的代码:

String imageInSD = "/sdcard/Hanud/" + c.getString(1) + ".PNG";
        Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
        ImageView myImageView = (ImageView)findViewById(R.id.imageview);
        myImageView.setImageBitmap(bitmap);
Run Code Online (Sandbox Code Playgroud)

这是我的主要文件:

<?xml version="1.0" encoding="utf-8"?>
<WebView 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/webview"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

 <!-- <ImageView
    android:id="@+id/image"
    android:scaleType="center"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    />-->

    <ImageView
  android:id="@+id/imageview"
  android:layout_gravity="center"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:scaleType="center"
  />


  <TextView  
    android:id="@+id/myNameText"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
  />
</WebView>
Run Code Online (Sandbox Code Playgroud)

Jac*_*ano 9

它应该是:

String imageInSD = Environment.getExternalStorageDirectory().getAbsolutePath() +"/Hanud/" + c.getString(1) + ".PNG";
        Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
        ImageView myImageView = (ImageView)findViewById(R.id.imageview);
        myImageView.setImageBitmap(bitmap);
Run Code Online (Sandbox Code Playgroud)

这将确保您找到正确的目录!


小智 0

首先定义xml

\n\n
<? Xml version = \xe2\x80\x9c1.0\xe2\x80\xb3 encoding = \xe2\x80\x9cutf-8\xe2\x80\xb3?>\n<RelativeLayout xmlns: android = \xe2\x80\x9chttp://schemas.android.com/apk/res/android\xe2\x80\x9d\nandroid: orientation = \xe2\x80\x9cvertical\xe2\x80\x9d\nthe android: layout_width = \xe2\x80\x9cfill_parent The\nandroid: layout_height = \xe2\x80\x9cfill_parent The\nandroid: background = \xe2\x80\x9c55 million\xe2\x80\x9d\n>\n<ImageSwitcher / / this control with the Gallery often used, remember\nandroid: id = \xe2\x80\x9c@ + the id / in switcher \xe2\x80\x98\xe2\x80\x9d\nthe android: layout_width = \xe2\x80\x9cwrap_content The\nandroid: layout_height = \xe2\x80\x9c350dip The\nandroid: layout_alignParentLeft = \xe2\x80\x9ctrue\xe2\x80\x9d\nandroid: layout_alignParentRight = \xe2\x80\x9ctrue\xe2\x80\x9d\n/>\n< Gallery\nandroid: id = \xe2\x80\x9c@ + id / mygallery\xe2\x80\x9d\nthe android: layout_width = \xe2\x80\x9cfill_parent The\nandroid: layout_height = \xe2\x80\x9c80dp\xe2\x80\x9d\nandroid: layout_alignParentBottom = \xe2\x80\x9ctrue\xe2\x80\x9d\nandroid: layout_alignParentLeft = \xe2\x80\x9ctrue\xe2\x80\x9d\nandroid: gravity = \xe2\x80\x9ccenter_vertical\xe2\x80\x9d\nandroid: spacing = \xe2\x80\x9c16dp\xe2\x80\x9d\n/>\n</ RelativeLayout>\n
Run Code Online (Sandbox Code Playgroud)\n\n

二。自定义显示中的活动

\n\n
package com.ldci.second.mypnone;\n\nimport java.io.File;\nimport java.util.ArrayList;\nimport of java.util.List;\n\nimport android.app.Activity;\nimport android.content.Context;\nimport android.content.res.TypedArray;\nimport the android.graphics.Bitmap;\nimport android.graphics.BitmapFactory;\nimport android.net.Uri;\nimport android.os.Bundle;\nimport android.util.Log;\nimport android.view.View;\nimport android.view.ViewGroup;\nimport android.view.WindowManager;\nimport android.view.View.OnClickListener;\nimport android.view.animation.AnimationUtils;\nimport android.widget.AdapterView;\nimport android.widget.BaseAdapter;\nimport android.widget.Gallery;\nimport android.widget.ImageSwitcher;\nimport android.widget.ImageView;\nimport android.widget.Toast;\nimport android.widget.ViewSwitcher;\nimport android.widget.AdapterView.OnItemClickListener;\nimport android.widget.Gallery.LayoutParams;\n\npublic class BgPictureShowActivity extends Activity implements\nAdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {\n\nthe private List <String> the ImageList;\nprivate String [] list;\nprivate ImageSwitcher mSwitcher;\npublic void onCreate (Bundle savedInstanceState) {\nsuper.onCreate (savedInstanceState);\n/ / / / Remove the status bar, and display their own program name\n/ / GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,\n/ / WindowManager.LayoutParams.FLAG_FULLSCREEN);\nsetContentView (R.layout.pictureshow);\n\nThe ImageList the getSD ();\nlist = ImageList.toArray (new String [ImageList.size ()]);\n\n/ * Set the Switcher * /\nmSwitcher = (ImageSwitcher) findViewById (R.id.switcher);\nmSwitcher.setFactory (this);\n/ * Set the load Switcher mode * /\nmSwitcher.setInAnimation (AnimationUtils.loadAnimation (this,\nandroid.R.anim.fade_in));\n/ * Set output Switcher mode * /\nmSwitcher.setOutAnimation (AnimationUtils.loadAnimation (this,\nandroid.R.anim.fade_out));\nmSwitcher.setOnClickListener (new OnClickListener () {\n\npublic void onClick (View v) the {\nToast.makeText (BgPictureShowActivity.this, \xe2\x80\x9cclick\xe2\x80\x9d, Toast.LENGTH_SHORT). The show ();\n\n}\n\n});\n\nGallery g = (Gallery) findViewById (R.id.mygallery);\n\n/ * Add several ImageAdapter to and set to the Gallery object * /\ng.setAdapter (new ImageAdapter (this, getSD ()));\n\ng.setOnItemSelectedListener (this);\n\n/ * Set a itemclickListener event * /\ng.setOnItemClickListener (new OnItemClickListener ()\n{\npublic void the onItemClick (AdapterView, which <?> parent,\nView v, int position, long id)\n{\nToast.makeText (BgPictureShowActivity.this, \xe2\x80\x9cdianjiale\xe2\x80\x9d, Toast.LENGTH_SHORT). Show ();\n}\n});\n}\n\nprivate List <String> getSD ()\n{\n/ * Set the current path * /\nThe the List <String> it = the new ArrayList <String> ();\nFile f = new File (\xe2\x80\x9c/ sdcard /\xe2\x80\x9d);\nFile [] files = f.listFiles ();\n\n/ * All the files stored in the ArrayList * /\nfor (int i = 0; i <files.length; i + +)\n{\nThe File the file = files [i];\nif (getImageFile (file.getPath ()))\nit.add (file.getPath ());\n}\nreturn it;\n}\n\nprivate boolean getImageFile (String fName)\n{\na boolean the re;\n\n/ * Get the extension * /\nString end = fName.substring (fName.lastIndexOf (\xe2\x80\x9c.\xe2\x80\x9d) +1,\nfName.length ()). toLowerCase ();\n\n/ * Decided by the type of extension The MimeType * /\nif (end.equals (\xe2\x80\x9cjpg\xe2\x80\x9d) | | end.equals (\xe2\x80\x9cgif\xe2\x80\x9d) | | end.equals (\xe2\x80\x9cpng\xe2\x80\x9d)\n| | End.equals (\xe2\x80\x9cjpeg\xe2\x80\x9d) | | end.equals (\xe2\x80\x9cbmp\xe2\x80\x9d))\n{\nre = true;\n}\nelse\n{\nre = false;\n}\nreturn the re;\n}\n\n/ * To rewrite BaseAdapter custom one ImageAdapter class * /\npublic class ImageAdapter the extends BaseAdapter,\n{\n/ * Declare variables * /\nint mGalleryItemBackground;\nprivate Context mContext;\nprivate the List <String> of lis;\n\n/ * ImageAdapter, constructed character * /\nthe public ImageAdapter (Context c, the List <String> li)\n{\nmContext = c;\nof lis = li;\n/ * Res / values \xe2\x80\x8b\xe2\x80\x8b/ attrs.xml <declare-styleable> defined\n* Gallery attribute * /\nTypedArray a = obtainStyledAttributes (R.styleable.Gallery);\n/ * Obtain attribute Gallery Index id * /\nmGalleryItemBackground = a.getResourceId (\nR.styleable.Gallery_android_galleryItemBackground, 0);\n/ * Let the object styleable properties can be used repeatedly * /\na.recycle ();\n}\n\n/ * A few set to rewrite the getCount, the number of returned images * /\npublic int the getCount ()\n{\nreturn lis.size ();\n}\n/ * Must override the method getItem, returns the position * /\nPublic Object getItem (int position)\n{\nthe return position;\n}\n\n/ * Must be rewritten getItemId pass and position * /\npublic long getItemId (int position)\n{\nthe return position;\n}\n\n/ * A few set to rewrite the method getView pass and View objects * /\npublic View getView (int position, View convertView,\nOf ViewGroup that the parent)\n{\n/ * Generate the ImageView object * /\nImageView i = new ImageView (mContext);\n/ * Set the picture to imageView object * /\nBitmap bm = BitmapFactory.decodeFile (lis.\nget (position). toString ());\ni.setImageBitmap (bm);\n/ * Reset the image width and height * /\ni.setScaleType (ImageView.ScaleType.FIT_XY);\n/ * Reset the Layout of the width and height * /\ni.setLayoutParams (new Gallery.LayoutParams (136, 88));\n/ * Set the Gallery background images * /\ni.setBackgroundResource (mGalleryItemBackground);\n/ * Pass back to imageView object * /\nreturn i;\n}\n}\n\npublic void onItemSelected (AdapterView <?> parent, View view, int position,\nlong id) {\nThe string photoURL = list [position];\nLog.i (\xe2\x80\x9cA\xe2\x80\x9d, String.valueOf (position));\n\nmSwitcher.setImageURI (Uri.parse (photoURL));\n\n}\n\npublic void onNothingSelected (AdapterView <?> parent) {\n/ / TODO Auto-generated method the stub\n\n}\n\npublic View makeView () {\nImageView i = new ImageView (this);\ni.setBackgroundColor (0xFF000000);\ni.setScaleType (ImageView.ScaleType.FIT_CENTER);\ni.setLayoutParams (new ImageSwitcher.LayoutParams (\nLayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));\nreturn i;\n}\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

这段代码很长,看下去肯定会头疼加DAN痛苦,但是要坚持住,MARS大神反复告诫我们这个行业的特点就是要有耐心。\n有一个很重要的东西,在价值观\xe2\ x80\x8b\xe2\x80\x8b文件文件夹,在书中创建一个xml:

\n\n
<? Xml version = \xe2\x80\x9c1.0\xe2\x80\xb3 encoding = \xe2\x80\x9cutf-8\xe2\x80\xb3?>\n<resources>\n<declare-styleable name=\xe2\x80\x9dGallery\xe2\x80\x9d>\n<attr name=\xe2\x80\x9dandroid:galleryItemBackground\xe2\x80\x9d />\n</ Declare-styleable\n</ Resources>\n
Run Code Online (Sandbox Code Playgroud)\n\n

http://developer.aiwgame.com/gallery-show-an-image-from-android-sd-card.html

\n