如何在play框架scala应用程序中显示S3私有访问图像/视频

时间:2016-05-21 06:23:21

标签: scala amazon-s3 playframework playframework-2.0

目的:

我有一个S3视频网址和S3 Image网址。 S3存储桶具有私有访问权限(非公开)。我有一个"访问密钥ID"和#34;秘密访问密钥"访问S3存储桶。我必须在屏幕上显示S3图像/视频而不在本地存储。我正在使用play framework 2.5.3和scala语言。

我尝试了什么:

我在互联网上搜索过。我使用下面的插件来使用S3。

https://github.com/seratch/AWScala

https://github.com/Kaliber/play-s3

通过使用上述插件,我们可以读取图像/视频文件内容。但我不想阅读整个文件内容。我想在视图中显示图像/视频而不存储在本地。

我想要的是什么:

如果有人提出他们的建议会有所帮助。

1)是否可以使用"访问密钥ID"从S3 URL播放视频和显示图像。和#34;秘密访问密钥"在我的Play scala应用程序中。

2)如果是,请分享这样做的方式。感谢!!!

更新于24-05-2016:

我尝试过以下方式:

public class SearchResult extends AppCompatActivity {

private ListView lvSearch;
private ProgressDialog dialog;
private final String URL_TO_HIT = "http://www.abcd.com/mobile_search.php";
private String location = "bathinda";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_result);
    setContentView(R.layout.activity_search_result);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("Search Result");
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            onBackPressed();
        }
    });
    dialog = new ProgressDialog(this);
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);
    dialog.setMessage("Loading. Please wait...");
    // Create default options which will be used for every
    //  displayImage(...) call if no options will be passed to this method
    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .build();
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
            .defaultDisplayImageOptions(defaultOptions)
            .build();
    ImageLoader.getInstance().init(config); // Do it on Application start

    lvSearch = (ListView)findViewById(R.id.lvSearch);

    Bundle bundle = getIntent().getExtras();
    String bar = bundle.getString("bar");
    String nights = bundle.getString("nights");
    String nearby = bundle.getString("nearby");
    String deals = bundle.getString("deals");

    // To start fetching the data when app start, uncomment below line to start the async task.
    new JSONTask().execute(URL_TO_HIT, location, bar, nights, nearby, deals );

}
public class JSONTask extends AsyncTask<String,String, List<SearchData> >{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog.show();
    }

    @Override
    protected List<SearchData> doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));
            StringBuffer buffer = new StringBuffer();
            String line ="";
            while ((line = reader.readLine()) != null){
                buffer.append(line);
            }

            String finalJson = buffer.toString();

            JSONObject parentObject = new JSONObject(finalJson);
            JSONArray parentArray = parentObject.getJSONArray("search");

            List<SearchData> searchDataList = new ArrayList<>();

            Gson gson = new Gson();
            for(int i=0; i<parentArray.length(); i++) {
                JSONObject finalObject = parentArray.getJSONObject(i);
                /**
                 * below single line of code from Gson saves you from writing the json parsing yourself which is commented below
                 */
                SearchData searchData = gson.fromJson(finalObject.toString(), SearchData.class);

                searchDataList.add(searchData);
            }
            return searchDataList;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            if(connection != null) {
                connection.disconnect();
            }
            try {
                if(reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return  null;
    }

    @Override
    protected void onPostExecute(final List<SearchData> result) {
        super.onPostExecute(result);
        dialog.dismiss();
        if(result != null) {
            SearchAdapter adapter = new SearchAdapter(getApplicationContext(), R.layout.searchresultrow, result);
            lvSearch.setAdapter(adapter);

        } else {
            Toast.makeText(getApplicationContext(), "Not able to fetch data from server, please check internet", Toast.LENGTH_SHORT).show();
        }
    }
}

public class SearchAdapter extends ArrayAdapter{

    private List<SearchData> searchDataList;
    private int resource;
    private LayoutInflater inflater;
    public SearchAdapter(Context context, int resource, List<SearchData> objects) {
        super(context, resource, objects);
        searchDataList = objects;
        this.resource = resource;
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        if(convertView == null){
            holder = new ViewHolder();
            convertView = inflater.inflate(resource, null);
            holder.searchimg11 = (ImageView)convertView.findViewById(R.id.searchimg1);
            holder.barname1 = (TextView)convertView.findViewById(R.id.barname);
            holder.address1 = (TextView)convertView.findViewById(R.id.address);
            holder.offer1 = (TextView)convertView.findViewById(R.id.offer);
            holder.hourtext1 = (TextView)convertView.findViewById(R.id.hourtext);
            holder.coststext1 = (TextView)convertView.findViewById(R.id.coststext);
            holder.textv11 = (TextView)convertView.findViewById(R.id.textv1);
            holder.featuredtext1 = (TextView)convertView.findViewById(R.id.featuredtext);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //final ProgressBar progressBar = (ProgressBar)convertView.findViewById(R.id.progressBar);

        // Then later, when you want to display image
        ImageLoader.getInstance().displayImage(searchDataList.get(position).getBar_image(), holder.searchimg11, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                // progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                //progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                // progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {
                // progressBar.setVisibility(View.GONE);
            }
        });

        holder.barname1.setText(searchDataList.get(position).getBarname());
        holder.address1.setText(searchDataList.get(position).getLocation());
        holder.offer1.setText( searchDataList.get(position).getOffers());
        holder.hourtext1.setText( searchDataList.get(position).getOpen_hours());
        holder.coststext1.setText(searchDataList.get(position).getCost_for_two());
        holder.textv11.setText(searchDataList.get(position).getFreebe());
        holder.featuredtext1.setText(searchDataList.get(position).getFeaured());
        return convertView;
    }

    class ViewHolder{
        private ImageView searchimg11;
        private TextView address1;
        private TextView offer1;
        private TextView hourtext1;
        private TextView coststext1;
        private TextView textv11;
        private TextView barname1;
        private TextView featuredtext1;
    }

}

但我确实得到了以下异常 - 错误:: orginalUri

上述实施有什么问题?

0 个答案:

没有答案