2016-11-25 66 views
0

獲取可繪製文件夾下的多個圖像並以網格視圖顯示。如何在點擊後將圖像存儲在內存中?

在網格視圖中顯示它之後,當我們點擊一​​個特定的圖像時,它必須存儲在設備的內部存儲器中。 我實現的代碼如下所示

public class Example3 extends AppCompatActivity { 
    String[] web = {"image1", "image2", "image3", 
      "image4", "image5", "image6", "image7", 
      "image8", "image9", "image10", "image11"}; 

    Integer[] displayImages = {R.drawable.image1, R.drawable.image2, R.drawable.image3, 
      R.drawable.image4, R.drawable.image5, R.drawable.image6, 
      R.drawable.image7, R.drawable.image8, R.drawable.image9, R.drawable.image10, R.drawable.image11}; 
    GridView gridView; 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.example3); 

     gridView = (GridView) findViewById(R.id.gridView1); 
     gridView.setAdapter(new ImageAdapter(this)); 

     gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 
       Toast.makeText(Example3.this, "You Clicked at " + web[+position] + " saved Successfully", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

    private class ImageAdapter extends BaseAdapter { 

     Context context; 

     public ImageAdapter(Context context) { 
      this.context = context; 
     } 

     @Override 
     public int getCount() { 
      return displayImages.length; 
     } 

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

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

     @Override 
     public View getView(int position, View view, ViewGroup viewGroup) { 
      ImageView imageView = new ImageView(this.context); 
      imageView.setImageResource(displayImages[position]); 
      imageView.setLayoutParams(new GridView.LayoutParams(180, 150)); 
      return imageView; 
     } 
    } 

} 

我的問題是 - 如何存儲在內存中的特定圖像?

+0

請詢問具體的問題,而不是一個一般的,unprecise目標 –

+0

你到底在期待什麼,有什麼問題?你應該顯示你的代碼,以便我們可以幫助你。 – Inconnu

+0

下一步在設備內存中存儲圖像.. –

回答

0

調用這個方法在你的形象點擊收聽:

public void addImageIntoSdcard(int position) { 

    //String extStorageDirectory = Environment.getExternalStorageDirectory().toString()+"/MyImages"; 
    File direct = new File(Environment.getExternalStorageDirectory(),"DEMO"); 

    if (!direct.exists()) { 
     direct.mkdirs(); //directory is created; 

    } 

    Bitmap bm = BitmapFactory.decodeResource(getResources(), displayImages[position]); 


    File file = new File(direct, web[position] + ".jpg"); 
    if(file.exists()){ 
     Toast.makeText(Example3.this, "You Clicked at " + web [+ position] + " already exists", Toast.LENGTH_SHORT).show(); 

    } 
    else { 
     Toast.makeText(Example3.this, "You Clicked at " + web [+ position] + " saved Successfully", Toast.LENGTH_SHORT).show(); 

    } 

    FileOutputStream outStream = null; 
    try { 
     outStream = new FileOutputStream(file); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    bm.compress(Bitmap.CompressFormat.JPEG, 100, outStream); 
    try { 
     outStream.flush(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    try { 
     outStream.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 


} 

請確保您有設定權限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
+0

謝謝..但是,名爲MyImages的文件夾沒有創建 – sree

+0

我將在你的內存中創建。 –

+0

我已更新我的code.it將在您的內存中創建。 –

0
@Override 
     public View getView(int position, View view, ViewGroup viewGroup) { 
      ImageView imageView = new ImageView(this.context); 
      imageView.setImageResource(displayImages[position]); 
      imageView.setLayoutParams(new GridView.LayoutParams(180, 150)); 
      //add an onClickListener for the image view here to identify the specific imageview 
      return imageView; 
     } 
0

假設您的圖形中有一個圖像,即ic_launcher。然後,從這樣的形象得到一個位圖對象:

String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); 

然後保存使用對電網點擊到SD卡:

File file = new File(extStorageDirectory, "ic_launcher.PNG"); 
    outStream = new FileOutputStream(file); 
    bm.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
    outStream.flush(); 
    outStream.close(); 

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 

到SD卡的路徑可以使用檢索不要忘記添加android.permission.WRITE_EXTERNAL_STORAGE權限。

+0

罰款...但如果想要添加多個那麼圖像怎麼樣? – sree

+0

你想添加多個圖像? –

+0

把所有的東西放在循環中 –

相關問題